Skip to content

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

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

Closed
mateon1 opened this issue Jul 18, 2018 · 0 comments
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug.

Comments

@mateon1
Copy link

mateon1 commented Jul 18, 2018

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().

@estebank estebank added C-bug Category: This is a bug. 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 21, 2019
…ld, r=matthewjasper

Suggest correct cast for struct fields with shorthand syntax

```
error[E0308]: mismatched types
  --> $DIR/type-mismatch-struct-field-shorthand.rs:8:19
   |
LL |     let _ = RGB { r, g, b };
   |                   ^ expected f64, found f32
help: you can cast an `f32` to `f64` in a lossless way
   |
LL |     let _ = RGB { r: r.into(), g, b };
   |                   ^^^^^^^^^^^
```

Fix rust-lang#52497.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants