提交 55464ebc 编写于 作者: Y Yashhwanth Ram

Fix order of comparison and remove incorrect case for ints in typeck/demand.rs

Add tests for *size
上级 b3c9912d
......@@ -753,9 +753,9 @@ pub fn check_for_cast(
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 @@ pub fn check_for_cast(
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,
};
......
......@@ -16,6 +16,7 @@ fn id_i8(n: i8) -> i8 { n }
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 id_u8(n: u8) -> u8 { n }
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 id_u64(n: u64) -> u64 { n }
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 id_u64(n: u64) -> u64 { n }
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 id_u64(n: u64) -> u64 { n }
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 id_u64(n: u64) -> u64 { n }
//~^ 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 id_u64(n: u64) -> u64 { n }
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 id_u64(n: u64) -> u64 { n }
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 id_u64(n: u64) -> u64 { n }
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 id_u64(n: u64) -> u64 { n }
//~^ 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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册