Skip to content

Commit

Permalink
Fix show error message when literal overflows in match patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
light4 committed Feb 25, 2022
1 parent 03c8ffa commit 261765f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/rustc_mir_build/src/thir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
};
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/issues/issue-94239.rs
Original file line number Diff line number Diff line change
@@ -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() {}
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-94239.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 261765f

Please sign in to comment.