33
44use std:: fmt:: Debug ;
55
6- use either:: Left ;
7-
86use rustc_const_eval:: interpret:: { ImmTy , MPlaceTy , Projectable } ;
97use rustc_const_eval:: interpret:: { InterpCx , InterpResult , OpTy , Scalar , StackPopCleanup } ;
108use rustc_hir:: def:: DefKind ;
@@ -248,12 +246,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
248246 source_info. scope . lint_root ( & self . body ( ) . source_scopes )
249247 }
250248
251- fn use_ecx < F , T > ( & mut self , location : Location , f : F ) -> Option < T >
249+ fn use_ecx < F , T > ( & mut self , f : F ) -> Option < T >
252250 where
253251 F : FnOnce ( & mut Self ) -> InterpResult < ' tcx , T > ,
254252 {
255- // Overwrite the PC -- whatever the interpreter does to it does not make any sense anyway.
256- self . ecx . frame_mut ( ) . loc = Left ( location) ;
257253 match f ( self ) {
258254 Ok ( val) => Some ( val) ,
259255 Err ( error) => {
@@ -275,7 +271,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
275271 fn eval_constant (
276272 & mut self ,
277273 c : & ConstOperand < ' tcx > ,
278- location : Location ,
279274 layout : Option < TyAndLayout < ' tcx > > ,
280275 ) -> Option < OpTy < ' tcx > > {
281276 // FIXME we need to revisit this for #67176
@@ -291,7 +286,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
291286 // manually normalized.
292287 let val = self . tcx . try_normalize_erasing_regions ( self . param_env , c. const_ ) . ok ( ) ?;
293288
294- self . use_ecx ( location , |this| this. ecx . eval_mir_constant ( & val, Some ( c. span ) , layout) )
289+ self . use_ecx ( |this| this. ecx . eval_mir_constant ( & val, Some ( c. span ) , layout) )
295290 }
296291
297292 /// Returns the value, if any, of evaluating `place`.
@@ -313,11 +308,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
313308 fn eval_operand (
314309 & mut self ,
315310 op : & Operand < ' tcx > ,
316- location : Location ,
317311 layout : Option < TyAndLayout < ' tcx > > ,
318312 ) -> Option < OpTy < ' tcx > > {
319313 match * op {
320- Operand :: Constant ( ref c) => self . eval_constant ( c, location , layout) ,
314+ Operand :: Constant ( ref c) => self . eval_constant ( c, layout) ,
321315 Operand :: Move ( place) | Operand :: Copy ( place) => self . eval_place ( place, layout) ,
322316 }
323317 }
@@ -329,8 +323,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
329323 }
330324
331325 fn check_unary_op ( & mut self , op : UnOp , arg : & Operand < ' tcx > , location : Location ) -> Option < ( ) > {
332- let arg = self . eval_operand ( arg, location , None ) ?;
333- if let ( val, true ) = self . use_ecx ( location , |this| {
326+ let arg = self . eval_operand ( arg, None ) ?;
327+ if let ( val, true ) = self . use_ecx ( |this| {
334328 let val = this. ecx . read_immediate ( & arg) ?;
335329 let ( _res, overflow) = this. ecx . overflowing_unary_op ( op, & val) ?;
336330 Ok ( ( val, overflow) )
@@ -360,11 +354,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
360354 location : Location ,
361355 ) -> Option < ( ) > {
362356 let r = self
363- . eval_operand ( right, location , None )
364- . and_then ( |r| self . use_ecx ( location , |this| this. ecx . read_immediate ( & r) ) ) ;
357+ . eval_operand ( right, None )
358+ . and_then ( |r| self . use_ecx ( |this| this. ecx . read_immediate ( & r) ) ) ;
365359 let l = self
366- . eval_operand ( left, location , None )
367- . and_then ( |l| self . use_ecx ( location , |this| this. ecx . read_immediate ( & l) ) ) ;
360+ . eval_operand ( left, None )
361+ . and_then ( |l| self . use_ecx ( |this| this. ecx . read_immediate ( & l) ) ) ;
368362 // Check for exceeding shifts *even if* we cannot evaluate the LHS.
369363 if matches ! ( op, BinOp :: Shr | BinOp :: Shl ) {
370364 let r = r. clone ( ) ?;
@@ -400,7 +394,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
400394
401395 if let ( Some ( l) , Some ( r) ) = ( l, r) {
402396 // The remaining operators are handled through `overflowing_binary_op`.
403- if self . use_ecx ( location , |this| {
397+ if self . use_ecx ( |this| {
404398 let ( _res, overflow) = this. ecx . overflowing_binary_op ( op, & l, & r) ?;
405399 Ok ( overflow)
406400 } ) ? {
@@ -501,11 +495,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
501495 cond : & Operand < ' tcx > ,
502496 location : Location ,
503497 ) -> Option < !> {
504- let value = & self . eval_operand ( cond, location , None ) ?;
498+ let value = & self . eval_operand ( cond, None ) ?;
505499 trace ! ( "assertion on {:?} should be {:?}" , value, expected) ;
506500
507501 let expected = Scalar :: from_bool ( expected) ;
508- let value_const = self . use_ecx ( location , |this| this. ecx . read_scalar ( value) ) ?;
502+ let value_const = self . use_ecx ( |this| this. ecx . read_scalar ( value) ) ?;
509503
510504 if expected != value_const {
511505 // Poison all places this operand references so that further code
@@ -529,7 +523,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
529523 let mut eval_to_int = |op| {
530524 // This can be `None` if the lhs wasn't const propagated and we just
531525 // triggered the assert on the value of the rhs.
532- self . eval_operand ( op, location , None )
526+ self . eval_operand ( op, None )
533527 . and_then ( |op| self . ecx . read_immediate ( & op) . ok ( ) )
534528 . map_or ( DbgVal :: Underscore , |op| DbgVal :: Val ( op. to_const_int ( ) ) )
535529 } ;
@@ -585,44 +579,43 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
585579 return None ;
586580 }
587581 use rustc_middle:: mir:: Rvalue :: * ;
588- let layout = self . use_ecx ( location , |this| this. ecx . eval_place ( * dest) ) ?. layout ;
582+ let layout = self . use_ecx ( |this| this. ecx . eval_place ( * dest) ) ?. layout ;
589583 trace ! ( ?layout) ;
590584
591585 let val: Value < ' _ > = match * rvalue {
592586 ThreadLocalRef ( _) => return None ,
593587
594- Use ( ref operand) => self . eval_operand ( operand, location , Some ( layout) ) ?. into ( ) ,
588+ Use ( ref operand) => self . eval_operand ( operand, Some ( layout) ) ?. into ( ) ,
595589
596590 CopyForDeref ( place) => self . eval_place ( place, Some ( layout) ) ?. into ( ) ,
597591
598592 BinaryOp ( bin_op, box ( ref left, ref right) ) => {
599593 let layout =
600594 rustc_const_eval:: util:: binop_left_homogeneous ( bin_op) . then_some ( layout) ;
601- let left = self . eval_operand ( left, location , layout) ?;
602- let left = self . use_ecx ( location , |this| this. ecx . read_immediate ( & left) ) ?;
595+ let left = self . eval_operand ( left, layout) ?;
596+ let left = self . use_ecx ( |this| this. ecx . read_immediate ( & left) ) ?;
603597
604598 let layout =
605599 rustc_const_eval:: util:: binop_right_homogeneous ( bin_op) . then_some ( left. layout ) ;
606- let right = self . eval_operand ( right, location , layout) ?;
607- let right = self . use_ecx ( location , |this| this. ecx . read_immediate ( & right) ) ?;
600+ let right = self . eval_operand ( right, layout) ?;
601+ let right = self . use_ecx ( |this| this. ecx . read_immediate ( & right) ) ?;
608602
609- let val = self
610- . use_ecx ( location , |this| this. ecx . wrapping_binary_op ( bin_op, & left, & right) ) ?;
603+ let val =
604+ self . use_ecx ( |this| this. ecx . wrapping_binary_op ( bin_op, & left, & right) ) ?;
611605 val. into ( )
612606 }
613607
614608 CheckedBinaryOp ( bin_op, box ( ref left, ref right) ) => {
615- let left = self . eval_operand ( left, location , None ) ?;
616- let left = self . use_ecx ( location , |this| this. ecx . read_immediate ( & left) ) ?;
609+ let left = self . eval_operand ( left, None ) ?;
610+ let left = self . use_ecx ( |this| this. ecx . read_immediate ( & left) ) ?;
617611
618612 let layout =
619613 rustc_const_eval:: util:: binop_right_homogeneous ( bin_op) . then_some ( left. layout ) ;
620- let right = self . eval_operand ( right, location , layout) ?;
621- let right = self . use_ecx ( location , |this| this. ecx . read_immediate ( & right) ) ?;
614+ let right = self . eval_operand ( right, layout) ?;
615+ let right = self . use_ecx ( |this| this. ecx . read_immediate ( & right) ) ?;
622616
623- let ( val, overflowed) = self . use_ecx ( location, |this| {
624- this. ecx . overflowing_binary_op ( bin_op, & left, & right)
625- } ) ?;
617+ let ( val, overflowed) =
618+ self . use_ecx ( |this| this. ecx . overflowing_binary_op ( bin_op, & left, & right) ) ?;
626619 let overflowed = ImmTy :: from_bool ( overflowed, self . tcx ) ;
627620 Value :: Aggregate {
628621 variant : VariantIdx :: new ( 0 ) ,
@@ -631,19 +624,18 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
631624 }
632625
633626 UnaryOp ( un_op, ref operand) => {
634- let operand = self . eval_operand ( operand, location , Some ( layout) ) ?;
635- let val = self . use_ecx ( location , |this| this. ecx . read_immediate ( & operand) ) ?;
627+ let operand = self . eval_operand ( operand, Some ( layout) ) ?;
628+ let val = self . use_ecx ( |this| this. ecx . read_immediate ( & operand) ) ?;
636629
637- let val = self . use_ecx ( location , |this| this. ecx . wrapping_unary_op ( un_op, & val) ) ?;
630+ let val = self . use_ecx ( |this| this. ecx . wrapping_unary_op ( un_op, & val) ) ?;
638631 val. into ( )
639632 }
640633
641634 Aggregate ( ref kind, ref fields) => Value :: Aggregate {
642635 fields : fields
643636 . iter ( )
644637 . map ( |field| {
645- self . eval_operand ( field, location, None )
646- . map_or ( Value :: Uninit , Value :: Immediate )
638+ self . eval_operand ( field, None ) . map_or ( Value :: Uninit , Value :: Immediate )
647639 } )
648640 . collect ( ) ,
649641 variant : match * * kind {
@@ -675,7 +667,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
675667 Ref ( ..) | AddressOf ( ..) => return None ,
676668
677669 NullaryOp ( ref null_op, ty) => {
678- let op_layout = self . use_ecx ( location , |this| this. ecx . layout_of ( ty) ) ?;
670+ let op_layout = self . use_ecx ( |this| this. ecx . layout_of ( ty) ) ?;
679671 let val = match null_op {
680672 NullOp :: SizeOf => op_layout. size . bytes ( ) ,
681673 NullOp :: AlignOf => op_layout. align . abi . bytes ( ) ,
@@ -690,21 +682,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
690682
691683 Cast ( ref kind, ref value, to) => match kind {
692684 CastKind :: IntToInt | CastKind :: IntToFloat => {
693- let value = self . eval_operand ( value, location , None ) ?;
685+ let value = self . eval_operand ( value, None ) ?;
694686 let value = self . ecx . read_immediate ( & value) . ok ( ) ?;
695687 let to = self . ecx . layout_of ( to) . ok ( ) ?;
696688 let res = self . ecx . int_to_int_or_float ( & value, to) . ok ( ) ?;
697689 res. into ( )
698690 }
699691 CastKind :: FloatToFloat | CastKind :: FloatToInt => {
700- let value = self . eval_operand ( value, location , None ) ?;
692+ let value = self . eval_operand ( value, None ) ?;
701693 let value = self . ecx . read_immediate ( & value) . ok ( ) ?;
702694 let to = self . ecx . layout_of ( to) . ok ( ) ?;
703695 let res = self . ecx . float_to_float_or_int ( & value, to) . ok ( ) ?;
704696 res. into ( )
705697 }
706698 CastKind :: Transmute => {
707- let value = self . eval_operand ( value, location , None ) ?;
699+ let value = self . eval_operand ( value, None ) ?;
708700 let to = self . ecx . layout_of ( to) . ok ( ) ?;
709701 // `offset` for immediates only supports scalar/scalar-pair ABIs,
710702 // so bail out if the target is not one.
@@ -724,12 +716,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
724716 let variant = match self . get_const ( place) ? {
725717 Value :: Immediate ( op) => {
726718 let op = op. clone ( ) ;
727- self . use_ecx ( location , |this| this. ecx . read_discriminant ( & op) ) ?
719+ self . use_ecx ( |this| this. ecx . read_discriminant ( & op) ) ?
728720 }
729721 Value :: Aggregate { variant, .. } => * variant,
730722 Value :: Uninit => return None ,
731723 } ;
732- let imm = self . use_ecx ( location , |this| {
724+ let imm = self . use_ecx ( |this| {
733725 this. ecx . discriminant_for_variant (
734726 place. ty ( this. local_decls ( ) , this. tcx ) . ty ,
735727 variant,
@@ -791,7 +783,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
791783 fn visit_constant ( & mut self , constant : & ConstOperand < ' tcx > , location : Location ) {
792784 trace ! ( "visit_constant: {:?}" , constant) ;
793785 self . super_constant ( constant, location) ;
794- self . eval_constant ( constant, location , None ) ;
786+ self . eval_constant ( constant, None ) ;
795787 }
796788
797789 fn visit_assign ( & mut self , place : & Place < ' tcx > , rvalue : & Rvalue < ' tcx > , location : Location ) {
@@ -864,9 +856,8 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
864856 self . check_assertion ( * expected, msg, cond, location) ;
865857 }
866858 TerminatorKind :: SwitchInt { ref discr, ref targets } => {
867- if let Some ( ref value) = self . eval_operand ( discr, location, None )
868- && let Some ( value_const) =
869- self . use_ecx ( location, |this| this. ecx . read_scalar ( value) )
859+ if let Some ( ref value) = self . eval_operand ( discr, None )
860+ && let Some ( value_const) = self . use_ecx ( |this| this. ecx . read_scalar ( value) )
870861 && let Ok ( constant) = value_const. try_to_int ( )
871862 && let Ok ( constant) = constant. to_bits ( constant. size ( ) )
872863 {
0 commit comments