Skip to content

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

Description

@alice-i-cecile

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions