Skip to content

Commit 444ad62

Browse files
Add utility to find locals that don't use Storage* annotations
1 parent 93dc97a commit 444ad62

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/librustc_mir/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod borrowck_errors;
33
pub mod def_use;
44
pub mod elaborate_drops;
55
pub mod patch;
6+
pub mod storage;
67

78
mod alignment;
89
pub mod collect_writes;

src/librustc_mir/util/storage.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use rustc_index::bit_set::BitSet;
2+
use rustc_middle::mir::{self, Local};
3+
4+
/// The set of locals in a MIR body that do not have `StorageLive`/`StorageDead` annotations.
5+
///
6+
/// These locals have fixed storage for the duration of the body.
7+
//
8+
// FIXME: Currently, we need to traverse the entire MIR to compute this. We should instead store it
9+
// as a field in the `LocalDecl` for each `Local`.
10+
#[derive(Debug, Clone)]
11+
pub struct AlwaysLiveLocals(BitSet<Local>);
12+
13+
impl AlwaysLiveLocals {
14+
pub fn new(body: &mir::Body<'tcx>) -> Self {
15+
let mut locals = BitSet::new_filled(body.local_decls.len());
16+
17+
// FIXME: Use a visitor for this when `visit_body` can take a plain `Body`.
18+
for block in body.basic_blocks().iter() {
19+
for stmt in &block.statements {
20+
if let mir::StatementKind::StorageLive(l) | mir::StatementKind::StorageDead(l) =
21+
stmt.kind
22+
{
23+
locals.remove(l);
24+
}
25+
}
26+
}
27+
28+
AlwaysLiveLocals(locals)
29+
}
30+
31+
pub fn into_inner(self) -> BitSet<Local> {
32+
self.0
33+
}
34+
}
35+
36+
impl std::ops::Deref for AlwaysLiveLocals {
37+
type Target = BitSet<Local>;
38+
39+
fn deref(&self) -> &Self::Target {
40+
&self.0
41+
}
42+
}

0 commit comments

Comments
 (0)