3
3
4
4
use std:: fmt:: Debug ;
5
5
6
- use rustc_const_eval:: interpret:: { ImmTy , MPlaceTy , Projectable } ;
7
- use rustc_const_eval:: interpret:: { InterpCx , InterpResult , OpTy , Scalar , StackPopCleanup } ;
6
+ use rustc_const_eval:: interpret:: { ImmTy , Projectable } ;
7
+ use rustc_const_eval:: interpret:: { InterpCx , InterpResult , OpTy , Scalar } ;
8
8
use rustc_hir:: def:: DefKind ;
9
9
use rustc_hir:: HirId ;
10
10
use rustc_index:: bit_set:: BitSet ;
11
11
use rustc_index:: { Idx , IndexVec } ;
12
12
use rustc_middle:: mir:: visit:: Visitor ;
13
13
use rustc_middle:: mir:: * ;
14
14
use rustc_middle:: ty:: layout:: { LayoutError , LayoutOf , LayoutOfHelpers , TyAndLayout } ;
15
- use rustc_middle:: ty:: GenericArgs ;
16
- use rustc_middle:: ty:: {
17
- self , ConstInt , Instance , ParamEnv , ScalarInt , Ty , TyCtxt , TypeVisitableExt ,
18
- } ;
15
+ use rustc_middle:: ty:: { self , ConstInt , ParamEnv , ScalarInt , Ty , TyCtxt , TypeVisitableExt } ;
19
16
use rustc_span:: Span ;
20
17
use rustc_target:: abi:: { Abi , FieldIdx , HasDataLayout , Size , TargetDataLayout , VariantIdx } ;
21
18
@@ -72,12 +69,13 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
72
69
73
70
/// Finds optimization opportunities on the MIR.
74
71
struct ConstPropagator < ' mir , ' tcx > {
75
- ecx : InterpCx < ' mir , ' tcx , ConstPropMachine < ' mir , ' tcx > > ,
72
+ ecx : InterpCx < ' mir , ' tcx , ConstPropMachine > ,
76
73
tcx : TyCtxt < ' tcx > ,
77
74
param_env : ParamEnv < ' tcx > ,
78
75
worklist : Vec < BasicBlock > ,
79
76
visited_blocks : BitSet < BasicBlock > ,
80
77
locals : IndexVec < Local , Value < ' tcx > > ,
78
+ body : & ' mir Body < ' tcx > ,
81
79
}
82
80
83
81
#[ derive( Debug , Clone ) ]
@@ -180,43 +178,29 @@ impl<'tcx> ty::layout::HasParamEnv<'tcx> for ConstPropagator<'_, 'tcx> {
180
178
impl < ' mir , ' tcx > ConstPropagator < ' mir , ' tcx > {
181
179
fn new ( body : & ' mir Body < ' tcx > , tcx : TyCtxt < ' tcx > ) -> ConstPropagator < ' mir , ' tcx > {
182
180
let def_id = body. source . def_id ( ) ;
183
- let args = & GenericArgs :: identity_for_item ( tcx, def_id) ;
184
181
let param_env = tcx. param_env_reveal_all_normalized ( def_id) ;
185
182
186
183
let can_const_prop = CanConstProp :: check ( tcx, param_env, body) ;
187
- let mut ecx = InterpCx :: new (
184
+ let ecx = InterpCx :: new (
188
185
tcx,
189
186
tcx. def_span ( def_id) ,
190
187
param_env,
191
188
ConstPropMachine :: new ( can_const_prop) ,
192
189
) ;
193
190
194
- let ret = MPlaceTy :: fake_alloc_zst ( ecx. layout_of ( tcx. types . unit ) . unwrap ( ) ) . into ( ) ;
195
-
196
- ecx. push_stack_frame (
197
- Instance :: new ( def_id, args) ,
198
- body,
199
- & ret,
200
- StackPopCleanup :: Root { cleanup : false } ,
201
- )
202
- . expect ( "failed to push initial stack frame" ) ;
203
-
204
191
ConstPropagator {
205
192
ecx,
206
193
tcx,
207
194
param_env,
208
195
worklist : vec ! [ START_BLOCK ] ,
209
196
visited_blocks : BitSet :: new_empty ( body. basic_blocks . len ( ) ) ,
210
197
locals : IndexVec :: from_elem_n ( Value :: Uninit , body. local_decls . len ( ) ) ,
198
+ body,
211
199
}
212
200
}
213
201
214
- fn body ( & self ) -> & ' mir Body < ' tcx > {
215
- self . ecx . frame ( ) . body
216
- }
217
-
218
202
fn local_decls ( & self ) -> & ' mir LocalDecls < ' tcx > {
219
- & self . body ( ) . local_decls
203
+ & self . body . local_decls
220
204
}
221
205
222
206
fn get_const ( & self , place : Place < ' tcx > ) -> Option < & Value < ' tcx > > {
@@ -243,7 +227,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
243
227
}
244
228
245
229
fn lint_root ( & self , source_info : SourceInfo ) -> Option < HirId > {
246
- source_info. scope . lint_root ( & self . body ( ) . source_scopes )
230
+ source_info. scope . lint_root ( & self . body . source_scopes )
247
231
}
248
232
249
233
fn use_ecx < F , T > ( & mut self , f : F ) -> Option < T >
@@ -332,7 +316,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
332
316
// `AssertKind` only has an `OverflowNeg` variant, so make sure that is
333
317
// appropriate to use.
334
318
assert_eq ! ( op, UnOp :: Neg , "Neg is the only UnOp that can overflow" ) ;
335
- let source_info = self . body ( ) . source_info ( location) ;
319
+ let source_info = self . body . source_info ( location) ;
336
320
self . report_assert_as_lint (
337
321
source_info,
338
322
AssertLint :: ArithmeticOverflow (
@@ -370,7 +354,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
370
354
let r_bits = r. to_scalar ( ) . to_bits ( right_size) . ok ( ) ;
371
355
if r_bits. is_some_and ( |b| b >= left_size. bits ( ) as u128 ) {
372
356
debug ! ( "check_binary_op: reporting assert for {:?}" , location) ;
373
- let source_info = self . body ( ) . source_info ( location) ;
357
+ let source_info = self . body . source_info ( location) ;
374
358
let panic = AssertKind :: Overflow (
375
359
op,
376
360
match l {
@@ -398,7 +382,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
398
382
let ( _res, overflow) = this. ecx . overflowing_binary_op ( op, & l, & r) ?;
399
383
Ok ( overflow)
400
384
} ) ? {
401
- let source_info = self . body ( ) . source_info ( location) ;
385
+ let source_info = self . body . source_info ( location) ;
402
386
self . report_assert_as_lint (
403
387
source_info,
404
388
AssertLint :: ArithmeticOverflow (
@@ -545,7 +529,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
545
529
// Need proper const propagator for these.
546
530
_ => return None ,
547
531
} ;
548
- let source_info = self . body ( ) . source_info ( location) ;
532
+ let source_info = self . body . source_info ( location) ;
549
533
self . report_assert_as_lint (
550
534
source_info,
551
535
AssertLint :: UnconditionalPanic ( source_info. span , msg) ,
@@ -564,7 +548,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
564
548
. layout_of( self . local_decls( ) [ local] . ty)
565
549
. map_or( true , |layout| layout. is_zst( ) ) ,
566
550
"failed to remove values for `{local:?}`, value={val:?}: {:#?}" ,
567
- self . body( ) ,
551
+ self . body,
568
552
)
569
553
}
570
554
}
@@ -580,7 +564,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
580
564
return None ;
581
565
}
582
566
use rustc_middle:: mir:: Rvalue :: * ;
583
- let layout = self . use_ecx ( |this| this . ecx . eval_place ( * dest) ) ? . layout ;
567
+ let layout = self . ecx . layout_of ( dest. ty ( self . body , self . tcx ) . ty ) . ok ( ) ? ;
584
568
trace ! ( ?layout) ;
585
569
586
570
let val: Value < ' _ > = match * rvalue {
0 commit comments