@@ -714,13 +714,22 @@ impl CanConstProp {
714714 }
715715}
716716
717- impl Visitor < ' _ > for CanConstProp {
717+ impl < ' tcx > Visitor < ' tcx > for CanConstProp {
718+ fn visit_place ( & mut self , place : & Place < ' tcx > , mut context : PlaceContext , loc : Location ) {
719+ use rustc_middle:: mir:: visit:: PlaceContext :: * ;
720+
721+ // Dereferencing just read the addess of `place.local`.
722+ if place. projection . first ( ) == Some ( & PlaceElem :: Deref ) {
723+ context = NonMutatingUse ( NonMutatingUseContext :: Copy ) ;
724+ }
725+
726+ self . visit_local ( place. local , context, loc) ;
727+ self . visit_projection ( place. as_ref ( ) , context, loc) ;
728+ }
729+
718730 fn visit_local ( & mut self , local : Local , context : PlaceContext , _: Location ) {
719731 use rustc_middle:: mir:: visit:: PlaceContext :: * ;
720732 match context {
721- // Projections are fine, because `&mut foo.x` will be caught by
722- // `MutatingUseContext::Borrow` elsewhere.
723- MutatingUse ( MutatingUseContext :: Projection )
724733 // These are just stores, where the storing is not propagatable, but there may be later
725734 // mutations of the same local via `Store`
726735 | MutatingUse ( MutatingUseContext :: Call )
@@ -751,7 +760,6 @@ impl Visitor<'_> for CanConstProp {
751760 NonMutatingUse ( NonMutatingUseContext :: Copy )
752761 | NonMutatingUse ( NonMutatingUseContext :: Move )
753762 | NonMutatingUse ( NonMutatingUseContext :: Inspect )
754- | NonMutatingUse ( NonMutatingUseContext :: Projection )
755763 | NonMutatingUse ( NonMutatingUseContext :: PlaceMention )
756764 | NonUse ( _) => { }
757765
@@ -771,6 +779,8 @@ impl Visitor<'_> for CanConstProp {
771779 trace ! ( "local {:?} can't be propagated because it's used: {:?}" , local, context) ;
772780 self . can_const_prop [ local] = ConstPropMode :: NoPropagation ;
773781 }
782+ MutatingUse ( MutatingUseContext :: Projection )
783+ | NonMutatingUse ( NonMutatingUseContext :: Projection ) => bug ! ( "visit_place should not pass {context:?} for {local:?}" ) ,
774784 }
775785 }
776786}
0 commit comments