diff options
| author | Yashhwanth Ram <ryr397@gmail.com> | 2020-04-12 10:02:02 +0530 |
|---|---|---|
| committer | Yashhwanth Ram <ryr397@gmail.com> | 2020-04-12 12:14:42 +0530 |
| commit | 55464ebc8372cb2b489aaee84c324f5a6f6ba58d (patch) | |
| tree | f0d79940dcae80720acbf7b82d2e393bb55ac08b | |
| parent | b3c9912dbad13d7a3c369df4913e36d7fd7315ad (diff) | |
| download | rust-55464ebc8372cb2b489aaee84c324f5a6f6ba58d.tar.gz rust-55464ebc8372cb2b489aaee84c324f5a6f6ba58d.zip | |
Fix order of comparison and remove incorrect case for ints in typeck/demand.rs
Add tests for *size
| -rw-r--r-- | src/librustc_typeck/check/demand.rs | 12 | ||||
| -rw-r--r-- | src/test/ui/integer-literal-suffix-inference.rs | 58 |
2 files changed, 64 insertions, 6 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 7ef4bfb0141..2f890d4dabb 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -753,9 +753,9 @@ 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, - (None, Some(8 | 16)) | (Some(8 | 16), None) => false, + 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, }; @@ -763,9 +763,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { 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, - (None, Some(8 | 16)) | (Some(8 | 16), None) => false, + 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, }; diff --git a/src/test/ui/integer-literal-suffix-inference.rs b/src/test/ui/integer-literal-suffix-inference.rs index 3f4bedc4c22..c320f2bb7b4 100644 --- a/src/test/ui/integer-literal-suffix-inference.rs +++ b/src/test/ui/integer-literal-suffix-inference.rs @@ -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; @@ -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); @@ -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 @@ -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 @@ -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 @@ -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); @@ -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 @@ -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 @@ -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 @@ -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 } |
