Skip to content

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

@mateon1

Description

@mateon1
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`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions