@@ -404,6 +404,18 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
404
404
}
405
405
}
406
406
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
+
407
419
pub fn simplify_locals < ' tcx > ( body : & mut Body < ' tcx > , tcx : TyCtxt < ' tcx > ) {
408
420
// First, we're going to get a count of *actual* uses for every `Local`.
409
421
let mut used_locals = UsedLocals :: new ( body) ;
@@ -413,7 +425,7 @@ pub fn simplify_locals<'tcx>(body: &mut Body<'tcx>, tcx: TyCtxt<'tcx>) {
413
425
// count. For example, if we removed `_2 = discriminant(_1)`, then we'll subtract one from
414
426
// `use_counts[_1]`. That in turn might make `_1` unused, so we loop until we hit a
415
427
// 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) ;
417
429
418
430
// Finally, we'll actually do the work of shrinking `body.local_decls` and remapping the `Local`s.
419
431
let map = make_local_map ( & mut body. local_decls , & used_locals) ;
@@ -548,7 +560,7 @@ impl<'tcx> Visitor<'tcx> for UsedLocals {
548
560
}
549
561
550
562
/// 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 < ' _ > ) {
552
564
// The use counts are updated as we remove the statements. A local might become unused
553
565
// during the retain operation, leading to a temporary inconsistency (storage statements or
554
566
// definitions referencing the local might remain). For correctness it is crucial that this
0 commit comments