Skip to content

Commit 7b8c4a9

Browse files
committed
Auto merge of #9464 - lukaslueg:issue9463, r=dswij
Don't panic on invalid shift while constfolding Instead of panicking on invalid shifts while folding constants we simply give up. Fixes #9463 Notice the "attempt to shift right by `1316134912_u32`", which seems weird. AFAICS it comes from rustc itself. changelog: none
2 parents cdf26de + 1e23c65 commit 7b8c4a9

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

clippy_utils/src/consts.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,8 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
501501
BinOpKind::Mul => l.checked_mul(r).map(zext),
502502
BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
503503
BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
504-
BinOpKind::Shr => l.checked_shr(r.try_into().expect("invalid shift")).map(zext),
505-
BinOpKind::Shl => l.checked_shl(r.try_into().expect("invalid shift")).map(zext),
504+
BinOpKind::Shr => l.checked_shr(r.try_into().ok()?).map(zext),
505+
BinOpKind::Shl => l.checked_shl(r.try_into().ok()?).map(zext),
506506
BinOpKind::BitXor => Some(zext(l ^ r)),
507507
BinOpKind::BitOr => Some(zext(l | r)),
508508
BinOpKind::BitAnd => Some(zext(l & r)),
@@ -521,8 +521,8 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
521521
BinOpKind::Mul => l.checked_mul(r).map(Constant::Int),
522522
BinOpKind::Div => l.checked_div(r).map(Constant::Int),
523523
BinOpKind::Rem => l.checked_rem(r).map(Constant::Int),
524-
BinOpKind::Shr => l.checked_shr(r.try_into().expect("shift too large")).map(Constant::Int),
525-
BinOpKind::Shl => l.checked_shl(r.try_into().expect("shift too large")).map(Constant::Int),
524+
BinOpKind::Shr => l.checked_shr(r.try_into().ok()?).map(Constant::Int),
525+
BinOpKind::Shl => l.checked_shl(r.try_into().ok()?).map(Constant::Int),
526526
BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
527527
BinOpKind::BitOr => Some(Constant::Int(l | r)),
528528
BinOpKind::BitAnd => Some(Constant::Int(l & r)),

tests/ui/crashes/ice-9463.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![deny(arithmetic_overflow, const_err)]
2+
fn main() {
3+
let _x = -1_i32 >> -1;
4+
let _y = 1u32 >> 10000000000000u32;
5+
}

tests/ui/crashes/ice-9463.stderr

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: this arithmetic operation will overflow
2+
--> $DIR/ice-9463.rs:3:14
3+
|
4+
LL | let _x = -1_i32 >> -1;
5+
| ^^^^^^^^^^^^ attempt to shift right by `-1_i32`, which would overflow
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/ice-9463.rs:1:9
9+
|
10+
LL | #![deny(arithmetic_overflow, const_err)]
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: this arithmetic operation will overflow
14+
--> $DIR/ice-9463.rs:4:14
15+
|
16+
LL | let _y = 1u32 >> 10000000000000u32;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to shift right by `1316134912_u32`, which would overflow
18+
19+
error: literal out of range for `u32`
20+
--> $DIR/ice-9463.rs:4:22
21+
|
22+
LL | let _y = 1u32 >> 10000000000000u32;
23+
| ^^^^^^^^^^^^^^^^^
24+
|
25+
= note: `#[deny(overflowing_literals)]` on by default
26+
= note: the literal `10000000000000u32` does not fit into the type `u32` whose range is `0..=4294967295`
27+
28+
error: aborting due to 3 previous errors
29+

0 commit comments

Comments
 (0)