rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.2k stars 12.56k forks source link

Confusing error message for `Foo{ bar @ .. }` pattern #74701

Open rodrimati1992 opened 4 years ago

rodrimati1992 commented 4 years ago

This erroneous code:

let std::ops::Range{ bar @ .. } = 0..0;

Produces this error message

error: expected `,`
 --> src/main.rs:2:26
  |
2 |     let std::ops::Range{ bar @ .. } = 0..0;
  |                          ^^^

It would be better if instead it produced an error message like this:

error: `bar @` is not allowed in a struct
 --> src/main.rs:2:26
  |
2 |     let std::ops::Range{ bar @ .. } = 0..0;
  |                          ^^^
  = help: remove this and bind each struct field independently
help: if you don't need to use the contents of bar, discard the struct's remaining fields
   |
11 |     let std::ops::Range{ .. } = 0..0;
   |                          ^^

Meta

Tried the code in these Rust versions and they all produced the same error message:

Nadrieril commented 10 months ago

This shouldn't be too hard. This comes from the parser, specifically the code that parses a pattern, which is one of these parse_pat_* functions. Somewhere there is the code to parse a struct pattern. I assume the error above happens when it successfully parses a struct field and then expects a comma or closing brace. To fix it you'd have to check for @ .. and report a better error message.

GrigorenkoPV commented 6 days ago

@rustbot claim