Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Remove StorageDead and StorageLive statements using MIR locals of type () #70004

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/librustc_mir/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod no_landing_pads;
pub mod promote_consts;
pub mod qualify_min_const_fn;
pub mod remove_noop_landing_pads;
pub mod remove_unit_storage;
pub mod rustc_peek;
pub mod simplify;
pub mod simplify_branches;
Expand Down Expand Up @@ -299,6 +300,7 @@ fn run_optimization_passes<'tcx>(
// From here on out, regions are gone.
&erase_regions::EraseRegions,
// Optimizations begin.
&remove_unit_storage::RemoveUnitStorage,
&unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching,
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
Expand Down
29 changes: 29 additions & 0 deletions src/librustc_mir/transform/remove_unit_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! The `RemoveUnitStorage` pass removes `StorageLive` and `StorageDead` statements
//! which operates on locals of type `()`.

use crate::transform::{MirPass, MirSource};
use rustc::mir::*;
use rustc::ty::TyCtxt;

pub struct RemoveUnitStorage;

impl<'tcx> MirPass<'tcx> for RemoveUnitStorage {
fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
// This pass removes UB, so only run it when optimizations are enabled.
if tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
return;
}

let (blocks, locals) = body.basic_blocks_and_local_decls_mut();

for block in blocks {
for stmt in &mut block.statements {
if let StatementKind::StorageLive(l) | StatementKind::StorageDead(l) = stmt.kind {
if locals[l].ty == tcx.types.unit {
stmt.make_nop();
}
}
}
}
}
}
5 changes: 4 additions & 1 deletion src/test/ui/consts/dangling-alloc-id-ice.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// run-pass
// https://github.com/rust-lang/rust/issues/55223

union Foo<'a> {
//~^ WARN union is never used
y: &'a (),
long_live_the_unit: &'static (),
}

const FOO: &() = { //~ ERROR any use of this value will cause an error
const FOO: &() = {
//~^ WARN constant item is never used
let y = ();
unsafe { Foo { y: &y }.long_live_the_unit }
};
Expand Down
19 changes: 12 additions & 7 deletions src/test/ui/consts/dangling-alloc-id-ice.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
error: any use of this value will cause an error
--> $DIR/dangling-alloc-id-ice.rs:8:1
warning: union is never used: `Foo`
--> $DIR/dangling-alloc-id-ice.rs:4:7
|
LL | union Foo<'a> {
| ^^^
|
= note: `#[warn(dead_code)]` on by default

warning: constant item is never used: `FOO`
--> $DIR/dangling-alloc-id-ice.rs:10:1
|
LL | / const FOO: &() = {
LL | |
LL | | let y = ();
LL | | unsafe { Foo { y: &y }.long_live_the_unit }
LL | | };
| |__^ encountered dangling pointer in final constant
|
= note: `#[deny(const_err)]` on by default

error: aborting due to previous error
| |__^