Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d86acdd

Browse files
committed
Prevent propagation of overflow if overflow occured
1 parent 5b7b309 commit d86acdd

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_mir_dataflow::value_analysis::{
1111
HasTop, Map, State, TrackElem, ValueAnalysis, ValueOrPlace, ValueOrPlaceOrRef,
1212
};
1313
use rustc_mir_dataflow::{lattice::FlatSet, Analysis, ResultsVisitor, SwitchIntEdgeEffects};
14-
use rustc_span::{sym, DUMMY_SP};
14+
use rustc_span::DUMMY_SP;
1515

1616
use crate::MirPass;
1717

@@ -42,7 +42,6 @@ struct ConstAnalysis<'tcx> {
4242
tcx: TyCtxt<'tcx>,
4343
ecx: InterpCx<'tcx, 'tcx, DummyMachine>,
4444
param_env: ty::ParamEnv<'tcx>,
45-
propagate_overflow: bool,
4645
}
4746

4847
impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'tcx> {
@@ -84,13 +83,11 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'tcx> {
8483
let overflow = match overflow {
8584
FlatSet::Top => FlatSet::Top,
8685
FlatSet::Elem(overflow) => {
87-
if overflow && !self.propagate_overflow {
86+
if overflow {
87+
// Overflow cannot be reliable propagated. See: https://github.com/rust-lang/rust/pull/101168#issuecomment-1288091446
8888
FlatSet::Top
8989
} else {
90-
self.wrap_scalar(
91-
Scalar::from_bool(overflow),
92-
self.tcx.types.bool,
93-
)
90+
self.wrap_scalar(Scalar::from_bool(false), self.tcx.types.bool)
9491
}
9592
}
9693
FlatSet::Bottom => FlatSet::Bottom,
@@ -220,20 +217,11 @@ impl<'tcx> std::fmt::Debug for ScalarTy<'tcx> {
220217

221218
impl<'tcx> ConstAnalysis<'tcx> {
222219
pub fn new(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, map: Map) -> Self {
223-
// It can happen that overflow will be detected even though overflow checks are disabled.
224-
// This is caused by inlining functions that have #[rustc_inherit_overflow_checks]. Such
225-
// overflows must not be propagated if `-C overflow-checks=off`. Also, if the function we
226-
// are optimizing here has #[rustc_inherit_overflow_checks], the overflow checks may
227-
// actually not be triggered by the consuming crate, so we have to ignore them too.
228-
// Related to https://github.com/rust-lang/rust/issues/35310.
229-
let propagate_overflow = tcx.sess.overflow_checks()
230-
&& !tcx.has_attr(body.source.def_id(), sym::rustc_inherit_overflow_checks);
231220
Self {
232221
map,
233222
tcx,
234223
ecx: InterpCx::new(tcx, DUMMY_SP, ty::ParamEnv::empty(), DummyMachine),
235224
param_env: tcx.param_env(body.source.def_id()),
236-
propagate_overflow,
237225
}
238226
}
239227

src/test/mir-opt/dataflow-const-prop/checked.main.DataflowConstProp.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
- assert(!move (_10.1: bool), "attempt to compute `{} + {}`, which would overflow", move _9, const 1_i32) -> bb2; // scope 4 at $DIR/checked.rs:+6:13: +6:18
6262
+ _9 = const i32::MAX; // scope 4 at $DIR/checked.rs:+6:13: +6:14
6363
+ _10 = CheckedAdd(const i32::MAX, const 1_i32); // scope 4 at $DIR/checked.rs:+6:13: +6:18
64-
+ assert(!const true, "attempt to compute `{} + {}`, which would overflow", const i32::MAX, const 1_i32) -> bb2; // scope 4 at $DIR/checked.rs:+6:13: +6:18
64+
+ assert(!move (_10.1: bool), "attempt to compute `{} + {}`, which would overflow", const i32::MAX, const 1_i32) -> bb2; // scope 4 at $DIR/checked.rs:+6:13: +6:18
6565
}
6666

6767
bb2: {

0 commit comments

Comments
 (0)