@@ -28,7 +28,8 @@ impl<'tcx> MirPass<'tcx> for SingleUseConsts {
2828 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
2929 let mut finder = SingleUseConstsFinder {
3030 ineligible_locals : BitSet :: new_empty ( body. local_decls . len ( ) ) ,
31- locations : IndexVec :: new ( ) ,
31+ locations : IndexVec :: from_elem ( LocationPair :: new ( ) , & body. local_decls ) ,
32+ locals_in_debug_info : BitSet :: new_empty ( body. local_decls . len ( ) ) ,
3233 } ;
3334
3435 finder. ineligible_locals . insert_range ( ..=Local :: from_usize ( body. arg_count ) ) ;
@@ -57,8 +58,10 @@ impl<'tcx> MirPass<'tcx> for SingleUseConsts {
5758
5859 let mut replacer = LocalReplacer { tcx, local, operand : Some ( operand) } ;
5960
60- for var_debug_info in & mut body. var_debug_info {
61- replacer. visit_var_debug_info ( var_debug_info) ;
61+ if finder. locals_in_debug_info . contains ( local) {
62+ for var_debug_info in & mut body. var_debug_info {
63+ replacer. visit_var_debug_info ( var_debug_info) ;
64+ }
6265 }
6366
6467 let Some ( use_loc) = locations. use_loc else { continue } ;
@@ -94,6 +97,7 @@ impl LocationPair {
9497struct SingleUseConstsFinder {
9598 ineligible_locals : BitSet < Local > ,
9699 locations : IndexVec < Local , LocationPair > ,
100+ locals_in_debug_info : BitSet < Local > ,
97101}
98102
99103impl < ' tcx > Visitor < ' tcx > for SingleUseConstsFinder {
@@ -102,7 +106,7 @@ impl<'tcx> Visitor<'tcx> for SingleUseConstsFinder {
102106 && let Rvalue :: Use ( operand) = rvalue
103107 && let Operand :: Constant ( _) = operand
104108 {
105- let locations = self . locations . ensure_contains_elem ( local, LocationPair :: new ) ;
109+ let locations = & mut self . locations [ local] ;
106110 if locations. init_loc . is_some ( ) {
107111 self . ineligible_locals . insert ( local) ;
108112 } else {
@@ -117,7 +121,7 @@ impl<'tcx> Visitor<'tcx> for SingleUseConstsFinder {
117121 if let Some ( place) = operand. place ( )
118122 && let Some ( local) = place. as_local ( )
119123 {
120- let locations = self . locations . ensure_contains_elem ( local, LocationPair :: new ) ;
124+ let locations = & mut self . locations [ local] ;
121125 if locations. use_loc . is_some ( ) {
122126 self . ineligible_locals . insert ( local) ;
123127 } else {
@@ -138,9 +142,9 @@ impl<'tcx> Visitor<'tcx> for SingleUseConstsFinder {
138142
139143 fn visit_var_debug_info ( & mut self , var_debug_info : & VarDebugInfo < ' tcx > ) {
140144 if let VarDebugInfoContents :: Place ( place) = & var_debug_info. value
141- && let Some ( _local ) = place. as_local ( )
145+ && let Some ( local ) = place. as_local ( )
142146 {
143- // It's a simple one that we can easily update
147+ self . locals_in_debug_info . insert ( local ) ;
144148 } else {
145149 self . super_var_debug_info ( var_debug_info) ;
146150 }
0 commit comments