Skip to content

Struct-style enum constructor gets misparsed as type ascription without suggestion #59962

Closed
@RalfJung

Description

@RalfJung

The following program

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
enum E {
    V { field: bool }
}
fn test_E(x: E) {
    if x == E::V { field: false } {}
}

shows the error

error: expected identifier, found keyword `false`
 --> src/lib.rs:6:27
  |
6 |     if x == E::V { field: false } {}
  |                           ^^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
  |
6 |     if x == E::V { field: r#false } {}
  |                           ^^^^^^^

error: expected type, found keyword `false`
 --> src/lib.rs:6:27
  |
6 |     if x == E::V { field: false } {}
  |                           ^^^^^ expecting a type here because of type ascription

error[E0423]: expected value, found struct variant `E::V`
 --> src/lib.rs:6:13
  |
6 |     if x == E::V { field: false } {}
  |             ^^^^ did you mean `E::V { /* fields */ }`?

error: aborting due to 3 previous errors

This is not very helpful: If I replace E::V by a struct constructor, it suggests adding parentheses, which is the real solution (the error is still not great, but better than this). Instead "did you mean E::V" is leading in the totally wrong direction.

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