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

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

Closed
RalfJung opened this issue Apr 14, 2019 · 0 comments · Fixed by #59981
Closed

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

RalfJung opened this issue Apr 14, 2019 · 0 comments · Fixed by #59981
Labels
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.

Comments

@RalfJung
Copy link
Member

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.

@jonas-schievink jonas-schievink 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 Apr 14, 2019
Centril added a commit to Centril/rust that referenced this issue Apr 18, 2019
…ochenkov

Emit specific error for struct literal in conditions

Fix rust-lang#59962, fix rust-lang#51311.
bors added a commit that referenced this issue Apr 19, 2019
Emit specific error for struct literal in conditions

Fix #59962, fix #51311.
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 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.

2 participants