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

Struct update syntax produces very confusing error messages when comma before .. is missed #104373

Closed
alice-i-cecile opened this issue Nov 13, 2022 · 3 comments · Fixed by #104504
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

@alice-i-cecile
Copy link

alice-i-cecile commented Nov 13, 2022

Begin with the following working code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=fb4c4c5e78cd9c3f549cdd806e68388a

Then, delete the , before ..Default::default(), resulting in:

#[derive(Default)]
struct Inner {
    a: u8,
    b: u8,
}

#[derive(Default)]
struct Outer {
    inner: Inner,
    defaulted: u8,
}

fn main(){
    Outer {
        inner: Inner {
            a: 1,
            b: 2,
        }
        ..Default::default()
    };
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
  --> src/main.rs:15:16
   |
15 |           inner: Inner {
   |  ________________^
16 | |             a: 1,
17 | |             b: 2,
18 | |         }
19 | |         ..Default::default()
   | |____________________________^ expected struct `Inner`, found struct `std::ops::Range`
   |
   = note: expected struct `Inner`
              found struct `std::ops::Range<Inner>`

error[[E0063]](https://doc.rust-lang.org/stable/error-index.html#E0063): missing field `defaulted` in initializer of `Outer`
  --> src/main.rs:14:5
   |
14 |     Outer {
   |     ^^^^^ missing `defaulted`
   |
help: to set the remaining fields from `Default::default()`, separate the last named field with a comma
   |
18 |         },
   |          +

Some errors have detailed explanations: E0063, E0308.
For more information about an error, try `rustc --explain E0063`.
error: could not compile `playground` due to 2 previous errors```

Ideally the output should explain that struct update syntax requires a comma separating the last field from the .. notation and point to that line.

Encountered in the wild in the Bevy community, although I run into this myself regularly when initializing structs by hand.

@alice-i-cecile alice-i-cecile 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 Nov 13, 2022
@compiler-errors
Copy link
Member

Ideally the output should explain that struct update syntax requires a comma separating the last field from the .. notation and point to that line.

This is always suggested at the bottom--how do you think that message can be improved?

@alice-i-cecile
Copy link
Author

alice-i-cecile commented Nov 13, 2022

Swapping the order or removing the first error completely would help: the latter is much more relevant and beginners often get flustered after the first incomprehensible error message.

In terms of text, I would try something closer to:

error[[E0063]](https://doc.rust-lang.org/stable/error-index.html#E0063): Missing comma: to set the remaining fields of this `Outer` object using `Default::default` add a comma after the last named field before the `..`.
  --> src/main.rs:14:5
   |
14 |     Outer {
   |
   |
18 |         }
   |     ^^^ missing comma after named item
   |          ..Default::default()
   |       }
   |          +

The fields aren't missing: the user has clear intent to declare them, and listing out which ones are missing won't help them solve the problem (and adds a lot of noise for complex types).

@pierwill
Copy link
Member

Hello, I have a draft PR to address this here #103838.

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

Successfully merging a pull request may close this issue.

3 participants