@@ -766,6 +766,8 @@ struct CanConstProp {
766
766
can_const_prop : IndexVec < Local , ConstPropMode > ,
767
767
// false at the beginning, once set, there are not allowed to be any more assignments
768
768
found_assignment : BitSet < Local > ,
769
+ // Cache of locals' information
770
+ local_kinds : IndexVec < Local , LocalKind > ,
769
771
}
770
772
771
773
impl CanConstProp {
@@ -774,16 +776,19 @@ impl CanConstProp {
774
776
let mut cpv = CanConstProp {
775
777
can_const_prop : IndexVec :: from_elem ( ConstPropMode :: FullConstProp , & body. local_decls ) ,
776
778
found_assignment : BitSet :: new_empty ( body. local_decls . len ( ) ) ,
779
+ local_kinds : IndexVec :: from_fn_n (
780
+ |local| body. local_kind ( local) ,
781
+ body. local_decls . len ( ) ,
782
+ ) ,
777
783
} ;
778
784
for ( local, val) in cpv. can_const_prop . iter_enumerated_mut ( ) {
779
785
// cannot use args at all
780
786
// cannot use locals because if x < y { y - x } else { x - y } would
781
787
// lint for x != y
782
788
// FIXME(oli-obk): lint variables until they are used in a condition
783
789
// FIXME(oli-obk): lint if return value is constant
784
- let local_kind = body. local_kind ( local) ;
785
-
786
- if local_kind == LocalKind :: Arg || local_kind == LocalKind :: Var {
790
+ if cpv. local_kinds [ local] == LocalKind :: Arg || cpv. local_kinds [ local] == LocalKind :: Var
791
+ {
787
792
* val = ConstPropMode :: OnlyPropagateInto ;
788
793
trace ! ( "local {:?} can't be const propagated because it's not a temporary" , local) ;
789
794
}
@@ -811,8 +816,12 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
811
816
| NonMutatingUse ( NonMutatingUseContext :: Move )
812
817
| NonMutatingUse ( NonMutatingUseContext :: Inspect )
813
818
| NonMutatingUse ( NonMutatingUseContext :: Projection )
814
- | MutatingUse ( MutatingUseContext :: Projection )
815
819
| NonUse ( _) => { }
820
+ MutatingUse ( MutatingUseContext :: Projection ) => {
821
+ if self . local_kinds [ local] != LocalKind :: Temp {
822
+ self . can_const_prop [ local] = ConstPropMode :: NoPropagation ;
823
+ }
824
+ }
816
825
_ => {
817
826
trace ! ( "local {:?} can't be propagaged because it's used: {:?}" , local, context) ;
818
827
self . can_const_prop [ local] = ConstPropMode :: NoPropagation ;
0 commit comments