Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix show error message when literal overflows in match patterns #94354

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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