@@ -714,13 +714,22 @@ impl CanConstProp {
714
714
}
715
715
}
716
716
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
+
718
730
fn visit_local ( & mut self , local : Local , context : PlaceContext , _: Location ) {
719
731
use rustc_middle:: mir:: visit:: PlaceContext :: * ;
720
732
match context {
721
- // Projections are fine, because `&mut foo.x` will be caught by
722
- // `MutatingUseContext::Borrow` elsewhere.
723
- MutatingUse ( MutatingUseContext :: Projection )
724
733
// These are just stores, where the storing is not propagatable, but there may be later
725
734
// mutations of the same local via `Store`
726
735
| MutatingUse ( MutatingUseContext :: Call )
@@ -751,7 +760,6 @@ impl Visitor<'_> for CanConstProp {
751
760
NonMutatingUse ( NonMutatingUseContext :: Copy )
752
761
| NonMutatingUse ( NonMutatingUseContext :: Move )
753
762
| NonMutatingUse ( NonMutatingUseContext :: Inspect )
754
- | NonMutatingUse ( NonMutatingUseContext :: Projection )
755
763
| NonMutatingUse ( NonMutatingUseContext :: PlaceMention )
756
764
| NonUse ( _) => { }
757
765
@@ -771,6 +779,8 @@ impl Visitor<'_> for CanConstProp {
771
779
trace ! ( "local {:?} can't be propagated because it's used: {:?}" , local, context) ;
772
780
self . can_const_prop [ local] = ConstPropMode :: NoPropagation ;
773
781
}
782
+ MutatingUse ( MutatingUseContext :: Projection )
783
+ | NonMutatingUse ( NonMutatingUseContext :: Projection ) => bug ! ( "visit_place should not pass {context:?} for {local:?}" ) ,
774
784
}
775
785
}
776
786
}
0 commit comments