Skip to content

Commit 6f01ff6

Browse files
committed
interpret: fix CheckedBinOp behavior when overflow checking is disabled
1 parent e1d1848 commit 6f01ff6

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
217217
sym::mul_with_overflow => BinOp::Mul,
218218
_ => bug!(),
219219
};
220-
self.binop_with_overflow(bin_op, &lhs, &rhs, dest)?;
220+
self.binop_with_overflow(
221+
bin_op, /*force_overflow_checks*/ true, &lhs, &rhs, dest,
222+
)?;
221223
}
222224
sym::saturating_add | sym::saturating_sub => {
223225
let l = self.read_immediate(&args[0])?;

compiler/rustc_const_eval/src/interpret/operator.rs

+8
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ use super::{ImmTy, Immediate, InterpCx, Machine, PlaceTy};
1212
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
1313
/// Applies the binary operation `op` to the two operands and writes a tuple of the result
1414
/// and a boolean signifying the potential overflow to the destination.
15+
///
16+
/// `force_overflow_checks` indicates whether overflow checks should be done even when
17+
/// `tcx.sess.overflow_checks()` is `false`.
1518
pub fn binop_with_overflow(
1619
&mut self,
1720
op: mir::BinOp,
21+
force_overflow_checks: bool,
1822
left: &ImmTy<'tcx, M::PointerTag>,
1923
right: &ImmTy<'tcx, M::PointerTag>,
2024
dest: &PlaceTy<'tcx, M::PointerTag>,
@@ -26,6 +30,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2630
"type mismatch for result of {:?}",
2731
op,
2832
);
33+
// As per https://github.com/rust-lang/rust/pull/98738, we always return `false` in the 2nd
34+
// component when overflow checking is disabled.
35+
let overflowed = overflowed && (force_overflow_checks || self.tcx.sess.overflow_checks());
36+
// Write the result to `dest`.
2937
if let Abi::ScalarPair(..) = dest.layout.abi {
3038
// We can use the optimized path and avoid `place_field` (which might do
3139
// `force_allocation`).

compiler/rustc_const_eval/src/interpret/step.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
185185
let left = self.read_immediate(&self.eval_operand(left, None)?)?;
186186
let layout = binop_right_homogeneous(bin_op).then_some(left.layout);
187187
let right = self.read_immediate(&self.eval_operand(right, layout)?)?;
188-
self.binop_with_overflow(bin_op, &left, &right, &dest)?;
188+
self.binop_with_overflow(
189+
bin_op, /*force_overflow_checks*/ false, &left, &right, &dest,
190+
)?;
189191
}
190192

191193
UnaryOp(un_op, ref operand) => {

0 commit comments

Comments
 (0)