Skip to content

Commit c5709ff

Browse files
committed
const-prop: handle overflow_check consistently for all operators
1 parent 0633a0e commit c5709ff

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
541541
right: &Operand<'tcx>,
542542
source_info: SourceInfo,
543543
place_layout: TyLayout<'tcx>,
544-
overflow_check: bool,
545544
) -> Option<()> {
546545
let r =
547546
self.use_ecx(|this| this.ecx.read_immediate(this.ecx.eval_operand(right, None)?))?;
@@ -564,18 +563,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
564563
}
565564
}
566565

567-
// If overflow checking is enabled (like in debug mode by default),
568-
// then we'll already catch overflow when we evaluate the `Assert` statement
569-
// in MIR. However, if overflow checking is disabled, then there won't be any
570-
// `Assert` statement and so we have to do additional checking here.
571-
if !overflow_check {
572-
if self.use_ecx(|this| {
573-
let l = this.ecx.read_immediate(this.ecx.eval_operand(left, None)?)?;
574-
let (_res, overflow, _ty) = this.ecx.overflowing_binary_op(op, l, r)?;
575-
Ok(overflow)
576-
})? {
577-
self.report_panic_as_lint(source_info, PanicInfo::Overflow(op))?;
578-
}
566+
// The remaining operators are handled through `overflowing_binary_op`.
567+
// FIXME: Why do we not also do this for `Shr` and `Shl`?
568+
if self.use_ecx(|this| {
569+
let l = this.ecx.read_immediate(this.ecx.eval_operand(left, None)?)?;
570+
let (_res, overflow, _ty) = this.ecx.overflowing_binary_op(op, l, r)?;
571+
Ok(overflow)
572+
})? {
573+
self.report_panic_as_lint(source_info, PanicInfo::Overflow(op))?;
579574
}
580575

581576
Some(())
@@ -618,9 +613,9 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
618613

619614
// Additional checking: check for overflows on integer binary operations and report
620615
// them to the user as lints.
621-
Rvalue::BinaryOp(op, left, right) => {
616+
Rvalue::BinaryOp(op, left, right) if !overflow_check => {
622617
trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
623-
self.check_binary_op(*op, left, right, source_info, place_layout, overflow_check)?;
618+
self.check_binary_op(*op, left, right, source_info, place_layout)?;
624619
}
625620

626621
// Do not try creating references (#67862)

0 commit comments

Comments
 (0)