@@ -404,6 +404,18 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
404404 }
405405}
406406
407+ pub fn remove_unused_definitions < ' tcx > ( body : & mut Body < ' tcx > ) {
408+ // First, we're going to get a count of *actual* uses for every `Local`.
409+ let mut used_locals = UsedLocals :: new ( body) ;
410+
411+ // Next, we're going to remove any `Local` with zero actual uses. When we remove those
412+ // `Locals`, we're also going to subtract any uses of other `Locals` from the `used_locals`
413+ // count. For example, if we removed `_2 = discriminant(_1)`, then we'll subtract one from
414+ // `use_counts[_1]`. That in turn might make `_1` unused, so we loop until we hit a
415+ // fixedpoint where there are no more unused locals.
416+ remove_unused_definitions_helper ( & mut used_locals, body) ;
417+ }
418+
407419pub fn simplify_locals < ' tcx > ( body : & mut Body < ' tcx > , tcx : TyCtxt < ' tcx > ) {
408420 // First, we're going to get a count of *actual* uses for every `Local`.
409421 let mut used_locals = UsedLocals :: new ( body) ;
@@ -413,7 +425,7 @@ pub fn simplify_locals<'tcx>(body: &mut Body<'tcx>, tcx: TyCtxt<'tcx>) {
413425 // count. For example, if we removed `_2 = discriminant(_1)`, then we'll subtract one from
414426 // `use_counts[_1]`. That in turn might make `_1` unused, so we loop until we hit a
415427 // fixedpoint where there are no more unused locals.
416- remove_unused_definitions ( & mut used_locals, body) ;
428+ remove_unused_definitions_helper ( & mut used_locals, body) ;
417429
418430 // Finally, we'll actually do the work of shrinking `body.local_decls` and remapping the `Local`s.
419431 let map = make_local_map ( & mut body. local_decls , & used_locals) ;
@@ -548,7 +560,7 @@ impl<'tcx> Visitor<'tcx> for UsedLocals {
548560}
549561
550562/// Removes unused definitions. Updates the used locals to reflect the changes made.
551- fn remove_unused_definitions ( used_locals : & mut UsedLocals , body : & mut Body < ' _ > ) {
563+ fn remove_unused_definitions_helper ( used_locals : & mut UsedLocals , body : & mut Body < ' _ > ) {
552564 // The use counts are updated as we remove the statements. A local might become unused
553565 // during the retain operation, leading to a temporary inconsistency (storage statements or
554566 // definitions referencing the local might remain). For correctness it is crucial that this
0 commit comments