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

Prefer T::from over .try_into().unwrap() in error messages #73145

Closed
leonardo-m opened this issue Jun 8, 2020 · 0 comments · Fixed by #73195
Closed

Prefer T::from over .try_into().unwrap() in error messages #73145

leonardo-m opened this issue Jun 8, 2020 · 0 comments · Fixed by #73195
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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.

Comments

@leonardo-m
Copy link

This wrong code:

fn foo(x: u8, y: u32) -> bool {
    x > y
}
fn main() {}

Currently gives:

error[E0308]: mismatched types
 --> ...\test.rs:2:9
  |
2 |     x > y
  |         ^ expected `u8`, found `u32`
  |
help: you can convert an `u32` to `u8` and panic if the converted value wouldn't fit
  |
2 |     x > y.try_into().unwrap()
  |         ^^^^^^^^^^^^^^^^^^^^^

But rust could give a little smarter error message:

error[E0308]: mismatched types
 --> ...\test.rs:2:5
  |
2 |     x > y
  |     ^ expected `u32`, found `u8`
  |
help: you can convert an `u8` to `u32`: `u32::from(x)`
  |
2 |     u32::from(x) > y
  |     ^^^^^^^^^^^^
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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. labels Jun 9, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 11, 2020
Provide suggestion to convert numeric op LHS rather than unwrapping RHS

Given a code

```rust
fn foo(x: u8, y: u32) -> bool {
    x > y
}
fn main() {}
```

it could be more helpful to provide a suggestion to do "u32::from(x)"
rather than "y.try_into().unwrap()", since the latter may panic.

We do this by passing the LHS of a binary expression up the stack into
the coercion checker.

Closes rust-lang#73145
@bors bors closed this as completed in e243f62 Jun 12, 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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants