-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Diagnostics for ambiguous number types could be better #40985
Comments
Method calls on an ambiguous number typeIn the following code add a check to see if rust/src/librustc_typeck/check/method/suggest.rs Lines 177 to 222 in 442b7bd
Casting an ambiguous number typeOn its own,
If the help where to be expanded, it could be done in the following code in the same way as above: rust/src/librustc_typeck/check/cast.rs Lines 233 to 241 in 442b7bd
Update: output in nightly no longer shows the second error:
|
Glad I found this issue, I was quite confused by the reference to the core Float trait, and the rust manual saying f32 powi() was stable since 1.0.0 |
…sakis Provide suggestion when trying to use method on numeric literal New output: ``` error[E0688]: can't call method `powi` on ambiguous numeric type `{float}` --> $DIR/method-on-ambiguous-numeric-type.rs:12:17 | 12 | let x = 2.0.powi(2); | ^^^^ help: you must specify a concrete type for this numeric value, like `f32` | 12 | let x = 2.0_f32.powi(2); | ^^^^^^^ ``` Previous output: ``` error[E0599]: no method named `powi` found for type `{float}` in the current scope --> src/main.rs:12:17 | 12 | let x = 2.0.powi(2); | ^^^^ | = help: items from traits can only be used if the trait is in scope help: the following trait is implemented but not in scope, perhaps add a `use` for it: | 11 | use core::num::Float; | ``` Fix #40985.
…sakis Provide suggestion when trying to use method on numeric literal New output: ``` error[E0688]: can't call method `powi` on ambiguous numeric type `{float}` --> $DIR/method-on-ambiguous-numeric-type.rs:12:17 | 12 | let x = 2.0.powi(2); | ^^^^ help: you must specify a concrete type for this numeric value, like `f32` | 12 | let x = 2.0_f32.powi(2); | ^^^^^^^ ``` Previous output: ``` error[E0599]: no method named `powi` found for type `{float}` in the current scope --> src/main.rs:12:17 | 12 | let x = 2.0.powi(2); | ^^^^ | = help: items from traits can only be used if the trait is in scope help: the following trait is implemented but not in scope, perhaps add a `use` for it: | 11 | use core::num::Float; | ``` Fix #40985.
Ambiguous integer or float type variable can lead to some rather confusing error messages. Consider:
Method calls on an ambiguous number type
The first error says that
{float}
doesn't have a method calledpowi
, but taken naively that's not true: bothf32
andf64
have methodspowi
. The actual problem is that the type inference doesn't know which floating point type the user wants.The
help
is even more confusing, because it suggests the user to use a deprecated trait that's not even documented anymore.Casting an ambiguous number type
Likewise, when casting one could again run into the same problem. Here, it complains that the cast is "non-scalar" but naively
{float}
sounds like a perfectly scalar type! In reality the problem is that casting requires the source type to be non-ambiguous.Suggestion
Tentative PR: master...Rufflewind:ambiguous-number-type
Tweak the error messages a bit when the type is
{float}
or{integer}
. Also, perhaps suggestions for deprecated traits could be suppressed?The text was updated successfully, but these errors were encountered: