Skip to content

Commit

Permalink
don't expose the borrows field
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Apr 15, 2018
1 parent 033c4f2 commit 818ae6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/librustc_mir/borrow_check/flows.rs
Expand Up @@ -14,25 +14,27 @@
//! but is not as ugly as it is right now.

use rustc::mir::{BasicBlock, Location};
use rustc_data_structures::indexed_set::Iter;

use dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
use dataflow::{EverInitializedPlaces, MovingOutStatements};
use dataflow::{Borrows};
use dataflow::{FlowAtLocation, FlowsAtLocation};
use dataflow::move_paths::HasMoveData;
use dataflow::move_paths::indexes::BorrowIndex;
use std::fmt;

// (forced to be `pub` due to its use as an associated type below.)
pub(crate) struct Flows<'b, 'gcx: 'tcx, 'tcx: 'b> {
pub borrows: FlowAtLocation<Borrows<'b, 'gcx, 'tcx>>,
crate struct Flows<'b, 'gcx: 'tcx, 'tcx: 'b> {
borrows: FlowAtLocation<Borrows<'b, 'gcx, 'tcx>>,
pub inits: FlowAtLocation<MaybeInitializedPlaces<'b, 'gcx, 'tcx>>,
pub uninits: FlowAtLocation<MaybeUninitializedPlaces<'b, 'gcx, 'tcx>>,
pub move_outs: FlowAtLocation<MovingOutStatements<'b, 'gcx, 'tcx>>,
pub ever_inits: FlowAtLocation<EverInitializedPlaces<'b, 'gcx, 'tcx>>,
}

impl<'b, 'gcx, 'tcx> Flows<'b, 'gcx, 'tcx> {
pub fn new(
crate fn new(
borrows: FlowAtLocation<Borrows<'b, 'gcx, 'tcx>>,
inits: FlowAtLocation<MaybeInitializedPlaces<'b, 'gcx, 'tcx>>,
uninits: FlowAtLocation<MaybeUninitializedPlaces<'b, 'gcx, 'tcx>>,
Expand All @@ -47,6 +49,14 @@ impl<'b, 'gcx, 'tcx> Flows<'b, 'gcx, 'tcx> {
ever_inits,
}
}

crate fn borrows_in_scope(&self) -> impl Iterator<Item = BorrowIndex> + '_ {
self.borrows.iter_incoming()
}

crate fn with_outgoing_borrows(&self, op: impl FnOnce(Iter<BorrowIndex>)) {
self.borrows.with_iter_outgoing(op)
}
}

macro_rules! each_flow {
Expand Down
13 changes: 6 additions & 7 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -550,7 +550,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
if self.movable_generator {
// Look for any active borrows to locals
let borrow_set = self.borrow_set.clone();
flow_state.borrows.with_iter_outgoing(|borrows| {
flow_state.with_outgoing_borrows(|borrows| {
for i in borrows {
let borrow = &borrow_set[i];
self.check_for_local_borrow(borrow, span);
Expand All @@ -565,7 +565,7 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
// StorageDead, but we don't always emit those (notably on unwind paths),
// so this "extra check" serves as a kind of backup.
let borrow_set = self.borrow_set.clone();
flow_state.borrows.with_iter_outgoing(|borrows| {
flow_state.with_outgoing_borrows(|borrows| {
for i in borrows {
let borrow = &borrow_set[i];
let context = ContextKind::StorageDead.new(loc);
Expand Down Expand Up @@ -2224,10 +2224,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
unreachable!("iter::repeat returned None")
}

/// This function iterates over all of the current borrows
/// (represented by 1-bits in `flow_state.borrows`) that conflict
/// with an access to a place, invoking the `op` callback for each
/// one.
/// This function iterates over all of the in-scope borrows that
/// conflict with an access to a place, invoking the `op` callback
/// for each one.
///
/// "Current borrow" here means a borrow that reaches the point in
/// the control-flow where the access occurs.
Expand All @@ -2251,7 +2250,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
// check for loan restricting path P being used. Accounts for
// borrows of P, P.a.b, etc.
let borrow_set = self.borrow_set.clone();
for i in flow_state.borrows.iter_incoming() {
for i in flow_state.borrows_in_scope() {
let borrowed = &borrow_set[i];

if self.places_conflict(&borrowed.borrowed_place, place, access) {
Expand Down

0 comments on commit 818ae6f

Please sign in to comment.