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

Compiler suggests unnecessary try into #63697

Closed
lopopolo opened this issue Aug 19, 2019 · 9 comments · Fixed by #63813
Closed

Compiler suggests unnecessary try into #63697

lopopolo opened this issue Aug 19, 2019 · 9 comments · Fixed by #63813
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lopopolo
Copy link
Contributor

lopopolo commented Aug 19, 2019

I updated a dependency and the type of a struct field changed from an i32 to a u16.

I am assigning a u16 value to this struct field. Before, I was calling i32::from to convert the u16 to an i32. With the upgrade this is unnecessary since the field and the value are both u16.

The rust compiler suggests this fix:

error[E0308]: mismatched types
  --> artichoke-frontend/src/parser.rs:90:37
   |
90 |             (*self.parser).lineno = i32::from((*self.context).lineno);
   |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u16, found i32
help: you can convert an `i32` to `u16` and panic if the converted value wouldn't fit
   |
90 |             (*self.parser).lineno = i32::from((*self.context).lineno).try_into().unwrap();
   |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: Could not compile `artichoke-frontend`.
warning: build failed, waiting for other jobs to finish...
error: build failed

But it should really suggest removing the i32::from and using the u16 directly.

@lopopolo
Copy link
Contributor Author

I am using Rust 1.37.0.

@jonas-schievink jonas-schievink added 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 19, 2019
@Centril Centril added C-bug Category: This is a bug. A-diagnostics Area: Messages for errors, warnings, and lints labels Aug 19, 2019
@Centril
Copy link
Contributor

Centril commented Aug 19, 2019

cc @estebank

@lopopolo
Copy link
Contributor Author

Similarly, this code should suggest replacing i32::from with u16::from.

#[derive(Debug)]
struct Member(u16);

pub fn main() {
    let m = Member(i32::from(0_u8));
    println!("{:?}", m);
}
   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:5:20
  |
5 |     let m = Member(i32::from(0_u8));
  |                    ^^^^^^^^^^^^^^^ expected u16, found i32
help: you can convert an `i32` to `u16` and panic if the converted value wouldn't fit
  |
5 |     let m = Member(i32::from(0_u8).try_into().unwrap());
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

playground

@estebank
Copy link
Contributor

For the case where we have i32::from(0_i16) and an i16 was expected, what should be the correct behavior? I believe simply suggesting i16::from(0_i16) is appropriate, if funny looking, because the 0_i16 could be coming from outside a macro, and the error be inside of one. Thoughts?

@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-medium Medium priority labels Aug 19, 2019
@lopopolo
Copy link
Contributor Author

@estebank that sounds good to me 👍

@awaitlink
Copy link
Contributor

@rustbot claim

@rustbot rustbot self-assigned this Aug 22, 2019
@awaitlink
Copy link
Contributor

awaitlink commented Aug 22, 2019

@estebank Which of these options makes more sense?


Check if the new (suggested) conversion is infallible

  • always (doesn't make sense in the 1st case?)
  • only in the 2nd case (if the new conversion is fallible, I presume we fall back to other suggestions and .try_from().unwrap() should be suggested, which makes a bit of sense, since it highlights the possible panic?)
  • never (if SomeNumberTy::from(...) is detected, always suggest to replace with ExpectedTy::from(...))

1. Seems like the user already opted in for panicking if the conversion fails, so conversion was fallible and will stay so:

struct A(i16);

fn main() {
    let _ = A(i32::from(0_i64));
}

2. The conversion is infallible but will become fallible if the suggestion is applied:

struct A(i16);

fn main() {
    let _ = A(i128::from(0_i64));
}

@awaitlink
Copy link
Contributor

@rustbot release-assignment

@rustbot rustbot removed their assignment Aug 22, 2019
@estebank
Copy link
Contributor

@u32i64 sorry, didn't see your claim before I pushed my PR :-|

Centril added a commit to Centril/rust that referenced this issue Aug 24, 2019
Do not suggest `.try_into()` on `i32::from(x)`

Fix rust-lang#63697.
Centril added a commit to Centril/rust that referenced this issue Aug 24, 2019
Do not suggest `.try_into()` on `i32::from(x)`

Fix rust-lang#63697.
@bors bors closed this as completed in ed8e13c Aug 25, 2019
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-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-medium Medium priority 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.

6 participants