Skip to content

Commit 0a19371

Browse files
committed
Add predecessors fn to ReadOnlyBodyCache, fix more Body -> (ReadOnly)BodyCache type errors
1 parent 26f1c01 commit 0a19371

File tree

10 files changed

+52
-47
lines changed

10 files changed

+52
-47
lines changed

src/librustc/mir/cache.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct Cache {
1414
predecessors: Option<IndexVec<BasicBlock, Vec<BasicBlock>>>,
1515
}
1616

17-
1817
//impl<'tcx, T> rustc_serialize::Encodable for Cache<'tcx, T> {
1918
// fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
2019
// Encodable::encode(&(), s)
@@ -261,6 +260,11 @@ pub struct ReadOnlyBodyCache<'a, 'tcx> {
261260
}
262261

263262
impl ReadOnlyBodyCache<'a, 'tcx> {
263+
#[inline]
264+
pub fn predecessors(&self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
265+
self.cache.predecessors.as_ref().unwrap()
266+
}
267+
264268
#[inline]
265269
pub fn predecessors_for(&self, bb: BasicBlock) -> &[BasicBlock] {
266270
self.cache.unwrap_predecessors_for(bb)

src/librustc/mir/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ macro_rules! make_mir_visitor {
812812

813813
fn visit_location(
814814
&mut self,
815-
body_cache: & $($mutability)? BodyCache<&'_ $($mutability)? Body<'tcx>>,
815+
body_cache: body_cache_type!($($mutability)? '_, 'tcx),
816816
location: Location
817817
) {
818818
let basic_block = & $($mutability)? body_cache[location.block];

src/librustc_mir/borrow_check/mutability_errors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc::hir;
22
use rustc::hir::Node;
3-
use rustc::mir::{self, Body, ClearCrossCrate, Local, LocalInfo, Location};
3+
use rustc::mir::{self, ClearCrossCrate, Local, LocalInfo, Location, ReadOnlyBodyCache};
44
use rustc::mir::{Mutability, Place, PlaceRef, PlaceBase, ProjectionElem};
55
use rustc::ty::{self, Ty, TyCtxt};
66
use rustc_index::vec::Idx;
@@ -529,14 +529,14 @@ fn suggest_ampmut_self<'tcx>(
529529
// by trying (3.), then (2.) and finally falling back on (1.).
530530
fn suggest_ampmut<'tcx>(
531531
tcx: TyCtxt<'tcx>,
532-
body: &Body<'tcx>,
532+
body_cache: &ReadOnlyBodyCache<'_, 'tcx>,
533533
local: Local,
534534
local_decl: &mir::LocalDecl<'tcx>,
535535
opt_ty_info: Option<Span>,
536536
) -> (Span, String) {
537-
let locations = body.find_assignments(local);
537+
let locations = body_cache.find_assignments(local);
538538
if !locations.is_empty() {
539-
let assignment_rhs_span = body.source_info(locations[0]).span;
539+
let assignment_rhs_span = body_cache.source_info(locations[0]).span;
540540
if let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span) {
541541
if let (true, Some(ws_pos)) = (
542542
src.starts_with("&'"),

src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::borrow_check::nll::region_infer::values::{PointIndex, RegionValueElements};
22
use crate::util::liveness::{categorize, DefUse};
33
use rustc::mir::visit::{PlaceContext, Visitor};
4-
use rustc::mir::{Body, Local, Location};
4+
use rustc::mir::{Local, Location, ReadOnlyBodyCache};
55
use rustc_index::vec::{Idx, IndexVec};
66
use rustc_data_structures::vec_linked_list as vll;
77

@@ -60,9 +60,9 @@ impl LocalUseMap {
6060
crate fn build(
6161
live_locals: &Vec<Local>,
6262
elements: &RegionValueElements,
63-
body: &Body<'_>,
63+
body_cache: &ReadOnlyBodyCache<'_, '_>,
6464
) -> Self {
65-
let nones = IndexVec::from_elem_n(None, body.local_decls.len());
65+
let nones = IndexVec::from_elem_n(None, body_cache.local_decls.len());
6666
let mut local_use_map = LocalUseMap {
6767
first_def_at: nones.clone(),
6868
first_use_at: nones.clone(),
@@ -75,11 +75,11 @@ impl LocalUseMap {
7575
}
7676

7777
let mut locals_with_use_data: IndexVec<Local, bool> =
78-
IndexVec::from_elem_n(false, body.local_decls.len());
78+
IndexVec::from_elem_n(false, body_cache.local_decls.len());
7979
live_locals.iter().for_each(|&local| locals_with_use_data[local] = true);
8080

8181
LocalUseMapBuild { local_use_map: &mut local_use_map, elements, locals_with_use_data }
82-
.visit_body(body);
82+
.visit_body(body_cache);
8383

8484
local_use_map
8585
}

src/librustc_mir/borrow_check/nll/type_check/liveness/polonius.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::dataflow::indexes::MovePathIndex;
33
use crate::dataflow::move_paths::{LookupResult, MoveData};
44
use crate::util::liveness::{categorize, DefUse};
55
use rustc::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
6-
use rustc::mir::{Body, Local, Location, Place};
6+
use rustc::mir::{Local, Location, Place, ReadOnlyBodyCache};
77
use rustc::ty::subst::GenericArg;
88
use rustc::ty::Ty;
99

@@ -97,7 +97,7 @@ fn add_var_uses_regions(typeck: &mut TypeChecker<'_, 'tcx>, local: Local, ty: Ty
9797

9898
pub(super) fn populate_access_facts(
9999
typeck: &mut TypeChecker<'_, 'tcx>,
100-
body: &Body<'tcx>,
100+
body_cache: &ReadOnlyBodyCache<'_, 'tcx>,
101101
location_table: &LocationTable,
102102
move_data: &MoveData<'_>,
103103
drop_used: &mut Vec<(Local, Location)>,
@@ -113,14 +113,14 @@ pub(super) fn populate_access_facts(
113113
location_table,
114114
move_data,
115115
}
116-
.visit_body(body);
116+
.visit_body(body_cache);
117117

118118
facts.var_drop_used.extend(drop_used.iter().map(|&(local, location)| {
119119
(local, location_table.mid_index(location))
120120
}));
121121
}
122122

123-
for (local, local_decl) in body.local_decls.iter_enumerated() {
123+
for (local, local_decl) in body_cache.local_decls.iter_enumerated() {
124124
add_var_uses_regions(typeck, local, local_decl.ty);
125125
}
126126
}

src/librustc_mir/dataflow/impls/storage_liveness.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,46 +75,46 @@ impl<'a, 'tcx> BottomValue for MaybeStorageLive<'a, 'tcx> {
7575
/// Dataflow analysis that determines whether each local requires storage at a
7676
/// given location; i.e. whether its storage can go away without being observed.
7777
pub struct RequiresStorage<'mir, 'tcx> {
78-
body: &'mir Body<'tcx>,
78+
body_cache: &'mir ReadOnlyBodyCache<'mir, 'tcx>,
7979
borrowed_locals:
8080
RefCell<DataflowResultsRefCursor<'mir, 'tcx, HaveBeenBorrowedLocals<'mir, 'tcx>>>,
8181
}
8282

8383
impl<'mir, 'tcx: 'mir> RequiresStorage<'mir, 'tcx> {
8484
pub fn new(
85-
body: &'mir Body<'tcx>,
85+
body_cache: &'mir ReadOnlyBodyCache<'mir, 'tcx>,
8686
borrowed_locals: &'mir DataflowResults<'tcx, HaveBeenBorrowedLocals<'mir, 'tcx>>,
8787
) -> Self {
8888
RequiresStorage {
89-
body,
90-
borrowed_locals: RefCell::new(DataflowResultsCursor::new(borrowed_locals, body)),
89+
body_cache,
90+
borrowed_locals: RefCell::new(DataflowResultsCursor::new(borrowed_locals, body_cache)),
9191
}
9292
}
9393

9494
pub fn body(&self) -> &Body<'tcx> {
95-
self.body
95+
&self.body_cache
9696
}
9797
}
9898

9999
impl<'mir, 'tcx> BitDenotation<'tcx> for RequiresStorage<'mir, 'tcx> {
100100
type Idx = Local;
101101
fn name() -> &'static str { "requires_storage" }
102102
fn bits_per_block(&self) -> usize {
103-
self.body.local_decls.len()
103+
self.body_cache.local_decls.len()
104104
}
105105

106106
fn start_block_effect(&self, _sets: &mut BitSet<Local>) {
107107
// Nothing is live on function entry (generators only have a self
108108
// argument, and we don't care about that)
109-
assert_eq!(1, self.body.arg_count);
109+
assert_eq!(1, self.body_cache.arg_count);
110110
}
111111

112112
fn before_statement_effect(&self, sets: &mut GenKillSet<Self::Idx>, loc: Location) {
113113
// If we borrow or assign to a place then it needs storage for that
114114
// statement.
115115
self.check_for_borrow(sets, loc);
116116

117-
let stmt = &self.body[loc.block].statements[loc.statement_index];
117+
let stmt = &self.body_cache[loc.block].statements[loc.statement_index];
118118
match stmt.kind {
119119
StatementKind::StorageDead(l) => sets.kill(l),
120120
StatementKind::Assign(box(ref place, _))
@@ -146,7 +146,7 @@ impl<'mir, 'tcx> BitDenotation<'tcx> for RequiresStorage<'mir, 'tcx> {
146146
if let TerminatorKind::Call {
147147
destination: Some((Place { base: PlaceBase::Local(local), .. }, _)),
148148
..
149-
} = self.body[loc.block].terminator().kind {
149+
} = self.body_cache[loc.block].terminator().kind {
150150
sets.gen(local);
151151
}
152152
}
@@ -159,7 +159,7 @@ impl<'mir, 'tcx> BitDenotation<'tcx> for RequiresStorage<'mir, 'tcx> {
159159
if let TerminatorKind::Call {
160160
destination: Some((ref place, _)),
161161
..
162-
} = self.body[loc.block].terminator().kind {
162+
} = self.body_cache[loc.block].terminator().kind {
163163
if let Some(local) = place.as_local() {
164164
sets.kill(local);
165165
}
@@ -187,7 +187,7 @@ impl<'mir, 'tcx> RequiresStorage<'mir, 'tcx> {
187187
sets,
188188
borrowed_locals: &self.borrowed_locals,
189189
};
190-
visitor.visit_location(self.body, loc);
190+
visitor.visit_location(&self.body_cache, loc);
191191
}
192192

193193
/// Gen locals that are newly borrowed. This includes borrowing any part of

src/librustc_mir/monomorphize/collector.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind, Instance};
186186
use rustc::ty::print::obsolete::DefPathBasedNames;
187187
use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast};
188188
use rustc::session::config::EntryFnType;
189-
use rustc::mir::{self, Location, PlaceBase, Static, StaticKind};
189+
use rustc::mir::{self, BodyCache, Location, PlaceBase, Static, StaticKind};
190190
use rustc::mir::visit::Visitor as MirVisitor;
191191
use rustc::mir::mono::{MonoItem, InstantiationMode};
192192
use rustc::mir::interpret::{Scalar, GlobalId, GlobalAlloc, ErrorHandled};
@@ -1249,13 +1249,14 @@ fn collect_neighbours<'tcx>(
12491249
) {
12501250
debug!("collect_neighbours: {:?}", instance.def_id());
12511251
let body = tcx.instance_mir(instance.def);
1252+
let body_cache = BodyCache::new(body).read_only();
12521253

12531254
MirNeighborCollector {
12541255
tcx,
12551256
body: &body,
12561257
output,
12571258
param_substs: instance.substs,
1258-
}.visit_body(&body);
1259+
}.visit_body(&body_cache);
12591260
}
12601261

12611262
fn def_id_to_string(tcx: TyCtxt<'_>, def_id: DefId) -> String {

src/librustc_mir/util/collect_writes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc::mir::{Local, Location};
2-
use rustc::mir::Body;
2+
use rustc::mir::ReadOnlyBodyCache;
33
use rustc::mir::visit::PlaceContext;
44
use rustc::mir::visit::Visitor;
55

@@ -9,7 +9,7 @@ crate trait FindAssignments {
99
fn find_assignments(&self, local: Local) -> Vec<Location>;
1010
}
1111

12-
impl<'tcx> FindAssignments for Body<'tcx>{
12+
impl<'a, 'tcx> FindAssignments for ReadOnlyBodyCache<'a, 'tcx>{
1313
fn find_assignments(&self, local: Local) -> Vec<Location>{
1414
let mut visitor = FindLocalAssignmentVisitor{ needle: local, locations: vec![]};
1515
visitor.visit_body(self);

src/librustc_mir/util/liveness.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ pub struct LivenessResult {
5757
/// Computes which local variables are live within the given function
5858
/// `mir`, including drops.
5959
pub fn liveness_of_locals(
60-
body: &Body<'_>,
60+
body_cache: &ReadOnlyBodyCache<'_, '_>,
6161
) -> LivenessResult {
62-
let num_live_vars = body.local_decls.len();
62+
let num_live_vars = body_cache.local_decls.len();
6363

64-
let def_use: IndexVec<_, DefsUses> = body
64+
let def_use: IndexVec<_, DefsUses> = body_cache
6565
.basic_blocks()
6666
.iter()
6767
.map(|b| block(b, num_live_vars))
6868
.collect();
6969

70-
let mut outs: IndexVec<_, LiveVarSet> = body
70+
let mut outs: IndexVec<_, LiveVarSet> = body_cache
7171
.basic_blocks()
7272
.indices()
7373
.map(|_| LiveVarSet::new_empty(num_live_vars))
@@ -83,18 +83,18 @@ pub fn liveness_of_locals(
8383
// FIXME(ecstaticmorse): Reverse post-order on the reverse CFG may generate a better iteration
8484
// order when cycles are present, but the overhead of computing the reverse CFG may outweigh
8585
// any benefits. Benchmark this and find out.
86-
let mut dirty_queue: WorkQueue<BasicBlock> = WorkQueue::with_none(body.basic_blocks().len());
87-
for (bb, _) in traversal::postorder(body) {
86+
let mut dirty_queue: WorkQueue<BasicBlock> = WorkQueue::with_none(body_cache.basic_blocks().len());
87+
for (bb, _) in traversal::postorder(body_cache) {
8888
dirty_queue.insert(bb);
8989
}
9090

9191
// Add blocks which are not reachable from START_BLOCK to the work queue. These blocks will
9292
// be processed after the ones added above.
93-
for bb in body.basic_blocks().indices() {
93+
for bb in body_cache.basic_blocks().indices() {
9494
dirty_queue.insert(bb);
9595
}
9696

97-
let predecessors = body.unwrap_predecessors();
97+
let predecessors = body_cache.predecessors();
9898

9999
while let Some(bb) = dirty_queue.pop() {
100100
// bits = use ∪ (bits - def)

src/librustc_mir/util/patch.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,21 @@ impl<'tcx> MirPatch<'tcx> {
127127
self.make_nop.push(loc);
128128
}
129129

130-
pub fn apply(self, body: &mut Body<'tcx>) {
130+
pub fn apply(self, body_cache: &mut BodyCache<&'_ mut Body<'tcx>>) {
131131
debug!("MirPatch: make nops at: {:?}", self.make_nop);
132132
for loc in self.make_nop {
133-
body.make_statement_nop(loc);
133+
body_cache.make_statement_nop(loc);
134134
}
135135
debug!("MirPatch: {:?} new temps, starting from index {}: {:?}",
136-
self.new_locals.len(), body.local_decls.len(), self.new_locals);
136+
self.new_locals.len(), body_cache.local_decls.len(), self.new_locals);
137137
debug!("MirPatch: {} new blocks, starting from index {}",
138-
self.new_blocks.len(), body.basic_blocks().len());
139-
body.basic_blocks_mut().extend(self.new_blocks);
140-
body.local_decls.extend(self.new_locals);
138+
self.new_blocks.len(), body_cache.basic_blocks().len());
139+
body_cache.basic_blocks_mut().extend(self.new_blocks);
140+
body_cache.local_decls.extend(self.new_locals);
141141
for (src, patch) in self.patch_map.into_iter_enumerated() {
142142
if let Some(patch) = patch {
143143
debug!("MirPatch: patching block {:?}", src);
144-
body[src].terminator_mut().kind = patch;
144+
body_cache[src].terminator_mut().kind = patch;
145145
}
146146
}
147147

@@ -159,9 +159,9 @@ impl<'tcx> MirPatch<'tcx> {
159159
stmt, loc, delta);
160160
loc.statement_index += delta;
161161
let source_info = Self::source_info_for_index(
162-
&body[loc.block], loc
162+
&body_cache[loc.block], loc
163163
);
164-
body[loc.block].statements.insert(
164+
body_cache[loc.block].statements.insert(
165165
loc.statement_index, Statement {
166166
source_info,
167167
kind: stmt

0 commit comments

Comments
 (0)