Skip to content

Misleading error message: Type mismatch when initializing struct fields with shorthand #52497

Closed
@mateon1

Description

@mateon1

The following code has a misleading proposed fix:

struct RGB { r: f64, g: f64, b: f64 }
fn main() {
  let (r, g, b): (f32, f32, f32) = (0., 0., 0.);
  let _ = RGB { r, g, b };
}

The reported error message is:

error[E0308]: mismatched types
 --> src/main.rs:4:17
  |
4 |   let _ = RGB { r, g, b };
  |                 ^ expected f64, found f32
help: you can cast an `f32` to `f64` in a lossless way
  |
4 |   let _ = RGB { r.into(), g, b };
  |                 ^^^^^^^^

Trying to apply the suggestion fails and causes a syntax error, the correct fix would be r: r.into().

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions