Skip to content

Commit

Permalink
Rollup merge of rust-lang#94354 - light4:issue-94239, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Fix show error message when literal overflows in match patterns

Fix rust-lang#94239
This changes overflow behavior in [fn lit_to_const](https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir_build/src/thir/constant.rs#L10)
  • Loading branch information
matthiaskrgr authored Feb 26, 2022
2 parents 0f95915 + b0c4db3 commit da3db4e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
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
6 changes: 3 additions & 3 deletions src/test/ui/error-codes/E0081.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/error-codes/E0081.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

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: u64) -> u64 {
match len {
10000000000000000000..=99999999999999999999 => 0, //~ ERROR literal out of range for `u64`
_ => 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 `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 `u64` whose range is `0..=18446744073709551615`

error: aborting due to previous error

0 comments on commit da3db4e

Please sign in to comment.