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

Suggest .into() over try_into() when it would work #71051

Merged
merged 3 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,17 +753,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

match (&expected_ty.kind, &checked_ty.kind) {
(&ty::Int(ref exp), &ty::Int(ref found)) => {
let is_fallible = match (found.bit_width(), exp.bit_width()) {
(Some(found), Some(exp)) if found > exp => true,
let is_fallible = match (exp.bit_width(), found.bit_width()) {
(Some(exp), Some(found)) if exp < found => true,
(None, Some(8 | 16)) => false,
(None, _) | (_, None) => true,
_ => false,
};
suggest_to_change_suffix_or_into(err, is_fallible);
true
}
(&ty::Uint(ref exp), &ty::Uint(ref found)) => {
let is_fallible = match (found.bit_width(), exp.bit_width()) {
(Some(found), Some(exp)) if found > exp => true,
let is_fallible = match (exp.bit_width(), found.bit_width()) {
(Some(exp), Some(found)) if exp < found => true,
(None, Some(8 | 16)) => false,
(None, _) | (_, None) => true,
_ => false,
};
Expand Down
58 changes: 58 additions & 0 deletions src/test/ui/integer-literal-suffix-inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn main() {
fn id_i16(n: i16) -> i16 { n }
fn id_i32(n: i32) -> i32 { n }
fn id_i64(n: i64) -> i64 { n }
fn id_isize(n: isize) -> isize { n }

// the smallest values that need these types
let b8: u8 = 16;
Expand All @@ -27,6 +28,11 @@ fn main() {
fn id_u16(n: u16) -> u16 { n }
fn id_u32(n: u32) -> u32 { n }
fn id_u64(n: u64) -> u64 { n }
fn id_usize(n: usize) -> usize { n }

// Values for testing *size
let asize: isize = 1;
let bsize: usize = 3;

id_i8(a8); // ok
id_i8(a16);
Expand All @@ -38,6 +44,9 @@ fn main() {
id_i8(a64);
//~^ ERROR mismatched types
//~| expected `i8`, found `i64`
id_i8(asize);
//~^ ERROR mismatched types
//~| expected `i8`, found `isize`

id_i16(a8);
//~^ ERROR mismatched types
Expand All @@ -49,6 +58,9 @@ fn main() {
id_i16(a64);
//~^ ERROR mismatched types
//~| expected `i16`, found `i64`
id_i16(asize);
//~^ ERROR mismatched types
//~| expected `i16`, found `isize`

id_i32(a8);
//~^ ERROR mismatched types
Expand All @@ -60,6 +72,9 @@ fn main() {
id_i32(a64);
//~^ ERROR mismatched types
//~| expected `i32`, found `i64`
id_i32(asize);
//~^ ERROR mismatched types
//~| expected `i32`, found `isize`

id_i64(a8);
//~^ ERROR mismatched types
Expand All @@ -71,6 +86,23 @@ fn main() {
//~^ ERROR mismatched types
//~| expected `i64`, found `i32`
id_i64(a64); // ok
id_i64(asize);
//~^ ERROR mismatched types
//~| expected `i64`, found `isize`

id_isize(a8);
//~^ ERROR mismatched types
//~| expected `isize`, found `i8`
id_isize(a16);
//~^ ERROR mismatched types
//~| expected `isize`, found `i16`
id_isize(a32);
//~^ ERROR mismatched types
//~| expected `isize`, found `i32`
id_isize(a64);
//~^ ERROR mismatched types
//~| expected `isize`, found `i64`
id_isize(asize); //ok

id_i8(c8); // ok
id_i8(c16);
Expand Down Expand Up @@ -126,6 +158,9 @@ fn main() {
id_u8(b64);
//~^ ERROR mismatched types
//~| expected `u8`, found `u64`
id_u8(bsize);
//~^ ERROR mismatched types
//~| expected `u8`, found `usize`

id_u16(b8);
//~^ ERROR mismatched types
Expand All @@ -137,6 +172,9 @@ fn main() {
id_u16(b64);
//~^ ERROR mismatched types
//~| expected `u16`, found `u64`
id_u16(bsize);
//~^ ERROR mismatched types
//~| expected `u16`, found `usize`

id_u32(b8);
//~^ ERROR mismatched types
Expand All @@ -148,6 +186,9 @@ fn main() {
id_u32(b64);
//~^ ERROR mismatched types
//~| expected `u32`, found `u64`
id_u32(bsize);
//~^ ERROR mismatched types
//~| expected `u32`, found `usize`

id_u64(b8);
//~^ ERROR mismatched types
Expand All @@ -159,4 +200,21 @@ fn main() {
//~^ ERROR mismatched types
//~| expected `u64`, found `u32`
id_u64(b64); // ok
id_u64(bsize);
//~^ ERROR mismatched types
//~| expected `u64`, found `usize`

id_usize(b8);
//~^ ERROR mismatched types
//~| expected `usize`, found `u8`
id_usize(b16);
//~^ ERROR mismatched types
//~| expected `usize`, found `u16`
id_usize(b32);
//~^ ERROR mismatched types
//~| expected `usize`, found `u32`
id_usize(b64);
//~^ ERROR mismatched types
//~| expected `usize`, found `u64`
id_usize(bsize); //ok
}
Loading