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

Suboptimal hint about converting numeric types: Suggests try_into().unwrap() when into() is possible. #71580

Closed
steffahn opened this issue Apr 26, 2020 · 5 comments · Fixed by #71617
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented Apr 26, 2020

fn main() {
    let a = 1u8;
    let _: i64 = a;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:3:18
  |
3 |     let _: i64 = a;
  |            ---   ^ expected `i64`, found `u8`
  |            |
  |            expected due to this
  |
help: you can convert an `u8` to `i64` and panic if the converted value wouldn't fit
  |
3 |     let _: i64 = a.try_into().unwrap();
  |                  ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

I would’ve expected something like:

help: you can convert an `u8` to `i64`
  |
3 |     let _: i64 = a.into();
  |                  ^^^^^^^^

Edit: What actually should happen is the same as currently already with _: u64, i.e. it would look like

error[E0308]: mismatched types
 --> src/main.rs:3:18
  |
3 |     let _: i64 = a;
  |            ---   ^
  |            |     |
  |            |     expected `i64`, found `u8`
  |            |     help: you can convert an `u8` to `i64`: `a.into()`
  |            expected due to this

@rustbot modify labels: A-diagnostics, T-compiler, D-papercut, C-enhancement, E-easy

This issue has been assigned to @samrat via this comment.

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Apr 26, 2020
@steffahn steffahn changed the title Suboptimal hint about converting numeric types: Suggests try_into when into is possible. Suboptimal hint about converting numeric types: Suggests try_into().unwrap() when into() is possible. Apr 26, 2020
@steffahn steffahn reopened this Apr 26, 2020
@steffahn
Copy link
Member Author

Okay, lots of cases that already work got me confused into thinking this was fixed in nightly for a moment.

What currently already works is the hint about converting

  • u𝑛u𝑚 where 𝑛<𝑚
  • i𝑛i𝑚 where 𝑛<𝑚

What currently suggest try_into().unwrap() even though into() works

  • u𝑛i𝑚 where 𝑛<𝑚
  • u8isize
  • i𝑛isize where 𝑛≤16
  • u𝑛usize where 𝑛≤16

@jonas-schievink
Copy link
Contributor

I reported a similar issue not long ago. Maybe it would be more robust and also more general to rewrite this suggestion code to actually look for the impl in question instead of having just a hard-coded list?

@samrat
Copy link
Contributor

samrat commented Apr 27, 2020

@rustbot claim

@samrat
Copy link
Contributor

samrat commented Apr 28, 2020

I've created a PR augmenting the existing code to change the suggestions for the cases listed.

However, I would like to explore the approach @jonas-schievink suggested(perhaps as part of a separate PR?). Any advice on how I could get the code to look for the impl so it can make the appropriate suggestion? Is the trait_selection crate the right place to be looking at for this?

@jonas-schievink
Copy link
Contributor

Is the trait_selection crate the right place to be looking at for this?

Yeah. I believe you'll need to create an InferCtxt and SelectionContext and then call SelectionContext::select, but there might be other/better ways to do this. You might want to ask for advice on Zulip.

@bors bors closed this as completed in e3bf870 Apr 29, 2020
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants