@@ -61,11 +61,16 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
61
61
62
62
let def_id = src. def_id ( ) ;
63
63
64
- let mut rpo = traversal:: reverse_postorder ( body) ;
65
- let ( temps, all_candidates) = collect_temps_and_candidates ( tcx, body, & mut rpo) ;
64
+ let read_only_body = read_only ! ( body) ;
65
+
66
+ let mut rpo = traversal:: reverse_postorder ( & read_only_body) ;
67
+
68
+ let ccx = ConstCx :: new ( tcx, def_id, read_only_body) ;
69
+
70
+ let ( temps, all_candidates) = collect_temps_and_candidates ( & ccx, & mut rpo) ;
66
71
67
72
let promotable_candidates =
68
- validate_candidates ( tcx, read_only ! ( body ) , def_id, & temps, & all_candidates) ;
73
+ validate_candidates ( tcx, read_only_body , def_id, & temps, & all_candidates) ;
69
74
70
75
let promoted = promote_candidates ( def_id, body, tcx, temps, promotable_candidates) ;
71
76
self . promoted_fragments . set ( promoted) ;
@@ -140,8 +145,7 @@ fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Vec<usize>> {
140
145
}
141
146
142
147
struct Collector < ' a , ' tcx > {
143
- tcx : TyCtxt < ' tcx > ,
144
- body : & ' a Body < ' tcx > ,
148
+ ccx : & ' a ConstCx < ' a , ' tcx > ,
145
149
temps : IndexVec < Local , TempState > ,
146
150
candidates : Vec < Candidate > ,
147
151
span : Span ,
@@ -151,7 +155,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
151
155
fn visit_local ( & mut self , & index: & Local , context : PlaceContext , location : Location ) {
152
156
debug ! ( "visit_local: index={:?} context={:?} location={:?}" , index, context, location) ;
153
157
// We're only interested in temporaries and the return place
154
- match self . body . local_kind ( index) {
158
+ match self . ccx . body . local_kind ( index) {
155
159
LocalKind :: Temp | LocalKind :: ReturnPointer => { }
156
160
LocalKind :: Arg | LocalKind :: Var => return ,
157
161
}
@@ -204,7 +208,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
204
208
Rvalue :: Ref ( ..) => {
205
209
self . candidates . push ( Candidate :: Ref ( location) ) ;
206
210
}
207
- Rvalue :: Repeat ( ..) if self . tcx . features ( ) . const_in_array_repeat_expressions => {
211
+ Rvalue :: Repeat ( ..) if self . ccx . tcx . features ( ) . const_in_array_repeat_expressions => {
208
212
// FIXME(#49147) only promote the element when it isn't `Copy`
209
213
// (so that code that can copy it at runtime is unaffected).
210
214
self . candidates . push ( Candidate :: Repeat ( location) ) ;
@@ -217,10 +221,10 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
217
221
self . super_terminator_kind ( kind, location) ;
218
222
219
223
if let TerminatorKind :: Call { ref func, .. } = * kind {
220
- if let ty:: FnDef ( def_id, _) = func. ty ( self . body , self . tcx ) . kind {
221
- let fn_sig = self . tcx . fn_sig ( def_id) ;
224
+ if let ty:: FnDef ( def_id, _) = func. ty ( * self . ccx . body , self . ccx . tcx ) . kind {
225
+ let fn_sig = self . ccx . tcx . fn_sig ( def_id) ;
222
226
if let Abi :: RustIntrinsic | Abi :: PlatformIntrinsic = fn_sig. abi ( ) {
223
- let name = self . tcx . item_name ( def_id) ;
227
+ let name = self . ccx . tcx . item_name ( def_id) ;
224
228
// FIXME(eddyb) use `#[rustc_args_required_const(2)]` for shuffles.
225
229
if name. as_str ( ) . starts_with ( "simd_shuffle" ) {
226
230
self . candidates . push ( Candidate :: Argument { bb : location. block , index : 2 } ) ;
@@ -229,7 +233,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
229
233
}
230
234
}
231
235
232
- if let Some ( constant_args) = args_required_const ( self . tcx , def_id) {
236
+ if let Some ( constant_args) = args_required_const ( self . ccx . tcx , def_id) {
233
237
for index in constant_args {
234
238
self . candidates . push ( Candidate :: Argument { bb : location. block , index } ) ;
235
239
}
@@ -244,16 +248,14 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
244
248
}
245
249
246
250
pub fn collect_temps_and_candidates (
247
- tcx : TyCtxt < ' tcx > ,
248
- body : & Body < ' tcx > ,
251
+ ccx : & ConstCx < ' mir , ' tcx > ,
249
252
rpo : & mut ReversePostorder < ' _ , ' tcx > ,
250
253
) -> ( IndexVec < Local , TempState > , Vec < Candidate > ) {
251
254
let mut collector = Collector {
252
- tcx,
253
- body,
254
- temps : IndexVec :: from_elem ( TempState :: Undefined , & body. local_decls ) ,
255
+ temps : IndexVec :: from_elem ( TempState :: Undefined , & ccx. body . local_decls ) ,
255
256
candidates : vec ! [ ] ,
256
- span : body. span ,
257
+ span : ccx. body . span ,
258
+ ccx,
257
259
} ;
258
260
for ( bb, data) in rpo {
259
261
collector. visit_basic_block_data ( bb, data) ;
@@ -1157,7 +1159,7 @@ crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>(
1157
1159
operand : & Operand < ' tcx > ,
1158
1160
) -> bool {
1159
1161
let mut rpo = traversal:: reverse_postorder ( & ccx. body ) ;
1160
- let ( temps, _) = collect_temps_and_candidates ( ccx . tcx , & ccx. body , & mut rpo) ;
1162
+ let ( temps, _) = collect_temps_and_candidates ( & ccx, & mut rpo) ;
1161
1163
let validator = Validator { ccx, temps : & temps, explicit : false } ;
1162
1164
1163
1165
let should_promote = validator. validate_operand ( operand) . is_ok ( ) ;
0 commit comments