Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest the correct name if using a struct literal and struct update syntax #105398

Open
alexwlchan opened this issue Dec 6, 2022 · 2 comments
Open
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alexwlchan
Copy link

alexwlchan commented Dec 6, 2022

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d00e87e77db94bfa1cc76d92b966c599

#[derive(Debug)]
struct Shape<'a> {
    colour: &'a str,
    sides: usize,
}

fn main() {
    let blue_triangle = Shape {
        colour: "blue",
        sides: 3,
    };

    println!("The blue triangle is {:?}", blue_triangle);

    let blue_square = {
        sides: 4,
        ..blue_triangle
    };

    println!("The blue square is {:?}", blue_square);
}

The current output is:

error: struct literal body without path
  --> src/main.rs:15:23
   |
15 |       let blue_square = {
   |  _______________________^
16 | |         sides: 4,
17 | |         ..blue_triangle
18 | |     };
   | |_____^
   |
help: you might have forgotten to add the struct literal inside the block
   |
15 ~     let blue_square = { SomeStruct {
16 |         sides: 4,
17 |         ..blue_triangle
18 ~     } };
   |

Ideally the output should look like:

 error: struct literal body without path
   --> src/main.rs:15:23
    |
 15 |       let blue_square = {
    |  _______________________^
 16 | |         sides: 4,
 17 | |         ..blue_triangle
 18 | |     };
    | |_____^
    |
 help: you might have forgotten to add the struct literal inside the block
    |
 15 ~     let blue_square = { SomeStruct {
 16 |         sides: 4,
 17 |         ..blue_triangle
 18 ~     } };
    |
+help: perhaps you intended to use this struct literal:
+   |
+15 ~     let blue_square = Shape {
+16 |         sides: 4,
+17 |         ..blue_triangle
+18 |     };
+   |

You'd want to be somewhat conservative here, but in this case:

  • the struct I'm passing in ..blue_triangle is a Shape
  • the field I'm updating is also valid on Shape

For comparison with a similar construction in another language: TypeScript has a spread operator which is similar to Rust's struct update syntax, and it recognises that I'm overriding a field here without asking me to respecify the type. See TypeScript Playground example

@alexwlchan alexwlchan added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 6, 2022
@alexwlchan
Copy link
Author

Possibly related: #98282

@fmease
Copy link
Member

fmease commented Feb 29, 2024

We'd somehow need to stash the parse error without accidentally accepting this malformed input syntactically (relevant for #[cfg]'ed-out code) and steal it during type checking where we should have the necessary information available in theory. However, this looks non-trivial to fix from an architectural standpoint, let's see.

Assigning myself, so I don't forget to look into this at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants