From 261765fe8ba7dbe28d33ed4f92609e5d468064d6 Mon Sep 17 00:00:00 2001 From: lightning1141 Date: Fri, 25 Feb 2022 19:35:03 +0800 Subject: [PATCH 1/3] Fix show error message when literal overflows in match patterns --- compiler/rustc_mir_build/src/thir/constant.rs | 8 +++++++- src/test/ui/issues/issue-94239.rs | 8 ++++++++ src/test/ui/issues/issue-94239.stderr | 11 +++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/issues/issue-94239.rs create mode 100644 src/test/ui/issues/issue-94239.stderr diff --git a/compiler/rustc_mir_build/src/thir/constant.rs b/compiler/rustc_mir_build/src/thir/constant.rs index 72c0985a63c33..a94c2b9f5f7d4 100644 --- a/compiler/rustc_mir_build/src/thir/constant.rs +++ b/compiler/rustc_mir_build/src/thir/constant.rs @@ -17,7 +17,13 @@ crate fn lit_to_const<'tcx>( let param_ty = ParamEnv::reveal_all().and(ty); let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size; trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits()); - let result = width.truncate(n); + let result = match &ty.kind() { + ty::Uint(_) => { + let max_value = width.unsigned_int_max(); + if n >= max_value { max_value } else { width.truncate(n) } + } + _ => width.truncate(n), + }; trace!("trunc result: {}", result); Ok(ConstValue::Scalar(Scalar::from_uint(result, width))) }; diff --git a/src/test/ui/issues/issue-94239.rs b/src/test/ui/issues/issue-94239.rs new file mode 100644 index 0000000000000..58ebba6764cef --- /dev/null +++ b/src/test/ui/issues/issue-94239.rs @@ -0,0 +1,8 @@ +pub const fn test_match_range(len: usize) -> usize { + match len { + 10000000000000000000..=99999999999999999999 => 0, //~ ERROR literal out of range for `usize` + _ => unreachable!(), + } +} + +fn main() {} diff --git a/src/test/ui/issues/issue-94239.stderr b/src/test/ui/issues/issue-94239.stderr new file mode 100644 index 0000000000000..f847d45949baf --- /dev/null +++ b/src/test/ui/issues/issue-94239.stderr @@ -0,0 +1,11 @@ +error: literal out of range for `usize` + --> $DIR/issue-94239.rs:3:32 + | +LL | 10000000000000000000..=99999999999999999999 => 0, + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[deny(overflowing_literals)]` on by default + = note: the literal `99999999999999999999` does not fit into the type `usize` whose range is `0..=18446744073709551615` + +error: aborting due to previous error + From 5b58c6feb87face0118bd36b61f58d137dd625e7 Mon Sep 17 00:00:00 2001 From: lightning1141 Date: Fri, 25 Feb 2022 21:12:04 +0800 Subject: [PATCH 2/3] fix E0081 error message due to lit_to_const behavior change --- src/test/ui/error-codes/E0081.rs | 6 +++--- src/test/ui/error-codes/E0081.stderr | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/ui/error-codes/E0081.rs b/src/test/ui/error-codes/E0081.rs index 255e05ced19f7..6f15b9d2ef95c 100644 --- a/src/test/ui/error-codes/E0081.rs +++ b/src/test/ui/error-codes/E0081.rs @@ -10,10 +10,10 @@ enum Enum { #[repr(u8)] enum EnumOverflowRepr { P = 257, - //~^ NOTE first use of `1` (overflowed from `257`) + //~^ NOTE first use of `255` (overflowed from `257`) X = 513, - //~^ ERROR discriminant value `1` already exists - //~| NOTE enum already has `1` (overflowed from `513`) + //~^ ERROR discriminant value `255` already exists + //~| NOTE enum already has `255` (overflowed from `513`) } fn main() { diff --git a/src/test/ui/error-codes/E0081.stderr b/src/test/ui/error-codes/E0081.stderr index 9b279bb0214c6..68bb0f407b82f 100644 --- a/src/test/ui/error-codes/E0081.stderr +++ b/src/test/ui/error-codes/E0081.stderr @@ -7,14 +7,14 @@ LL | LL | X = 3, | ^ enum already has `3` -error[E0081]: discriminant value `1` already exists +error[E0081]: discriminant value `255` already exists --> $DIR/E0081.rs:14:9 | LL | P = 257, - | --- first use of `1` (overflowed from `257`) + | --- first use of `255` (overflowed from `257`) LL | LL | X = 513, - | ^^^ enum already has `1` (overflowed from `513`) + | ^^^ enum already has `255` (overflowed from `513`) error: aborting due to 2 previous errors From b0c4db35942cfaa860d2d60b468af8734a34674d Mon Sep 17 00:00:00 2001 From: lightning1141 Date: Fri, 25 Feb 2022 21:57:09 +0800 Subject: [PATCH 3/3] Fix ci test issue-94239.rs --- src/test/ui/issues/issue-94239.rs | 4 ++-- src/test/ui/issues/issue-94239.stderr | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/ui/issues/issue-94239.rs b/src/test/ui/issues/issue-94239.rs index 58ebba6764cef..a09ae32aa74d6 100644 --- a/src/test/ui/issues/issue-94239.rs +++ b/src/test/ui/issues/issue-94239.rs @@ -1,6 +1,6 @@ -pub const fn test_match_range(len: usize) -> usize { +pub const fn test_match_range(len: u64) -> u64 { match len { - 10000000000000000000..=99999999999999999999 => 0, //~ ERROR literal out of range for `usize` + 10000000000000000000..=99999999999999999999 => 0, //~ ERROR literal out of range for `u64` _ => unreachable!(), } } diff --git a/src/test/ui/issues/issue-94239.stderr b/src/test/ui/issues/issue-94239.stderr index f847d45949baf..057fbfec64f9b 100644 --- a/src/test/ui/issues/issue-94239.stderr +++ b/src/test/ui/issues/issue-94239.stderr @@ -1,11 +1,11 @@ -error: literal out of range for `usize` +error: literal out of range for `u64` --> $DIR/issue-94239.rs:3:32 | LL | 10000000000000000000..=99999999999999999999 => 0, | ^^^^^^^^^^^^^^^^^^^^ | = note: `#[deny(overflowing_literals)]` on by default - = note: the literal `99999999999999999999` does not fit into the type `usize` whose range is `0..=18446744073709551615` + = note: the literal `99999999999999999999` does not fit into the type `u64` whose range is `0..=18446744073709551615` error: aborting due to previous error