Skip to content

Commit 2f6e996

Browse files
committed
always check overflow in CheckedBinOp in CTFE
1 parent 6f01ff6 commit 2f6e996

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

+8
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ pub trait Machine<'mir, 'tcx>: Sized {
144144
true
145145
}
146146

147+
/// Whether CheckedBinOp MIR statements should actually check for overflow.
148+
fn check_binop_checks_overflow(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
149+
147150
/// Entry point for obtaining the MIR of anything that should get evaluated.
148151
/// So not just functions and shims, but also const/static initializers, anonymous
149152
/// constants, ...
@@ -468,6 +471,11 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
468471
true
469472
}
470473

474+
#[inline(always)]
475+
fn check_binop_checks_overflow(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
476+
true
477+
}
478+
471479
#[inline(always)]
472480
fn call_extra_fn(
473481
_ecx: &mut InterpCx<$mir, $tcx, Self>,

compiler/rustc_const_eval/src/interpret/operator.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3232
);
3333
// As per https://github.com/rust-lang/rust/pull/98738, we always return `false` in the 2nd
3434
// component when overflow checking is disabled.
35-
let overflowed = overflowed && (force_overflow_checks || self.tcx.sess.overflow_checks());
35+
let overflowed =
36+
overflowed && (force_overflow_checks || M::check_binop_checks_overflow(self));
3637
// Write the result to `dest`.
3738
if let Abi::ScalarPair(..) = dest.layout.abi {
3839
// We can use the optimized path and avoid `place_field` (which might do

compiler/rustc_middle/src/mir/syntax.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,9 @@ pub enum Rvalue<'tcx> {
993993

994994
/// Same as `BinaryOp`, but yields `(T, bool)` with a `bool` indicating an error condition.
995995
///
996-
/// When overflow checking is disabled, the error condition is false. Otherwise, the error
997-
/// condition is determined as described below.
996+
/// When overflow checking is disabled and we are generating run-time code, the error condition
997+
/// is false. Otherwise, and always during CTFE, the error condition is determined as described
998+
/// below.
998999
///
9991000
/// For addition, subtraction, and multiplication on integers the error condition is set when
10001001
/// the infinite precision result would be unequal to the actual result.

0 commit comments

Comments
 (0)