Skip to content

Commit b5c28e0

Browse files
committed
[const-prop] Replace CheckedBinaryOp handling with use of InterpCx
1 parent ec15579 commit b5c28e0

9 files changed

+18
-18
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
312312
Rvalue::Use(_) |
313313
Rvalue::Len(_) |
314314
Rvalue::Cast(..) |
315-
Rvalue::NullaryOp(..) => {
315+
Rvalue::NullaryOp(..) |
316+
Rvalue::CheckedBinaryOp(..) => {
316317
self.use_ecx(source_info, |this| {
317318
this.ecx.eval_rvalue_into_place(rvalue, place)?;
318319
this.ecx.eval_place_to_op(place, Some(place_layout))
@@ -348,7 +349,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
348349
this.ecx.eval_place_to_op(place, Some(place_layout))
349350
})
350351
}
351-
Rvalue::CheckedBinaryOp(op, ref left, ref right) |
352352
Rvalue::BinaryOp(op, ref left, ref right) => {
353353
trace!("rvalue binop {:?} for {:?} and {:?}", op, left, right);
354354
let right = self.eval_operand(right, source_info)?;
@@ -403,23 +403,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
403403
let (val, overflow, _ty) = self.use_ecx(source_info, |this| {
404404
this.ecx.overflowing_binary_op(op, l, r)
405405
})?;
406-
let val = if let Rvalue::CheckedBinaryOp(..) = *rvalue {
407-
Immediate::ScalarPair(
408-
val.into(),
409-
Scalar::from_bool(overflow).into(),
410-
)
411-
} else {
412-
// We check overflow in debug mode already
413-
// so should only check in release mode.
414-
if !self.tcx.sess.overflow_checks() && overflow {
415-
let err = err_panic!(Overflow(op)).into();
416-
let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
417-
return None;
418-
}
419-
Immediate::Scalar(val.into())
420-
};
406+
// We check overflow in debug mode already
407+
// so should only check in release mode.
408+
if !self.tcx.sess.overflow_checks() && overflow {
409+
let err = err_panic!(Overflow(op)).into();
410+
let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
411+
return None;
412+
}
421413
let res = ImmTy {
422-
imm: val,
414+
imm: Immediate::Scalar(val.into()),
423415
layout: place_layout,
424416
};
425417
Some(res.into())

src/test/run-fail/overflowing-lsh-1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// compile-flags: -C debug-assertions
33

44
#![warn(exceeding_bitshifts)]
5+
#![warn(const_err)]
56

67
fn main() {
78
let _x = 1_i32 << 32;

src/test/run-fail/overflowing-lsh-2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// compile-flags: -C debug-assertions
33

44
#![warn(exceeding_bitshifts)]
5+
#![warn(const_err)]
56

67
fn main() {
78
let _x = 1 << -1;

src/test/run-fail/overflowing-lsh-3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// compile-flags: -C debug-assertions
33

44
#![warn(exceeding_bitshifts)]
5+
#![warn(const_err)]
56

67
fn main() {
78
let _x = 1_u64 << 64;

src/test/run-fail/overflowing-lsh-4.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// sidestep the overflow checking.
66

77
#![warn(exceeding_bitshifts)]
8+
#![warn(const_err)]
89

910
fn main() {
1011
// this signals overflow when checking is on

src/test/run-fail/overflowing-rsh-1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// compile-flags: -C debug-assertions
33

44
#![warn(exceeding_bitshifts)]
5+
#![warn(const_err)]
56

67
fn main() {
78
let _x = -1_i32 >> 32;

src/test/run-fail/overflowing-rsh-2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// compile-flags: -C debug-assertions
33

44
#![warn(exceeding_bitshifts)]
5+
#![warn(const_err)]
56

67
fn main() {
78
let _x = -1_i32 >> -1;

src/test/run-fail/overflowing-rsh-3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// compile-flags: -C debug-assertions
33

44
#![warn(exceeding_bitshifts)]
5+
#![warn(const_err)]
56

67
fn main() {
78
let _x = -1_i64 >> 64;

src/test/run-fail/overflowing-rsh-4.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// truncation does not sidestep the overflow checking.
66

77
#![warn(exceeding_bitshifts)]
8+
#![warn(const_err)]
89

910
fn main() {
1011
// this signals overflow when checking is on

0 commit comments

Comments
 (0)