diff options
| author | Paul Daniel Faria <Nashenas88@users.noreply.github.com> | 2019-10-25 09:36:59 -0400 |
|---|---|---|
| committer | Paul Daniel Faria <Nashenas88@users.noreply.github.com> | 2019-12-02 08:31:35 -0500 |
| commit | 0a193712431d61cd70dda517b88c564a411b3541 (patch) | |
| tree | b255cf2b043398e10330c85c968996339679386f | |
| parent | 26f1c01ff7d63415cabacefa301b6b4a6814f661 (diff) | |
| download | rust-0a193712431d61cd70dda517b88c564a411b3541.tar.gz rust-0a193712431d61cd70dda517b88c564a411b3541.zip | |
Add predecessors fn to ReadOnlyBodyCache, fix more Body -> (ReadOnly)BodyCache type errors
| -rw-r--r-- | src/librustc/mir/cache.rs | 6 | ||||
| -rw-r--r-- | src/librustc/mir/visit.rs | 2 | ||||
| -rw-r--r-- | src/librustc_mir/borrow_check/mutability_errors.rs | 8 | ||||
| -rw-r--r-- | src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs | 10 | ||||
| -rw-r--r-- | src/librustc_mir/borrow_check/nll/type_check/liveness/polonius.rs | 8 | ||||
| -rw-r--r-- | src/librustc_mir/dataflow/impls/storage_liveness.rs | 22 | ||||
| -rw-r--r-- | src/librustc_mir/monomorphize/collector.rs | 5 | ||||
| -rw-r--r-- | src/librustc_mir/util/collect_writes.rs | 4 | ||||
| -rw-r--r-- | src/librustc_mir/util/liveness.rs | 16 | ||||
| -rw-r--r-- | src/librustc_mir/util/patch.rs | 18 |
10 files changed, 52 insertions, 47 deletions
diff --git a/src/librustc/mir/cache.rs b/src/librustc/mir/cache.rs index 24d1f9e373b..b6682470606 100644 --- a/src/librustc/mir/cache.rs +++ b/src/librustc/mir/cache.rs @@ -14,7 +14,6 @@ pub struct Cache { predecessors: Option<IndexVec<BasicBlock, Vec<BasicBlock>>>, } - //impl<'tcx, T> rustc_serialize::Encodable for Cache<'tcx, T> { // fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { // Encodable::encode(&(), s) @@ -262,6 +261,11 @@ pub struct ReadOnlyBodyCache<'a, 'tcx> { impl ReadOnlyBodyCache<'a, 'tcx> { #[inline] + pub fn predecessors(&self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> { + self.cache.predecessors.as_ref().unwrap() + } + + #[inline] pub fn predecessors_for(&self, bb: BasicBlock) -> &[BasicBlock] { self.cache.unwrap_predecessors_for(bb) } diff --git a/src/librustc/mir/visit.rs b/src/librustc/mir/visit.rs index 7877466fbf6..c464247c4b4 100644 --- a/src/librustc/mir/visit.rs +++ b/src/librustc/mir/visit.rs @@ -812,7 +812,7 @@ macro_rules! make_mir_visitor { fn visit_location( &mut self, - body_cache: & $($mutability)? BodyCache<&'_ $($mutability)? Body<'tcx>>, + body_cache: body_cache_type!($($mutability)? '_, 'tcx), location: Location ) { let basic_block = & $($mutability)? body_cache[location.block]; diff --git a/src/librustc_mir/borrow_check/mutability_errors.rs b/src/librustc_mir/borrow_check/mutability_errors.rs index 654666882cc..44e139889c9 100644 --- a/src/librustc_mir/borrow_check/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/mutability_errors.rs @@ -1,6 +1,6 @@ use rustc::hir; use rustc::hir::Node; -use rustc::mir::{self, Body, ClearCrossCrate, Local, LocalInfo, Location}; +use rustc::mir::{self, ClearCrossCrate, Local, LocalInfo, Location, ReadOnlyBodyCache}; use rustc::mir::{Mutability, Place, PlaceRef, PlaceBase, ProjectionElem}; use rustc::ty::{self, Ty, TyCtxt}; use rustc_index::vec::Idx; @@ -529,14 +529,14 @@ fn suggest_ampmut_self<'tcx>( // by trying (3.), then (2.) and finally falling back on (1.). fn suggest_ampmut<'tcx>( tcx: TyCtxt<'tcx>, - body: &Body<'tcx>, + body_cache: &ReadOnlyBodyCache<'_, 'tcx>, local: Local, local_decl: &mir::LocalDecl<'tcx>, opt_ty_info: Option<Span>, ) -> (Span, String) { - let locations = body.find_assignments(local); + let locations = body_cache.find_assignments(local); if !locations.is_empty() { - let assignment_rhs_span = body.source_info(locations[0]).span; + let assignment_rhs_span = body_cache.source_info(locations[0]).span; if let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span) { if let (true, Some(ws_pos)) = ( src.starts_with("&'"), diff --git a/src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs b/src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs index 7dee00b3eca..181508a983a 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/liveness/local_use_map.rs @@ -1,7 +1,7 @@ use crate::borrow_check::nll::region_infer::values::{PointIndex, RegionValueElements}; use crate::util::liveness::{categorize, DefUse}; use rustc::mir::visit::{PlaceContext, Visitor}; -use rustc::mir::{Body, Local, Location}; +use rustc::mir::{Local, Location, ReadOnlyBodyCache}; use rustc_index::vec::{Idx, IndexVec}; use rustc_data_structures::vec_linked_list as vll; @@ -60,9 +60,9 @@ impl LocalUseMap { crate fn build( live_locals: &Vec<Local>, elements: &RegionValueElements, - body: &Body<'_>, + body_cache: &ReadOnlyBodyCache<'_, '_>, ) -> Self { - let nones = IndexVec::from_elem_n(None, body.local_decls.len()); + let nones = IndexVec::from_elem_n(None, body_cache.local_decls.len()); let mut local_use_map = LocalUseMap { first_def_at: nones.clone(), first_use_at: nones.clone(), @@ -75,11 +75,11 @@ impl LocalUseMap { } let mut locals_with_use_data: IndexVec<Local, bool> = - IndexVec::from_elem_n(false, body.local_decls.len()); + IndexVec::from_elem_n(false, body_cache.local_decls.len()); live_locals.iter().for_each(|&local| locals_with_use_data[local] = true); LocalUseMapBuild { local_use_map: &mut local_use_map, elements, locals_with_use_data } - .visit_body(body); + .visit_body(body_cache); local_use_map } diff --git a/src/librustc_mir/borrow_check/nll/type_check/liveness/polonius.rs b/src/librustc_mir/borrow_check/nll/type_check/liveness/polonius.rs index 526ad7fb905..3320815688f 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/liveness/polonius.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/liveness/polonius.rs @@ -3,7 +3,7 @@ use crate::dataflow::indexes::MovePathIndex; use crate::dataflow::move_paths::{LookupResult, MoveData}; use crate::util::liveness::{categorize, DefUse}; use rustc::mir::visit::{MutatingUseContext, PlaceContext, Visitor}; -use rustc::mir::{Body, Local, Location, Place}; +use rustc::mir::{Local, Location, Place, ReadOnlyBodyCache}; use rustc::ty::subst::GenericArg; use rustc::ty::Ty; @@ -97,7 +97,7 @@ fn add_var_uses_regions(typeck: &mut TypeChecker<'_, 'tcx>, local: Local, ty: Ty pub(super) fn populate_access_facts( typeck: &mut TypeChecker<'_, 'tcx>, - body: &Body<'tcx>, + body_cache: &ReadOnlyBodyCache<'_, 'tcx>, location_table: &LocationTable, move_data: &MoveData<'_>, drop_used: &mut Vec<(Local, Location)>, @@ -113,14 +113,14 @@ pub(super) fn populate_access_facts( location_table, move_data, } - .visit_body(body); + .visit_body(body_cache); facts.var_drop_used.extend(drop_used.iter().map(|&(local, location)| { (local, location_table.mid_index(location)) })); } - for (local, local_decl) in body.local_decls.iter_enumerated() { + for (local, local_decl) in body_cache.local_decls.iter_enumerated() { add_var_uses_regions(typeck, local, local_decl.ty); } } diff --git a/src/librustc_mir/dataflow/impls/storage_liveness.rs b/src/librustc_mir/dataflow/impls/storage_liveness.rs index 1b81032bfe6..2e164765043 100644 --- a/src/librustc_mir/dataflow/impls/storage_liveness.rs +++ b/src/librustc_mir/dataflow/impls/storage_liveness.rs @@ -75,24 +75,24 @@ impl<'a, 'tcx> BottomValue for MaybeStorageLive<'a, 'tcx> { /// Dataflow analysis that determines whether each local requires storage at a /// given location; i.e. whether its storage can go away without being observed. pub struct RequiresStorage<'mir, 'tcx> { - body: &'mir Body<'tcx>, + body_cache: &'mir ReadOnlyBodyCache<'mir, 'tcx>, borrowed_locals: RefCell<DataflowResultsRefCursor<'mir, 'tcx, HaveBeenBorrowedLocals<'mir, 'tcx>>>, } impl<'mir, 'tcx: 'mir> RequiresStorage<'mir, 'tcx> { pub fn new( - body: &'mir Body<'tcx>, + body_cache: &'mir ReadOnlyBodyCache<'mir, 'tcx>, borrowed_locals: &'mir DataflowResults<'tcx, HaveBeenBorrowedLocals<'mir, 'tcx>>, ) -> Self { RequiresStorage { - body, - borrowed_locals: RefCell::new(DataflowResultsCursor::new(borrowed_locals, body)), + body_cache, + borrowed_locals: RefCell::new(DataflowResultsCursor::new(borrowed_locals, body_cache)), } } pub fn body(&self) -> &Body<'tcx> { - self.body + &self.body_cache } } @@ -100,13 +100,13 @@ impl<'mir, 'tcx> BitDenotation<'tcx> for RequiresStorage<'mir, 'tcx> { type Idx = Local; fn name() -> &'static str { "requires_storage" } fn bits_per_block(&self) -> usize { - self.body.local_decls.len() + self.body_cache.local_decls.len() } fn start_block_effect(&self, _sets: &mut BitSet<Local>) { // Nothing is live on function entry (generators only have a self // argument, and we don't care about that) - assert_eq!(1, self.body.arg_count); + assert_eq!(1, self.body_cache.arg_count); } fn before_statement_effect(&self, sets: &mut GenKillSet<Self::Idx>, loc: Location) { @@ -114,7 +114,7 @@ impl<'mir, 'tcx> BitDenotation<'tcx> for RequiresStorage<'mir, 'tcx> { // statement. self.check_for_borrow(sets, loc); - let stmt = &self.body[loc.block].statements[loc.statement_index]; + let stmt = &self.body_cache[loc.block].statements[loc.statement_index]; match stmt.kind { StatementKind::StorageDead(l) => sets.kill(l), StatementKind::Assign(box(ref place, _)) @@ -146,7 +146,7 @@ impl<'mir, 'tcx> BitDenotation<'tcx> for RequiresStorage<'mir, 'tcx> { if let TerminatorKind::Call { destination: Some((Place { base: PlaceBase::Local(local), .. }, _)), .. - } = self.body[loc.block].terminator().kind { + } = self.body_cache[loc.block].terminator().kind { sets.gen(local); } } @@ -159,7 +159,7 @@ impl<'mir, 'tcx> BitDenotation<'tcx> for RequiresStorage<'mir, 'tcx> { if let TerminatorKind::Call { destination: Some((ref place, _)), .. - } = self.body[loc.block].terminator().kind { + } = self.body_cache[loc.block].terminator().kind { if let Some(local) = place.as_local() { sets.kill(local); } @@ -187,7 +187,7 @@ impl<'mir, 'tcx> RequiresStorage<'mir, 'tcx> { sets, borrowed_locals: &self.borrowed_locals, }; - visitor.visit_location(self.body, loc); + visitor.visit_location(&self.body_cache, loc); } /// Gen locals that are newly borrowed. This includes borrowing any part of diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index a4c4c7ff616..285bdf50d4a 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -186,7 +186,7 @@ use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind, Instance}; use rustc::ty::print::obsolete::DefPathBasedNames; use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast}; use rustc::session::config::EntryFnType; -use rustc::mir::{self, Location, PlaceBase, Static, StaticKind}; +use rustc::mir::{self, BodyCache, Location, PlaceBase, Static, StaticKind}; use rustc::mir::visit::Visitor as MirVisitor; use rustc::mir::mono::{MonoItem, InstantiationMode}; use rustc::mir::interpret::{Scalar, GlobalId, GlobalAlloc, ErrorHandled}; @@ -1249,13 +1249,14 @@ fn collect_neighbours<'tcx>( ) { debug!("collect_neighbours: {:?}", instance.def_id()); let body = tcx.instance_mir(instance.def); + let body_cache = BodyCache::new(body).read_only(); MirNeighborCollector { tcx, body: &body, output, param_substs: instance.substs, - }.visit_body(&body); + }.visit_body(&body_cache); } fn def_id_to_string(tcx: TyCtxt<'_>, def_id: DefId) -> String { diff --git a/src/librustc_mir/util/collect_writes.rs b/src/librustc_mir/util/collect_writes.rs index c8804dfbaf2..0092c3c86d7 100644 --- a/src/librustc_mir/util/collect_writes.rs +++ b/src/librustc_mir/util/collect_writes.rs @@ -1,5 +1,5 @@ use rustc::mir::{Local, Location}; -use rustc::mir::Body; +use rustc::mir::ReadOnlyBodyCache; use rustc::mir::visit::PlaceContext; use rustc::mir::visit::Visitor; @@ -9,7 +9,7 @@ crate trait FindAssignments { fn find_assignments(&self, local: Local) -> Vec<Location>; } -impl<'tcx> FindAssignments for Body<'tcx>{ +impl<'a, 'tcx> FindAssignments for ReadOnlyBodyCache<'a, 'tcx>{ fn find_assignments(&self, local: Local) -> Vec<Location>{ let mut visitor = FindLocalAssignmentVisitor{ needle: local, locations: vec![]}; visitor.visit_body(self); diff --git a/src/librustc_mir/util/liveness.rs b/src/librustc_mir/util/liveness.rs index d313cba6bdf..87e6291a454 100644 --- a/src/librustc_mir/util/liveness.rs +++ b/src/librustc_mir/util/liveness.rs @@ -57,17 +57,17 @@ pub struct LivenessResult { /// Computes which local variables are live within the given function /// `mir`, including drops. pub fn liveness_of_locals( - body: &Body<'_>, + body_cache: &ReadOnlyBodyCache<'_, '_>, ) -> LivenessResult { - let num_live_vars = body.local_decls.len(); + let num_live_vars = body_cache.local_decls.len(); - let def_use: IndexVec<_, DefsUses> = body + let def_use: IndexVec<_, DefsUses> = body_cache .basic_blocks() .iter() .map(|b| block(b, num_live_vars)) .collect(); - let mut outs: IndexVec<_, LiveVarSet> = body + let mut outs: IndexVec<_, LiveVarSet> = body_cache .basic_blocks() .indices() .map(|_| LiveVarSet::new_empty(num_live_vars)) @@ -83,18 +83,18 @@ pub fn liveness_of_locals( // FIXME(ecstaticmorse): Reverse post-order on the reverse CFG may generate a better iteration // order when cycles are present, but the overhead of computing the reverse CFG may outweigh // any benefits. Benchmark this and find out. - let mut dirty_queue: WorkQueue<BasicBlock> = WorkQueue::with_none(body.basic_blocks().len()); - for (bb, _) in traversal::postorder(body) { + let mut dirty_queue: WorkQueue<BasicBlock> = WorkQueue::with_none(body_cache.basic_blocks().len()); + for (bb, _) in traversal::postorder(body_cache) { dirty_queue.insert(bb); } // Add blocks which are not reachable from START_BLOCK to the work queue. These blocks will // be processed after the ones added above. - for bb in body.basic_blocks().indices() { + for bb in body_cache.basic_blocks().indices() { dirty_queue.insert(bb); } - let predecessors = body.unwrap_predecessors(); + let predecessors = body_cache.predecessors(); while let Some(bb) = dirty_queue.pop() { // bits = use ∪ (bits - def) diff --git a/src/librustc_mir/util/patch.rs b/src/librustc_mir/util/patch.rs index a5f7e540157..01be0f598ed 100644 --- a/src/librustc_mir/util/patch.rs +++ b/src/librustc_mir/util/patch.rs @@ -127,21 +127,21 @@ impl<'tcx> MirPatch<'tcx> { self.make_nop.push(loc); } - pub fn apply(self, body: &mut Body<'tcx>) { + pub fn apply(self, body_cache: &mut BodyCache<&'_ mut Body<'tcx>>) { debug!("MirPatch: make nops at: {:?}", self.make_nop); for loc in self.make_nop { - body.make_statement_nop(loc); + body_cache.make_statement_nop(loc); } debug!("MirPatch: {:?} new temps, starting from index {}: {:?}", - self.new_locals.len(), body.local_decls.len(), self.new_locals); + self.new_locals.len(), body_cache.local_decls.len(), self.new_locals); debug!("MirPatch: {} new blocks, starting from index {}", - self.new_blocks.len(), body.basic_blocks().len()); - body.basic_blocks_mut().extend(self.new_blocks); - body.local_decls.extend(self.new_locals); + self.new_blocks.len(), body_cache.basic_blocks().len()); + body_cache.basic_blocks_mut().extend(self.new_blocks); + body_cache.local_decls.extend(self.new_locals); for (src, patch) in self.patch_map.into_iter_enumerated() { if let Some(patch) = patch { debug!("MirPatch: patching block {:?}", src); - body[src].terminator_mut().kind = patch; + body_cache[src].terminator_mut().kind = patch; } } @@ -159,9 +159,9 @@ impl<'tcx> MirPatch<'tcx> { stmt, loc, delta); loc.statement_index += delta; let source_info = Self::source_info_for_index( - &body[loc.block], loc + &body_cache[loc.block], loc ); - body[loc.block].statements.insert( + body_cache[loc.block].statements.insert( loc.statement_index, Statement { source_info, kind: stmt |
