Skip to content

Improve syntax diagnostics or suggestions with enum vs struct #75770

Closed
@rossmacarthur

Description

@rossmacarthur
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.

Activity

rossmacarthur

rossmacarthur commented on Aug 21, 2020

@rossmacarthur
ContributorAuthor

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

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
on Aug 21, 2020
added
A-parserArea: The lexing & parsing of Rust source code to an AST
D-papercutDiagnostics: An error or lint that needs small tweaks.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Aug 23, 2020
added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Mar 30, 2021
self-assigned this
on Mar 15, 2022
removed their assignment
on Oct 13, 2022
fmease

fmease commented on Feb 29, 2024

@fmease
Member

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

rossmacarthur commented on Mar 1, 2024

@rossmacarthur
ContributorAuthor

Looks reasonably fixed

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 lintsA-parserArea: The lexing & parsing of Rust source code to an ASTA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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

      Participants

      @estebank@fmease@rossmacarthur@JohnTitor@TaKO8Ki

      Issue actions

        Improve syntax diagnostics or suggestions with `enum` vs `struct` · Issue #75770 · rust-lang/rust