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

Improve syntax diagnostics or suggestions with enum vs struct #75770

Closed
rossmacarthur opened this issue Aug 21, 2020 · 3 comments
Closed

Improve syntax diagnostics or suggestions with enum vs struct #75770

rossmacarthur opened this issue Aug 21, 2020 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

@rossmacarthur
Copy link
Contributor

I changed an enum to a struct but forgot to change the enum keyword to a struct keyword, it took me a while to realize what the problem was because the error is unclear.

enum used where code looks like a struct

pub enum NotEnum {
    field: u8   
}

The error is

error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
 --> src/lib.rs:2:10
  |
2 |     field: u8,    
  |          ^ expected one of `(`, `,`, `=`, `{`, or `}`

error: expected item, found `:`
 --> src/lib.rs:2:10
  |
2 |     field: u8,    
  |          ^ expected item

A suggestion saying help: change `enum` to `struct` would be useful.

struct used where code looks like an enum

pub struct NotStruct {
    Variant
}
error: expected `:`, found `}`
 --> src/lib.rs:3:1
  |
2 |     Variant
  |            - expected `:`
3 | }
  | ^ unexpected token

A suggestion saying help: change `struct` to `enum` would be useful.

@rossmacarthur
Copy link
Contributor Author

@rustbot modify labels: +A-diagnostics +A-suggestion-diagnostics

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Aug 21, 2020
@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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 23, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Mar 30, 2021
@TaKO8Ki TaKO8Ki self-assigned this Mar 15, 2022
@TaKO8Ki TaKO8Ki removed their assignment Oct 13, 2022
@fmease
Copy link
Member

fmease commented Feb 29, 2024

Current output for the first snippet:

error: expected one of `(`, `,`, `=`, `{`, or `}`, found `:`
 --> src/lib.rs:2:10
  |
1 | pub enum NotEnum {
  |          ------- while parsing this enum
2 |     field: u8   
  |          ^ expected one of `(`, `,`, `=`, `{`, or `}`
  |
  = help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
help: perhaps you meant to use `struct` here
  |
1 | pub struct NotEnum {
  |     ~~~~~~

→ Clearly fixed.

Current output for the second snippet:

error: expected `:`, found `}`
 --> src/lib.rs:3:1
  |
1 | pub struct NotStruct {
  |            --------- while parsing this struct
2 |     Variant
  |            - expected `:`
3 | }
  | ^ unexpected token

→ Slightly improved (due to the label).

@rossmacarthur
Copy link
Contributor Author

Looks reasonably fixed

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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. 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.

6 participants