Skip to content

Commit 261765f

Browse files
committed
Fix show error message when literal overflows in match patterns
1 parent 03c8ffa commit 261765f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

compiler/rustc_mir_build/src/thir/constant.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ crate fn lit_to_const<'tcx>(
1717
let param_ty = ParamEnv::reveal_all().and(ty);
1818
let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
1919
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
20-
let result = width.truncate(n);
20+
let result = match &ty.kind() {
21+
ty::Uint(_) => {
22+
let max_value = width.unsigned_int_max();
23+
if n >= max_value { max_value } else { width.truncate(n) }
24+
}
25+
_ => width.truncate(n),
26+
};
2127
trace!("trunc result: {}", result);
2228
Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
2329
};

src/test/ui/issues/issue-94239.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub const fn test_match_range(len: usize) -> usize {
2+
match len {
3+
10000000000000000000..=99999999999999999999 => 0, //~ ERROR literal out of range for `usize`
4+
_ => unreachable!(),
5+
}
6+
}
7+
8+
fn main() {}

src/test/ui/issues/issue-94239.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: literal out of range for `usize`
2+
--> $DIR/issue-94239.rs:3:32
3+
|
4+
LL | 10000000000000000000..=99999999999999999999 => 0,
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[deny(overflowing_literals)]` on by default
8+
= note: the literal `99999999999999999999` does not fit into the type `usize` whose range is `0..=18446744073709551615`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)