Skip to content

Commit

Permalink
remove ReserveOrActivateIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Apr 15, 2018
1 parent d32e5aa commit a849da6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 80 deletions.
7 changes: 3 additions & 4 deletions src/librustc_mir/borrow_check/flows.rs
Expand Up @@ -88,9 +88,8 @@ impl<'b, 'gcx, 'tcx> fmt::Display for Flows<'b, 'gcx, 'tcx> {
s.push_str(", ");
};
saw_one = true;
let borrow_data = &self.borrows.operator().borrows()[borrow.borrow_index()];
s.push_str(&format!("{}{}", borrow_data,
if borrow.is_activation() { "@active" } else { "" }));
let borrow_data = &self.borrows.operator().borrows()[borrow];
s.push_str(&format!("{}", borrow_data));
});
s.push_str("] ");

Expand All @@ -101,7 +100,7 @@ impl<'b, 'gcx, 'tcx> fmt::Display for Flows<'b, 'gcx, 'tcx> {
s.push_str(", ");
};
saw_one = true;
let borrow_data = &self.borrows.operator().borrows()[borrow.borrow_index()];
let borrow_data = &self.borrows.operator().borrows()[borrow];
s.push_str(&format!("{}", borrow_data));
});
s.push_str("] ");
Expand Down
19 changes: 5 additions & 14 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -220,9 +220,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
&attributes,
&dead_unwinds,
Borrows::new(tcx, mir, opt_regioncx.clone(), def_id, body_id, borrow_set),
|rs, i| {
DebugFormatted::new(&(i.kind(), rs.location(i.borrow_index())))
}
|rs, i| DebugFormatted::new(&rs.location(i)),
));

let movable_generator = !match tcx.hir.get(id) {
Expand Down Expand Up @@ -549,7 +547,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
let data = domain.borrows();
flow_state.borrows.with_iter_outgoing(|borrows| {
for i in borrows {
let borrow = &data[i.borrow_index()];
let borrow = &data[i];
self.check_for_local_borrow(borrow, span);
}
});
Expand All @@ -565,7 +563,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
let data = domain.borrows();
flow_state.borrows.with_iter_outgoing(|borrows| {
for i in borrows {
let borrow = &data[i.borrow_index()];
let borrow = &data[i];
let context = ContextKind::StorageDead.new(loc);
self.check_for_invalidation_at_exit(context, borrow, span, flow_state);
}
Expand Down Expand Up @@ -2245,21 +2243,14 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
// borrows of P, P.a.b, etc.
let mut iter_incoming = flow_state.borrows.iter_incoming();
while let Some(i) = iter_incoming.next() {
// TODO -- for now, just skip activations, since
// everywhere that activation is set, reservation should
// be set
if i.is_activation() {
continue;
}

let borrowed = &data[i.borrow_index()];
let borrowed = &data[i];

if self.places_conflict(&borrowed.borrowed_place, place, access) {
debug!(
"each_borrow_involving_path: {:?} @ {:?} vs. {:?}/{:?}",
i, borrowed, place, access
);
let ctrl = op(self, i.borrow_index(), borrowed);
let ctrl = op(self, i, borrowed);
if ctrl == Control::Break {
return;
}
Expand Down
79 changes: 20 additions & 59 deletions src/librustc_mir/dataflow/impls/borrows.rs
Expand Up @@ -20,13 +20,13 @@ use rustc::ty::{Region, TyCtxt};
use rustc::ty::RegionKind;
use rustc::ty::RegionKind::ReScope;

use rustc_data_structures::bitslice::{BitwiseOperator};
use rustc_data_structures::indexed_set::{IdxSet};
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc_data_structures::bitslice::BitwiseOperator;
use rustc_data_structures::indexed_set::IdxSet;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::sync::Lrc;

use dataflow::{BitDenotation, BlockSets, InitialFlow};
pub use dataflow::indexes::{BorrowIndex, ReserveOrActivateIndex};
pub use dataflow::indexes::BorrowIndex;
use borrow_check::nll::region_infer::RegionInferenceContext;
use borrow_check::nll::ToRegionVid;

Expand All @@ -53,21 +53,6 @@ pub struct Borrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
nonlexical_regioncx: Option<Rc<RegionInferenceContext<'tcx>>>,
}

impl ReserveOrActivateIndex {
fn reserved(i: BorrowIndex) -> Self { ReserveOrActivateIndex::new(i.index() * 2) }
fn active(i: BorrowIndex) -> Self { ReserveOrActivateIndex::new((i.index() * 2) + 1) }

pub(crate) fn is_reservation(self) -> bool { self.index() % 2 == 0 }
pub(crate) fn is_activation(self) -> bool { self.index() % 2 == 1}

pub(crate) fn kind(self) -> &'static str {
if self.is_reservation() { "reserved" } else { "active" }
}
pub(crate) fn borrow_index(self) -> BorrowIndex {
BorrowIndex::new(self.index() / 2)
}
}

impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
crate fn new(
tcx: TyCtxt<'a, 'gcx, 'tcx>,
Expand Down Expand Up @@ -120,7 +105,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
/// That means either they went out of either a nonlexical scope, if we care about those
/// at the moment, or the location represents a lexical EndRegion
fn kill_loans_out_of_scope_at_location(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
sets: &mut BlockSets<BorrowIndex>,
location: Location) {
if let Some(ref regioncx) = self.nonlexical_regioncx {
// NOTE: The state associated with a given `location`
Expand All @@ -137,22 +122,18 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
for (borrow_index, borrow_data) in self.borrow_set.borrows.iter_enumerated() {
let borrow_region = borrow_data.region.to_region_vid();
if !regioncx.region_contains_point(borrow_region, location) {
sets.kill(&ReserveOrActivateIndex::reserved(borrow_index));
sets.kill(&ReserveOrActivateIndex::active(borrow_index));
sets.kill(&borrow_index);
}
}
}
}

fn kill_borrows_on_local(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
sets: &mut BlockSets<BorrowIndex>,
local: &rustc::mir::Local)
{
if let Some(borrow_indexes) = self.borrow_set.local_map.get(local) {
sets.kill_all(borrow_indexes.iter()
.map(|b| ReserveOrActivateIndex::reserved(*b)));
sets.kill_all(borrow_indexes.iter()
.map(|b| ReserveOrActivateIndex::active(*b)));
sets.kill_all(borrow_indexes);
}
}

Expand All @@ -162,45 +143,29 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
.map(|activations| &activations[..])
.unwrap_or(&[])
}

/// Performs the activations for a given location
fn perform_activations_at_location(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
location: Location) {
// Handle activations
match self.borrow_set.activation_map.get(&location) {
Some(activations) => {
for activated in activations {
debug!("activating borrow {:?}", activated);
sets.gen(&ReserveOrActivateIndex::active(*activated))
}
}
None => {}
}
}
}

impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
type Idx = ReserveOrActivateIndex;
type Idx = BorrowIndex;
fn name() -> &'static str { "borrows" }
fn bits_per_block(&self) -> usize {
self.borrow_set.borrows.len() * 2
}

fn start_block_effect(&self, _entry_set: &mut IdxSet<ReserveOrActivateIndex>) {
fn start_block_effect(&self, _entry_set: &mut IdxSet<BorrowIndex>) {
// no borrows of code region_scopes have been taken prior to
// function execution, so this method has no effect on
// `_sets`.
}

fn before_statement_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
sets: &mut BlockSets<BorrowIndex>,
location: Location) {
debug!("Borrows::before_statement_effect sets: {:?} location: {:?}", sets, location);
self.kill_loans_out_of_scope_at_location(sets, location);
}

fn statement_effect(&self, sets: &mut BlockSets<ReserveOrActivateIndex>, location: Location) {
fn statement_effect(&self, sets: &mut BlockSets<BorrowIndex>, location: Location) {
debug!("Borrows::statement_effect sets: {:?} location: {:?}", sets, location);

let block = &self.mir.basic_blocks().get(location.block).unwrap_or_else(|| {
Expand All @@ -210,7 +175,6 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
panic!("could not find statement at location {:?}");
});

self.perform_activations_at_location(sets, location);
self.kill_loans_out_of_scope_at_location(sets, location);

match stmt.kind {
Expand All @@ -221,8 +185,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
{
assert!(self.nonlexical_regioncx.is_none());
for idx in borrow_indexes {
sets.kill(&ReserveOrActivateIndex::reserved(*idx));
sets.kill(&ReserveOrActivateIndex::active(*idx));
sets.kill(idx);
}
} else {
// (if there is no entry, then there are no borrows to be tracked)
Expand Down Expand Up @@ -252,14 +215,14 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
if let RegionKind::ReEmpty = region {
// If the borrowed value dies before the borrow is used, the region for
// the borrow can be empty. Don't track the borrow in that case.
sets.kill(&ReserveOrActivateIndex::active(*index));
sets.kill(&index);
return
}

assert!(self.borrow_set.region_map.get(region).unwrap_or_else(|| {
panic!("could not find BorrowIndexs for region {:?}", region);
}).contains(&index));
sets.gen(&ReserveOrActivateIndex::reserved(*index));
sets.gen(&index);

// Issue #46746: Two-phase borrows handles
// stmts of form `Tmp = &mut Borrow` ...
Expand All @@ -270,7 +233,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
// e.g. `box (&mut _)`. Current
// conservative solution: force
// immediate activation here.
sets.gen(&ReserveOrActivateIndex::active(*index));
sets.gen(&index);
}
}
}
Expand Down Expand Up @@ -306,21 +269,20 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
}

fn before_terminator_effect(&self,
sets: &mut BlockSets<ReserveOrActivateIndex>,
sets: &mut BlockSets<BorrowIndex>,
location: Location) {
debug!("Borrows::before_terminator_effect sets: {:?} location: {:?}", sets, location);
self.kill_loans_out_of_scope_at_location(sets, location);
}

fn terminator_effect(&self, sets: &mut BlockSets<ReserveOrActivateIndex>, location: Location) {
fn terminator_effect(&self, sets: &mut BlockSets<BorrowIndex>, location: Location) {
debug!("Borrows::terminator_effect sets: {:?} location: {:?}", sets, location);

let block = &self.mir.basic_blocks().get(location.block).unwrap_or_else(|| {
panic!("could not find block at location {:?}", location);
});

let term = block.terminator();
self.perform_activations_at_location(sets, location);
self.kill_loans_out_of_scope_at_location(sets, location);


Expand All @@ -343,8 +305,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
if *scope != root_scope &&
self.scope_tree.is_subscope_of(*scope, root_scope)
{
sets.kill(&ReserveOrActivateIndex::reserved(borrow_index));
sets.kill(&ReserveOrActivateIndex::active(borrow_index));
sets.kill(&borrow_index);
}
}
}
Expand All @@ -365,7 +326,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
}

fn propagate_call_return(&self,
_in_out: &mut IdxSet<ReserveOrActivateIndex>,
_in_out: &mut IdxSet<BorrowIndex>,
_call_bb: mir::BasicBlock,
_dest_bb: mir::BasicBlock,
_dest_place: &mir::Place) {
Expand Down
3 changes: 0 additions & 3 deletions src/librustc_mir/dataflow/move_paths/mod.rs
Expand Up @@ -65,9 +65,6 @@ pub(crate) mod indexes {

/// Index into Borrows.locations
new_index!(BorrowIndex, "bw");

/// Index into Reservations/Activations bitvector
new_index!(ReserveOrActivateIndex, "ra");
}

pub use self::indexes::MovePathIndex;
Expand Down

0 comments on commit a849da6

Please sign in to comment.