Skip to content

Commit

Permalink
Rollup merge of #71617 - samrat:suggest-int-into, r=ecstatic-morse
Browse files Browse the repository at this point in the history
Suggest `into` instead of `try_into` if possible with int types

If it is possible to convert an integer type into another using `into`, don't suggest `try_into`. This commit changes the suggested method to convert from one integer type to another for the following cases:

- u{n} -> i{m} where n < m
- u8 -> isize
- i{n} -> isize where n <= 16
- u{n} -> usize where n <= 16

Fixes #71580
  • Loading branch information
Dylan-DPC authored Apr 29, 2020
2 parents 843ffb8 + a6033e3 commit e3bf870
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 64 deletions.
11 changes: 10 additions & 1 deletion src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
suggest_to_change_suffix_or_into(err, is_fallible);
true
}
(&ty::Int(_), &ty::Uint(_)) | (&ty::Uint(_), &ty::Int(_)) => {
(&ty::Int(exp), &ty::Uint(found)) => {
let is_fallible = match (exp.bit_width(), found.bit_width()) {
(Some(exp), Some(found)) if found < exp => false,
(None, Some(8)) => false,
_ => true,
};
suggest_to_change_suffix_or_into(err, is_fallible);
true
}
(&ty::Uint(_), &ty::Int(_)) => {
suggest_to_change_suffix_or_into(err, true);
true
}
Expand Down
22 changes: 8 additions & 14 deletions src/test/ui/numeric/numeric-cast-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,21 @@ error[E0308]: mismatched types
--> $DIR/numeric-cast-2.rs:7:18
|
LL | let y: i64 = x + x;
| --- ^^^^^ expected `i64`, found `u16`
| |
| --- ^^^^^
| | |
| | expected `i64`, found `u16`
| | help: you can convert an `u16` to `i64`: `(x + x).into()`
| expected due to this
|
help: you can convert an `u16` to `i64` and panic if the converted value wouldn't fit
|
LL | let y: i64 = (x + x).try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
--> $DIR/numeric-cast-2.rs:9:18
|
LL | let z: i32 = x + x;
| --- ^^^^^ expected `i32`, found `u16`
| |
| --- ^^^^^
| | |
| | expected `i32`, found `u16`
| | help: you can convert an `u16` to `i32`: `(x + x).into()`
| expected due to this
|
help: you can convert an `u16` to `i32` and panic if the converted value wouldn't fit
|
LL | let z: i32 = (x + x).try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

Expand Down
14 changes: 7 additions & 7 deletions src/test/ui/numeric/numeric-cast.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() {
//~^ ERROR mismatched types
foo::<isize>(x_u16.try_into().unwrap());
//~^ ERROR mismatched types
foo::<isize>(x_u8.try_into().unwrap());
foo::<isize>(x_u8.into());
//~^ ERROR mismatched types
foo::<isize>(x_isize);
foo::<isize>(x_i64.try_into().unwrap());
Expand Down Expand Up @@ -89,11 +89,11 @@ fn main() {
//~^ ERROR mismatched types
foo::<i64>(x_u64.try_into().unwrap());
//~^ ERROR mismatched types
foo::<i64>(x_u32.try_into().unwrap());
foo::<i64>(x_u32.into());
//~^ ERROR mismatched types
foo::<i64>(x_u16.try_into().unwrap());
foo::<i64>(x_u16.into());
//~^ ERROR mismatched types
foo::<i64>(x_u8.try_into().unwrap());
foo::<i64>(x_u8.into());
//~^ ERROR mismatched types
foo::<i64>(x_isize.try_into().unwrap());
//~^ ERROR mismatched types
Expand Down Expand Up @@ -135,9 +135,9 @@ fn main() {
//~^ ERROR mismatched types
foo::<i32>(x_u32.try_into().unwrap());
//~^ ERROR mismatched types
foo::<i32>(x_u16.try_into().unwrap());
foo::<i32>(x_u16.into());
//~^ ERROR mismatched types
foo::<i32>(x_u8.try_into().unwrap());
foo::<i32>(x_u8.into());
//~^ ERROR mismatched types
foo::<i32>(x_isize.try_into().unwrap());
//~^ ERROR mismatched types
Expand Down Expand Up @@ -181,7 +181,7 @@ fn main() {
//~^ ERROR mismatched types
foo::<i16>(x_u16.try_into().unwrap());
//~^ ERROR mismatched types
foo::<i16>(x_u8.try_into().unwrap());
foo::<i16>(x_u8.into());
//~^ ERROR mismatched types
foo::<i16>(x_isize.try_into().unwrap());
//~^ ERROR mismatched types
Expand Down
70 changes: 28 additions & 42 deletions src/test/ui/numeric/numeric-cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,10 @@ error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:52:18
|
LL | foo::<isize>(x_u8);
| ^^^^ expected `isize`, found `u8`
|
help: you can convert an `u8` to `isize` and panic if the converted value wouldn't fit
|
LL | foo::<isize>(x_u8.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^
| |
| expected `isize`, found `u8`
| help: you can convert an `u8` to `isize`: `x_u8.into()`

error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:55:18
Expand Down Expand Up @@ -307,34 +305,28 @@ error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:92:16
|
LL | foo::<i64>(x_u32);
| ^^^^^ expected `i64`, found `u32`
|
help: you can convert an `u32` to `i64` and panic if the converted value wouldn't fit
|
LL | foo::<i64>(x_u32.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^
| |
| expected `i64`, found `u32`
| help: you can convert an `u32` to `i64`: `x_u32.into()`

error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:94:16
|
LL | foo::<i64>(x_u16);
| ^^^^^ expected `i64`, found `u16`
|
help: you can convert an `u16` to `i64` and panic if the converted value wouldn't fit
|
LL | foo::<i64>(x_u16.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^
| |
| expected `i64`, found `u16`
| help: you can convert an `u16` to `i64`: `x_u16.into()`

error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:96:16
|
LL | foo::<i64>(x_u8);
| ^^^^ expected `i64`, found `u8`
|
help: you can convert an `u8` to `i64` and panic if the converted value wouldn't fit
|
LL | foo::<i64>(x_u8.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^
| |
| expected `i64`, found `u8`
| help: you can convert an `u8` to `i64`: `x_u8.into()`

error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:98:16
Expand Down Expand Up @@ -506,23 +498,19 @@ error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:138:16
|
LL | foo::<i32>(x_u16);
| ^^^^^ expected `i32`, found `u16`
|
help: you can convert an `u16` to `i32` and panic if the converted value wouldn't fit
|
LL | foo::<i32>(x_u16.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^
| |
| expected `i32`, found `u16`
| help: you can convert an `u16` to `i32`: `x_u16.into()`

error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:140:16
|
LL | foo::<i32>(x_u8);
| ^^^^ expected `i32`, found `u8`
|
help: you can convert an `u8` to `i32` and panic if the converted value wouldn't fit
|
LL | foo::<i32>(x_u8.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^
| |
| expected `i32`, found `u8`
| help: you can convert an `u8` to `i32`: `x_u8.into()`

error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:142:16
Expand Down Expand Up @@ -709,12 +697,10 @@ error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:184:16
|
LL | foo::<i16>(x_u8);
| ^^^^ expected `i16`, found `u8`
|
help: you can convert an `u8` to `i16` and panic if the converted value wouldn't fit
|
LL | foo::<i16>(x_u8.try_into().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^
| |
| expected `i16`, found `u8`
| help: you can convert an `u8` to `i16`: `x_u8.into()`

error[E0308]: mismatched types
--> $DIR/numeric-cast.rs:186:16
Expand Down

0 comments on commit e3bf870

Please sign in to comment.