Skip to content
/ rust Public
forked from rust-lang/rust

Commit 883e746

Browse files
committed
Auto merge of rust-lang#48770 - bobtwinkles:two_phase_borrows_rewrite, r=pnkfelix
Two phase borrows rewrite This definitely needs a careful review. Both @pnkfelix and @nikomatsakis were involved with the design of this so they're natural choices here. I'm r?'ing @pnkfelix since they wrote the original two-phase borrow implementation. Also ping @KiChjang who expressed interest in working on this. I'm going to leave a few comments below pointing out some of the more dangerous changes I made (i.e. what I would like reviewers to pay special attention too.) r? @pnkfelix
2 parents 222b0eb + 9a5d61a commit 883e746

13 files changed

+572
-600
lines changed

src/libgraphviz/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,12 @@ impl<'a> IntoCow<'a, str> for &'a str {
711711
}
712712
}
713713

714+
impl<'a> IntoCow<'a, str> for Cow<'a, str> {
715+
fn into_cow(self) -> Cow<'a, str> {
716+
self
717+
}
718+
}
719+
714720
impl<'a, T: Clone> IntoCow<'a, [T]> for Vec<T> {
715721
fn into_cow(self) -> Cow<'a, [T]> {
716722
Cow::Owned(self)

src/librustc_mir/borrow_check/error_reporting.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_data_structures::sync::Lrc;
1818

1919
use super::{Context, MirBorrowckCtxt};
2020
use super::{InitializationRequiringAction, PrefixSet};
21-
use dataflow::{ActiveBorrows, BorrowData, FlowAtLocation, MovingOutStatements};
21+
use dataflow::{Borrows, BorrowData, FlowAtLocation, MovingOutStatements};
2222
use dataflow::move_paths::MovePathIndex;
2323
use util::borrowck_errors::{BorrowckErrors, Origin};
2424

@@ -250,7 +250,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
250250

251251
let new_closure_span = self.find_closure_span(span, context.loc);
252252
let span = new_closure_span.map(|(args, _)| args).unwrap_or(span);
253-
let old_closure_span = self.find_closure_span(issued_span, issued_borrow.location);
253+
let old_closure_span = self.find_closure_span(issued_span, issued_borrow.reserve_location);
254254
let issued_span = old_closure_span
255255
.map(|(args, _)| args)
256256
.unwrap_or(issued_span);
@@ -372,15 +372,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
372372
context: Context,
373373
borrow: &BorrowData<'tcx>,
374374
drop_span: Span,
375-
borrows: &ActiveBorrows<'cx, 'gcx, 'tcx>,
375+
borrows: &Borrows<'cx, 'gcx, 'tcx>
376376
) {
377377
let end_span = borrows.opt_region_end_span(&borrow.region);
378-
let scope_tree = borrows.0.scope_tree();
378+
let scope_tree = borrows.scope_tree();
379379
let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All)
380380
.last()
381381
.unwrap();
382382

383-
let borrow_span = self.mir.source_info(borrow.location).span;
383+
let borrow_span = self.mir.source_info(borrow.reserve_location).span;
384384
let proper_span = match *root_place {
385385
Place::Local(local) => self.mir.local_decls[local].source_info.span,
386386
_ => drop_span,
@@ -817,7 +817,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
817817

818818
// Retrieve span of given borrow from the current MIR representation
819819
pub fn retrieve_borrow_span(&self, borrow: &BorrowData) -> Span {
820-
self.mir.source_info(borrow.location).span
820+
self.mir.source_info(borrow.reserve_location).span
821821
}
822822

823823
// Retrieve type of a place for the current MIR representation

src/librustc_mir/borrow_check/flows.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ use rustc::mir::{BasicBlock, Location};
1717

1818
use dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
1919
use dataflow::{EverInitializedPlaces, MovingOutStatements};
20-
use dataflow::{ActiveBorrows, FlowAtLocation, FlowsAtLocation};
20+
use dataflow::{Borrows};
21+
use dataflow::{FlowAtLocation, FlowsAtLocation};
2122
use dataflow::move_paths::HasMoveData;
2223
use std::fmt;
2324

2425
// (forced to be `pub` due to its use as an associated type below.)
2526
pub(crate) struct Flows<'b, 'gcx: 'tcx, 'tcx: 'b> {
26-
pub borrows: FlowAtLocation<ActiveBorrows<'b, 'gcx, 'tcx>>,
27+
pub borrows: FlowAtLocation<Borrows<'b, 'gcx, 'tcx>>,
2728
pub inits: FlowAtLocation<MaybeInitializedPlaces<'b, 'gcx, 'tcx>>,
2829
pub uninits: FlowAtLocation<MaybeUninitializedPlaces<'b, 'gcx, 'tcx>>,
2930
pub move_outs: FlowAtLocation<MovingOutStatements<'b, 'gcx, 'tcx>>,
@@ -32,7 +33,7 @@ pub(crate) struct Flows<'b, 'gcx: 'tcx, 'tcx: 'b> {
3233

3334
impl<'b, 'gcx, 'tcx> Flows<'b, 'gcx, 'tcx> {
3435
pub fn new(
35-
borrows: FlowAtLocation<ActiveBorrows<'b, 'gcx, 'tcx>>,
36+
borrows: FlowAtLocation<Borrows<'b, 'gcx, 'tcx>>,
3637
inits: FlowAtLocation<MaybeInitializedPlaces<'b, 'gcx, 'tcx>>,
3738
uninits: FlowAtLocation<MaybeUninitializedPlaces<'b, 'gcx, 'tcx>>,
3839
move_outs: FlowAtLocation<MovingOutStatements<'b, 'gcx, 'tcx>>,

src/librustc_mir/borrow_check/mod.rs

+15-38
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ use syntax_pos::Span;
3434
use dataflow::{do_dataflow, DebugFormatted};
3535
use dataflow::FlowAtLocation;
3636
use dataflow::MoveDataParamEnv;
37-
use dataflow::{DataflowAnalysis, DataflowResultsConsumer};
37+
use dataflow::{DataflowResultsConsumer};
3838
use dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
3939
use dataflow::{EverInitializedPlaces, MovingOutStatements};
4040
use dataflow::{BorrowData, Borrows, ReserveOrActivateIndex};
41-
use dataflow::{ActiveBorrows, Reservations};
4241
use dataflow::indexes::BorrowIndex;
4342
use dataflow::move_paths::{IllegalMoveOriginKind, MoveError};
4443
use dataflow::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex};
@@ -54,8 +53,6 @@ mod error_reporting;
5453
mod flows;
5554
mod prefixes;
5655

57-
use std::borrow::Cow;
58-
5956
pub(crate) mod nll;
6057

6158
pub fn provide(providers: &mut Providers) {
@@ -209,6 +206,18 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
209206
};
210207
let flow_inits = flow_inits; // remove mut
211208

209+
let flow_borrows = FlowAtLocation::new(do_dataflow(
210+
tcx,
211+
mir,
212+
id,
213+
&attributes,
214+
&dead_unwinds,
215+
Borrows::new(tcx, mir, opt_regioncx.clone(), def_id, body_id),
216+
|rs, i| {
217+
DebugFormatted::new(&(i.kind(), rs.location(i.borrow_index())))
218+
}
219+
));
220+
212221
let movable_generator = !match tcx.hir.get(id) {
213222
hir::map::Node::NodeExpr(&hir::Expr {
214223
node: hir::ExprClosure(.., Some(hir::GeneratorMovability::Static)),
@@ -230,44 +239,12 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
230239
},
231240
access_place_error_reported: FxHashSet(),
232241
reservation_error_reported: FxHashSet(),
233-
nonlexical_regioncx: opt_regioncx.clone(),
242+
nonlexical_regioncx: opt_regioncx,
234243
nonlexical_cause_info: None,
235244
};
236245

237-
let borrows = Borrows::new(tcx, mir, opt_regioncx, def_id, body_id);
238-
let flow_reservations = do_dataflow(
239-
tcx,
240-
mir,
241-
id,
242-
&attributes,
243-
&dead_unwinds,
244-
Reservations::new(borrows),
245-
|rs, i| {
246-
// In principle we could make the dataflow ensure that
247-
// only reservation bits show up, and assert so here.
248-
//
249-
// In practice it is easier to be looser; in particular,
250-
// it is okay for the kill-sets to hold activation bits.
251-
DebugFormatted::new(&(i.kind(), rs.location(i)))
252-
},
253-
);
254-
let flow_active_borrows = {
255-
let reservations_on_entry = flow_reservations.0.sets.entry_set_state();
256-
let reservations = flow_reservations.0.operator;
257-
let a = DataflowAnalysis::new_with_entry_sets(
258-
mir,
259-
&dead_unwinds,
260-
Cow::Borrowed(reservations_on_entry),
261-
ActiveBorrows::new(reservations),
262-
);
263-
let results = a.run(tcx, id, &attributes, |ab, i| {
264-
DebugFormatted::new(&(i.kind(), ab.location(i)))
265-
});
266-
FlowAtLocation::new(results)
267-
};
268-
269246
let mut state = Flows::new(
270-
flow_active_borrows,
247+
flow_borrows,
271248
flow_inits,
272249
flow_uninits,
273250
flow_move_outs,

0 commit comments

Comments
 (0)