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

"integral variable" error should suggest how to make literal a float #53280

Closed
sunjay opened this issue Aug 12, 2018 · 0 comments
Closed

"integral variable" error should suggest how to make literal a float #53280

sunjay opened this issue Aug 12, 2018 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@sunjay
Copy link
Member

sunjay commented Aug 12, 2018

The following code:

fn foo(x: f64) {}

fn main() {
    foo(10);
}

Produces the following error message:

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
4 |     foo(10);
  |         ^^ expected f64, found integral variable
  |
  = note: expected type `f64`
             found type `{integer}`

This error message tells you that the types are mismatched, but doesn't fully explain why. To fix it, you need to use a floating point literal instead of an integer literal. This isn't clear from the error message.

If you're new to programming or if you come from a language that doesn't draw the same distinction between integer and floating point literals, it may not be clear why the literal 10 is not the same as 10.0.

It would be great if we could add something in our error message that would suggest one or more of the valid ways to fix this:

  • 10.0
  • 10.
  • 10f64 - type could be f32 in another context
@sunjay sunjay changed the title "integral variable" should suggest how to make literal a float "integral variable" error should suggest how to make literal a float Aug 12, 2018
@killercup killercup added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 12, 2018
zackmdavis added a commit to zackmdavis/rust that referenced this issue Aug 12, 2018
Sunjay Varma pointed out that this is a nice thing that we could do.

Resolves rust-lang#53280.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Aug 12, 2018
…ation_device, r=estebank

wherein we suggest float for integer literals where a float was expected

@sunjay pointed out that this is a nice thing that we could do.

Resolves rust-lang#53280.

r? @estebank
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

No branches or pull requests

2 participants