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

Misleading error message in case of ambiguous if blocks #51311

Closed
vandenheuvel opened this issue Jun 2, 2018 · 1 comment · Fixed by #59981
Closed

Misleading error message in case of ambiguous if blocks #51311

vandenheuvel opened this issue Jun 2, 2018 · 1 comment · Fixed by #59981
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@vandenheuvel
Copy link
Contributor

For the following code, the compiler generates error E0423 on both stable and nightly in the playground:

fn main() {
    let x = MyType { y: 1, };
    
    // Fails
    if x == MyType { y: 1, } {
    }
    // Is ok
    if x == (MyType { y: 1, }) {
    }
}

#[derive(Eq, PartialEq)]
struct MyType {
    y: u64,
}
error: expected type, found `1`
 --> src/main.rs:5:25
  |
5 |     if x == MyType { y: 1, } {
  |                         ^ expecting a type here because of type ascription

error[E0423]: expected value, found struct `MyType`
 --> src/main.rs:5:13
  |
5 |     if x == MyType { y: 1, } {
  |             ^^^^^^ did you mean `MyType { /* fields */ }`?

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0423`.

I learned that there is a parsing ambiguity in the if-statement which fails to compile. But rather than producing an error message explaining this ambiguity, the compiler seems to assume one of the possibilities ("the first accolade belongs to the if-statement") and proceeds, encounting the error E0423 which it then displays.

It seems preferable for the compiler to stop at the point of encounting the ambiguity, with an error message explaining the problem.

@PramodBisht PramodBisht added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 4, 2018
@estebank
Copy link
Contributor

estebank commented Jun 8, 2018

cc #51360

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants