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

Error message when defining enum using struct syntax #103869

Closed
rdfriese opened this issue Nov 2, 2022 · 3 comments · Fixed by #105098
Closed

Error message when defining enum using struct syntax #103869

rdfriese opened this issue Nov 2, 2022 · 3 comments · Fixed by #105098
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rdfriese
Copy link

rdfriese commented Nov 2, 2022

Although I know Enums take to form of:

enum VecOrMap{
    Vec(Vec<usize>),
    Map(HashMap<String,usize>)
}

I sometimes find myself accidentally defining them as I would a Struct (playground):

enum VecOrMap{
    vec: Vec<usize>,
    map: HashMap<String,usize>
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
 --> src/lib.rs:2:8
  |
2 |     vec: Vec<usize>,
  |        ^ expected one of `(`, `,`, `=`, `{`, or `}`

error: expected item, found `:`
 --> src/lib.rs:2:8
  |
2 |     vec: Vec<usize>,
  |        ^ expected item

error: could not compile `playground` due to 2 previous errors

This always takes more than a few seconds to parse and then realize my mistake.
Is it possible to change this to something like:

It looks like you are trying to define an Enum using Struct syntax, change `vec: Vec<usize>` to `Vec(Vec<usize>)`
@rdfriese rdfriese 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 2, 2022
@BGR360
Copy link
Contributor

BGR360 commented Nov 6, 2022

Do you have any suggestion for how the compiler should distinguish between "trying to write an enum with struct syntax" and "trying to write a struct but used the enum keyword"?

@rdfriese
Copy link
Author

rdfriese commented Nov 8, 2022

Linking to the topic on internals where some other folks have left suggestions (https://internals.rust-lang.org/t/error-message-when-definining-enum-using-struct-syntax/17669).

Although none of those directly address your question.

My gut reaction is to assume that if the "enum" keyword is used that truly was the intention of the developer, but if it wasn't, hopefully some help message like the following:

error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
 --> src/lib.rs:2:8
  |
2 |     vec: Vec<usize>,
  |        ^ expected one of `(`, `,`, `=`, `{`, or `}`
  |
  | help: enum variants are either `Variant(Type)` or `Variant { field: Type }`

Would provide enough information to trigger an "Oh wait did I really want to use an enum here?"

@lyming2007
Copy link

@rustbot claim

@estebank estebank added A-parser Area: The parsing of Rust source code to an AST D-papercut Diagnostics: An error or lint that needs small tweaks. labels Nov 11, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 6, 2022
propagate the error from parsing enum variant to the parser and emit out

While parsing enum variant, the error message always disappear
Because the error message that emit out is from main error of parser
The information of enum variant disappears while parsing enum variant with error
We only check the syntax of expecting token, i.e, in case rust-lang#103869
It will error it without telling the message that this error is from pasring enum variant.
Propagate the sub-error from parsing enum variant to the main error of parser by chaining it with map_err
Check the sub-error before emitting the main error of parser and attach it.
Fix rust-lang#103869
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 6, 2022
propagate the error from parsing enum variant to the parser and emit out

While parsing enum variant, the error message always disappear
Because the error message that emit out is from main error of parser
The information of enum variant disappears while parsing enum variant with error
We only check the syntax of expecting token, i.e, in case rust-lang#103869
It will error it without telling the message that this error is from pasring enum variant.
Propagate the sub-error from parsing enum variant to the main error of parser by chaining it with map_err
Check the sub-error before emitting the main error of parser and attach it.
Fix rust-lang#103869
@bors bors closed this as completed in 0e19fb9 Dec 6, 2022
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 A-parser Area: The parsing of Rust source code to an AST D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

4 participants