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

Provide suggestion when trying to use method on numeric literal #47171

Merged
merged 2 commits into from
Jan 8, 2018

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jan 4, 2018

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.

@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@estebank estebank force-pushed the numeric-literal-suggestion branch from 96bca24 to 87242f3 Compare January 4, 2018 05:47
@leonardo-m
Copy link

The help message is not good. The number of "as" cast should be minimized in good Rust code. So instead of this:

let x = (2.0 as f32).powi(2);

It's better to suggest something like:

let x = 2.0_f32.powi(2);

@alexcrichton alexcrichton added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 4, 2018
Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code seems fine; I agree that 2.0_f32 is probably a better suggestion though.

@estebank
Copy link
Contributor Author

estebank commented Jan 4, 2018

This error can also be triggered by

fn main() {
    let y = 2.0;
    let x = y.powi(2);
    println!("{:?}", x);
}

Granted, in that case the suggestion let y: f32 = 2.0; would be more appropriate. I'll have to take a look at it.

@estebank
Copy link
Contributor Author

estebank commented Jan 4, 2018

Both literals and local bindings are now handled correctly.

@estebank estebank force-pushed the numeric-literal-suggestion branch from 19e6a5e to 4cd6c04 Compare January 4, 2018 20:27
@estebank estebank force-pushed the numeric-literal-suggestion branch from 4cd6c04 to f7aed3e Compare January 4, 2018 23:28
@estebank
Copy link
Contributor Author

estebank commented Jan 5, 2018

@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented Jan 5, 2018

📌 Commit f7aed3e has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented Jan 7, 2018

⌛ Testing commit f7aed3e with merge b950587...

bors added a commit that referenced this pull request Jan 7, 2018
…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.
@bors
Copy link
Contributor

bors commented Jan 7, 2018

💔 Test failed - status-appveyor

@kennytm
Copy link
Member

kennytm commented Jan 7, 2018

@bors retry #46903

@bors
Copy link
Contributor

bors commented Jan 7, 2018

⌛ Testing commit f7aed3e with merge 9c663205a2b984f635c577d17a5729c9e1754843...

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 7, 2018
@bors
Copy link
Contributor

bors commented Jan 7, 2018

💔 Test failed - status-appveyor

@estebank
Copy link
Contributor Author

estebank commented Jan 7, 2018

@bors retry

@bors
Copy link
Contributor

bors commented Jan 7, 2018

⌛ Testing commit f7aed3e with merge a29461f...

bors added a commit that referenced this pull request Jan 7, 2018
…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.
@bors
Copy link
Contributor

bors commented Jan 8, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing a29461f to master...

@bors bors merged commit f7aed3e into rust-lang:master Jan 8, 2018
@estebank estebank deleted the numeric-literal-suggestion branch November 9, 2023 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants