Skip to content

Commit 370251c

Browse files
committed
Remove StorageDead and StorageLive statements using MIR locals of type ()
1 parent f4c675c commit 370251c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/librustc_mir/transform/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod no_landing_pads;
3030
pub mod promote_consts;
3131
pub mod qualify_min_const_fn;
3232
pub mod remove_noop_landing_pads;
33+
pub mod remove_unit_storage;
3334
pub mod rustc_peek;
3435
pub mod simplify;
3536
pub mod simplify_branches;
@@ -299,6 +300,7 @@ fn run_optimization_passes<'tcx>(
299300
// From here on out, regions are gone.
300301
&erase_regions::EraseRegions,
301302
// Optimizations begin.
303+
&remove_unit_storage::RemoveUnitStorage,
302304
&unreachable_prop::UnreachablePropagation,
303305
&uninhabited_enum_branching::UninhabitedEnumBranching,
304306
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! The `RemoveUnitStorage` pass removes `StorageLive` and `StorageDead` statements
2+
//! which operates on locals of type `()`.
3+
4+
use crate::transform::{MirPass, MirSource};
5+
use rustc::mir::*;
6+
use rustc::ty::TyCtxt;
7+
8+
pub struct RemoveUnitStorage;
9+
10+
impl<'tcx> MirPass<'tcx> for RemoveUnitStorage {
11+
fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
12+
// This pass removes UB, so only run it when optimizations are enabled.
13+
if tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
14+
return;
15+
}
16+
17+
let (blocks, locals) = body.basic_blocks_and_local_decls_mut();
18+
19+
for block in blocks {
20+
for stmt in &mut block.statements {
21+
if let StatementKind::StorageLive(l) | StatementKind::StorageDead(l) = stmt.kind {
22+
if locals[l].ty == tcx.types.unit {
23+
stmt.make_nop();
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)