diff options
| author | bors <bors@rust-lang.org> | 2022-09-12 16:56:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-09-12 16:56:53 +0000 |
| commit | 7b8c4a9e832b8a2b9717cd0b26382f5ccb217c5a (patch) | |
| tree | 5ac843535564f755a16f70510c204f8219d213a0 | |
| parent | cdf26de6b5cd0025b941433e723dbdd711b2d4b2 (diff) | |
| parent | 1e23c65d5e4d112c89da3d826a4658c14932930c (diff) | |
| download | rust-7b8c4a9e832b8a2b9717cd0b26382f5ccb217c5a.tar.gz rust-7b8c4a9e832b8a2b9717cd0b26382f5ccb217c5a.zip | |
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
| -rw-r--r-- | clippy_utils/src/consts.rs | 8 | ||||
| -rw-r--r-- | tests/ui/crashes/ice-9463.rs | 5 | ||||
| -rw-r--r-- | tests/ui/crashes/ice-9463.stderr | 29 |
3 files changed, 38 insertions, 4 deletions
diff --git a/clippy_utils/src/consts.rs b/clippy_utils/src/consts.rs index e053708edd5..53d47ec53fb 100644 --- a/clippy_utils/src/consts.rs +++ b/clippy_utils/src/consts.rs @@ -501,8 +501,8 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> { BinOpKind::Mul => l.checked_mul(r).map(zext), BinOpKind::Div if r != 0 => l.checked_div(r).map(zext), BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext), - BinOpKind::Shr => l.checked_shr(r.try_into().expect("invalid shift")).map(zext), - BinOpKind::Shl => l.checked_shl(r.try_into().expect("invalid shift")).map(zext), + BinOpKind::Shr => l.checked_shr(r.try_into().ok()?).map(zext), + BinOpKind::Shl => l.checked_shl(r.try_into().ok()?).map(zext), BinOpKind::BitXor => Some(zext(l ^ r)), BinOpKind::BitOr => Some(zext(l | r)), BinOpKind::BitAnd => Some(zext(l & r)), @@ -521,8 +521,8 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> { BinOpKind::Mul => l.checked_mul(r).map(Constant::Int), BinOpKind::Div => l.checked_div(r).map(Constant::Int), BinOpKind::Rem => l.checked_rem(r).map(Constant::Int), - BinOpKind::Shr => l.checked_shr(r.try_into().expect("shift too large")).map(Constant::Int), - BinOpKind::Shl => l.checked_shl(r.try_into().expect("shift too large")).map(Constant::Int), + BinOpKind::Shr => l.checked_shr(r.try_into().ok()?).map(Constant::Int), + BinOpKind::Shl => l.checked_shl(r.try_into().ok()?).map(Constant::Int), BinOpKind::BitXor => Some(Constant::Int(l ^ r)), BinOpKind::BitOr => Some(Constant::Int(l | r)), BinOpKind::BitAnd => Some(Constant::Int(l & r)), diff --git a/tests/ui/crashes/ice-9463.rs b/tests/ui/crashes/ice-9463.rs new file mode 100644 index 00000000000..41ef930d323 --- /dev/null +++ b/tests/ui/crashes/ice-9463.rs @@ -0,0 +1,5 @@ +#![deny(arithmetic_overflow, const_err)] +fn main() { + let _x = -1_i32 >> -1; + let _y = 1u32 >> 10000000000000u32; +} diff --git a/tests/ui/crashes/ice-9463.stderr b/tests/ui/crashes/ice-9463.stderr new file mode 100644 index 00000000000..7daa08aeb6c --- /dev/null +++ b/tests/ui/crashes/ice-9463.stderr @@ -0,0 +1,29 @@ +error: this arithmetic operation will overflow + --> $DIR/ice-9463.rs:3:14 + | +LL | let _x = -1_i32 >> -1; + | ^^^^^^^^^^^^ attempt to shift right by `-1_i32`, which would overflow + | +note: the lint level is defined here + --> $DIR/ice-9463.rs:1:9 + | +LL | #![deny(arithmetic_overflow, const_err)] + | ^^^^^^^^^^^^^^^^^^^ + +error: this arithmetic operation will overflow + --> $DIR/ice-9463.rs:4:14 + | +LL | let _y = 1u32 >> 10000000000000u32; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to shift right by `1316134912_u32`, which would overflow + +error: literal out of range for `u32` + --> $DIR/ice-9463.rs:4:22 + | +LL | let _y = 1u32 >> 10000000000000u32; + | ^^^^^^^^^^^^^^^^^ + | + = note: `#[deny(overflowing_literals)]` on by default + = note: the literal `10000000000000u32` does not fit into the type `u32` whose range is `0..=4294967295` + +error: aborting due to 3 previous errors + |
