@@ -7,7 +7,7 @@ use rustc_mir_dataflow::value_analysis::{
7
7
Map , ProjElem , State , ValueAnalysis , ValueOrPlace , ValueOrPlaceOrRef ,
8
8
} ;
9
9
use rustc_mir_dataflow:: { lattice:: FlatSet , Analysis , ResultsVisitor , SwitchIntEdgeEffects } ;
10
- use rustc_span:: DUMMY_SP ;
10
+ use rustc_span:: { sym , DUMMY_SP } ;
11
11
12
12
use crate :: MirPass ;
13
13
@@ -38,6 +38,7 @@ struct ConstAnalysis<'tcx> {
38
38
tcx : TyCtxt < ' tcx > ,
39
39
ecx : InterpCx < ' tcx , ' tcx , DummyMachine > ,
40
40
param_env : ty:: ParamEnv < ' tcx > ,
41
+ propagate_overflow : bool ,
41
42
}
42
43
43
44
impl < ' tcx > ValueAnalysis < ' tcx > for ConstAnalysis < ' tcx > {
@@ -72,7 +73,11 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'tcx> {
72
73
} ) ;
73
74
74
75
if value_target. is_some ( ) || overflow_target. is_some ( ) {
75
- let ( val, overflow) = self . binary_op ( state, * op, left, right) ;
76
+ let ( val, mut overflow) = self . binary_op ( state, * op, left, right) ;
77
+
78
+ if !self . propagate_overflow {
79
+ overflow = FlatSet :: Top ;
80
+ }
76
81
77
82
if let Some ( value_target) = value_target {
78
83
state. assign_idx ( value_target, ValueOrPlaceOrRef :: Value ( val) , self . map ( ) ) ;
@@ -202,11 +207,20 @@ impl<'tcx> std::fmt::Debug for ScalarTy<'tcx> {
202
207
203
208
impl < ' tcx > ConstAnalysis < ' tcx > {
204
209
pub fn new ( tcx : TyCtxt < ' tcx > , body : & Body < ' tcx > , map : Map ) -> Self {
210
+ // It can happen that overflow will be detected even though overflow checks are disabled.
211
+ // This is caused by inlining functions that have #[rustc_inherit_overflow_checks]. Such
212
+ // overflows must not be propagated if `-C overflow-checks=off`. Also, if the function we
213
+ // are optimizing here has #[rustc_inherit_overflow_checks], the overflow checks may
214
+ // actually not be triggered by the consuming crate, so we have to ignore them too.
215
+ // Related to https://github.com/rust-lang/rust/issues/35310.
216
+ let propagate_overflow = tcx. sess . overflow_checks ( )
217
+ && !tcx. has_attr ( body. source . def_id ( ) , sym:: rustc_inherit_overflow_checks) ;
205
218
Self {
206
219
map,
207
220
tcx,
208
221
ecx : InterpCx :: new ( tcx, DUMMY_SP , ty:: ParamEnv :: empty ( ) , DummyMachine ) ,
209
222
param_env : tcx. param_env ( body. source . def_id ( ) ) ,
223
+ propagate_overflow,
210
224
}
211
225
}
212
226
0 commit comments