33
44use std:: fmt:: Debug ;
55
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 } ;
88use rustc_hir:: def:: DefKind ;
99use rustc_hir:: HirId ;
1010use rustc_index:: bit_set:: BitSet ;
1111use rustc_index:: { Idx , IndexVec } ;
1212use rustc_middle:: mir:: visit:: Visitor ;
1313use rustc_middle:: mir:: * ;
1414use 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 } ;
1916use rustc_span:: Span ;
2017use rustc_target:: abi:: { Abi , FieldIdx , HasDataLayout , Size , TargetDataLayout , VariantIdx } ;
2118
@@ -72,12 +69,13 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
7269
7370/// Finds optimization opportunities on the MIR.
7471struct ConstPropagator < ' mir , ' tcx > {
75- ecx : InterpCx < ' mir , ' tcx , ConstPropMachine < ' mir , ' tcx > > ,
72+ ecx : InterpCx < ' mir , ' tcx , ConstPropMachine > ,
7673 tcx : TyCtxt < ' tcx > ,
7774 param_env : ParamEnv < ' tcx > ,
7875 worklist : Vec < BasicBlock > ,
7976 visited_blocks : BitSet < BasicBlock > ,
8077 locals : IndexVec < Local , Value < ' tcx > > ,
78+ body : & ' mir Body < ' tcx > ,
8179}
8280
8381#[ derive( Debug , Clone ) ]
@@ -180,43 +178,29 @@ impl<'tcx> ty::layout::HasParamEnv<'tcx> for ConstPropagator<'_, 'tcx> {
180178impl < ' mir , ' tcx > ConstPropagator < ' mir , ' tcx > {
181179 fn new ( body : & ' mir Body < ' tcx > , tcx : TyCtxt < ' tcx > ) -> ConstPropagator < ' mir , ' tcx > {
182180 let def_id = body. source . def_id ( ) ;
183- let args = & GenericArgs :: identity_for_item ( tcx, def_id) ;
184181 let param_env = tcx. param_env_reveal_all_normalized ( def_id) ;
185182
186183 let can_const_prop = CanConstProp :: check ( tcx, param_env, body) ;
187- let mut ecx = InterpCx :: new (
184+ let ecx = InterpCx :: new (
188185 tcx,
189186 tcx. def_span ( def_id) ,
190187 param_env,
191188 ConstPropMachine :: new ( can_const_prop) ,
192189 ) ;
193190
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-
204191 ConstPropagator {
205192 ecx,
206193 tcx,
207194 param_env,
208195 worklist : vec ! [ START_BLOCK ] ,
209196 visited_blocks : BitSet :: new_empty ( body. basic_blocks . len ( ) ) ,
210197 locals : IndexVec :: from_elem_n ( Value :: Uninit , body. local_decls . len ( ) ) ,
198+ body,
211199 }
212200 }
213201
214- fn body ( & self ) -> & ' mir Body < ' tcx > {
215- self . ecx . frame ( ) . body
216- }
217-
218202 fn local_decls ( & self ) -> & ' mir LocalDecls < ' tcx > {
219- & self . body ( ) . local_decls
203+ & self . body . local_decls
220204 }
221205
222206 fn get_const ( & self , place : Place < ' tcx > ) -> Option < & Value < ' tcx > > {
@@ -243,7 +227,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
243227 }
244228
245229 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 )
247231 }
248232
249233 fn use_ecx < F , T > ( & mut self , f : F ) -> Option < T >
@@ -332,7 +316,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
332316 // `AssertKind` only has an `OverflowNeg` variant, so make sure that is
333317 // appropriate to use.
334318 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) ;
336320 self . report_assert_as_lint (
337321 source_info,
338322 AssertLint :: ArithmeticOverflow (
@@ -370,7 +354,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
370354 let r_bits = r. to_scalar ( ) . to_bits ( right_size) . ok ( ) ;
371355 if r_bits. is_some_and ( |b| b >= left_size. bits ( ) as u128 ) {
372356 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) ;
374358 let panic = AssertKind :: Overflow (
375359 op,
376360 match l {
@@ -398,7 +382,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
398382 let ( _res, overflow) = this. ecx . overflowing_binary_op ( op, & l, & r) ?;
399383 Ok ( overflow)
400384 } ) ? {
401- let source_info = self . body ( ) . source_info ( location) ;
385+ let source_info = self . body . source_info ( location) ;
402386 self . report_assert_as_lint (
403387 source_info,
404388 AssertLint :: ArithmeticOverflow (
@@ -545,7 +529,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
545529 // Need proper const propagator for these.
546530 _ => return None ,
547531 } ;
548- let source_info = self . body ( ) . source_info ( location) ;
532+ let source_info = self . body . source_info ( location) ;
549533 self . report_assert_as_lint (
550534 source_info,
551535 AssertLint :: UnconditionalPanic ( source_info. span , msg) ,
@@ -564,7 +548,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
564548 . layout_of( self . local_decls( ) [ local] . ty)
565549 . map_or( true , |layout| layout. is_zst( ) ) ,
566550 "failed to remove values for `{local:?}`, value={val:?}: {:#?}" ,
567- self . body( ) ,
551+ self . body,
568552 )
569553 }
570554 }
@@ -580,7 +564,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
580564 return None ;
581565 }
582566 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 ( ) ? ;
584568 trace ! ( ?layout) ;
585569
586570 let val: Value < ' _ > = match * rvalue {
0 commit comments