@@ -59,14 +59,22 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
5959 let storage_to_remove = if tcx. sess . emit_lifetime_markers ( ) {
6060 storage_to_remove. clear ( ) ;
6161
62+ // If the local is borrowed, we cannot easily determine if it is used, so we have to remove the storage statements.
63+ let borrowed_locals = ssa. borrowed_locals ( ) ;
64+
65+ for ( local, & head) in ssa. copy_classes ( ) . iter_enumerated ( ) {
66+ if local != head && borrowed_locals. contains ( local) {
67+ storage_to_remove. insert ( head) ;
68+ }
69+ }
70+
6271 let maybe_uninit = MaybeUninitializedLocals :: new ( )
6372 . iterate_to_fixpoint ( tcx, body, Some ( "mir_opt::copy_prop" ) )
6473 . into_results_cursor ( body) ;
6574
6675 let mut storage_checker = StorageChecker {
6776 maybe_uninit,
6877 copy_classes : ssa. copy_classes ( ) ,
69- borrowed_locals : ssa. borrowed_locals ( ) ,
7078 storage_to_remove,
7179 } ;
7280
@@ -193,7 +201,6 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
193201struct StorageChecker < ' a , ' tcx > {
194202 maybe_uninit : ResultsCursor < ' a , ' tcx , MaybeUninitializedLocals > ,
195203 copy_classes : & ' a IndexSlice < Local , Local > ,
196- borrowed_locals : & ' a DenseBitSet < Local > ,
197204 storage_to_remove : DenseBitSet < Local > ,
198205}
199206
@@ -220,12 +227,6 @@ impl<'a, 'tcx> Visitor<'tcx> for StorageChecker<'a, 'tcx> {
220227 return ;
221228 }
222229
223- // If the local is borrowed, we cannot easily determine if it is used, so we have to remove the storage statements.
224- if self . borrowed_locals . contains ( local) {
225- self . storage_to_remove . insert ( head) ;
226- return ;
227- }
228-
229230 self . maybe_uninit . seek_before_primary_effect ( loc) ;
230231
231232 if self . maybe_uninit . get ( ) . contains ( head) {
0 commit comments