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

Confusing error message when using arithmetic operators on different number types #27604

Closed
wesleywiser opened this issue Aug 8, 2015 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@wesleywiser
Copy link
Member

fn main() {
    let x : f32 = 1.0;
    let y = x / 10; //error: the trait `core::ops::Div<_>` is not implemented for the type `f32`
}

The error message is confusing because it reads as though you cannot divide f32s at all when the real issue is that you can't divide a f32 by any other numeric type. The error message should at least say something about the different numeric types involved. Ideally it would suggest changing 10 to 10.0 or casting the term.

@therustmonk
Copy link
Contributor

This's because type system of Rust is strict, and there isn't generic typeclass similar Num in Haskell with rules to coercions. But it's not bad, because implicit bugs give large problems in system programming, statistics and medical computations.

fn main() {
    let x : f32 = 1.0;
    let _y = x / 10 as f32;
}

Maybe in the future releases Rust can recognize more complex relation cases like yours.

@wesleywiser
Copy link
Member Author

Hi, I think Rust's strict type system is awesome and I'm not complaining about that. I'm saying that the error message is bad because it doesn't tell the user what the actual problem is (which is that they are trying to perform operations on two different number types). I knew that Rust doesn't implicitly convert numbers but I was still briefly confused when I got this particular error from the compiler. I imagine a new user would be lost and would have to turn to StackOverflow or the user forms to figure out how to fix the error. Giving the user one of these errors instead would let them easily understand what's going on:

error: the trait `core::ops::Div<_>` is not implemented for the type `f32`
hint: you cannot perform math operations between the types `f32` and `i32`
hint: try converting the `i32` to an `f32`

or better yet

error: you cannot divide an `f32` by an `i32`
hint: try converting the `i32` to an `f32`

@arielb1
Copy link
Contributor

arielb1 commented Aug 12, 2015

@wesleywiser

The problem is that you are dealing with integer literal variables (they can be inferred into an integer of unknown signedness and size - u32,i32,u8,usize, etc.) and these display as _.

If you provided the literal's type, you would get an error like:

<anon>:3:13: 3:22 error: the trait `core::ops::Div<u32>` is not implemented for the type `f32` [E0277]
<anon>:3     let y = x / 10u32; //error: the trait `core::ops::Div<_>` is not implemented for the type `f32`

@wesleywiser
Copy link
Member Author

@arielb1 Thanks for the info! I just wanted to raise the issue because it seems like performing these kinds of operations on numbers is something a beginner is likely to do and if they try to do something like I did, the error message can be confusing.

@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 13, 2015
@mbrubeck
Copy link
Contributor

mbrubeck commented Feb 4, 2016

I think this is a duplicate of #24770.

@mbrubeck mbrubeck closed this as completed Feb 4, 2016
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

5 participants