Skip to content

Incorrect/misleading error messages after syntax errors in struct initializers #52496

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
mateon1 opened this issue Jul 18, 2018 · 0 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`

Comments

@mateon1
Copy link

mateon1 commented Jul 18, 2018

struct Foo { bar: f64, baz: i64 }
fn main() {
   let _ = Foo { bar: .5, baz: 42 };
   let bar = 1.5f32;
   let _ = Foo { bar.into(), baz: -1 };
}

The first struct reports missing fields, bar and baz due to the syntax error:

error: expected expression, found `.`
 --> src/main.rs:3:23
  |
3 |    let _ = Foo { bar: .5, baz: 42 };
  |            ---        ^ expected expression
  |            |
  |            while parsing this struct

error[E0063]: missing fields `bar`, `baz` in initializer of `Foo`
 --> src/main.rs:3:12
  |
3 |    let _ = Foo { bar: .5, baz: 42 };
  |            ^^^ missing `bar`, `baz`

The second struct reports a missing field baz, and suggests an obviously wrong fix for a type mismatch error:

error: expected one of `,` or `}`, found `.`
 --> src/main.rs:5:21
  |
5 |    let _ = Foo { bar.into(), baz: -1 };
  |                     ^ expected one of `,` or `}` here

error[E0308]: mismatched types
 --> src/main.rs:5:18
  |
5 |    let _ = Foo { bar.into(), baz: -1 };
  |                  ^^^ expected f64, found f32
help: you can cast an `f32` to `f64` in a lossless way
  |
5 |    let _ = Foo { bar.into().into(), baz: -1 };
  |                  ^^^^^^^^^^

error[E0063]: missing field `baz` in initializer of `Foo`
 --> src/main.rs:5:12
  |
5 |    let _ = Foo { bar.into(), baz: -1 };
  |            ^^^ missing `baz`
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Jan 8, 2019
Centril added a commit to Centril/rust that referenced this issue Jan 23, 2019
…avidtwco

Recover from parse errors in literal struct fields and incorrect float literals

Fix rust-lang#52496.
@bors bors closed this as completed in 2dd63a2 Jan 24, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`
Projects
None yet
Development

No branches or pull requests

2 participants