Skip to content

Commit 0bb2b66

Browse files
committed
Rename lifetimes on ConstraintGeneration, move macro impl to HasMoveData impl, ensure mir is immutably borrowed after renumbering, and add find_local fn to simplify mpi lookup in constraint generation
1 parent ce8e837 commit 0bb2b66

File tree

3 files changed

+52
-54
lines changed

3 files changed

+52
-54
lines changed

src/librustc_mir/borrow_check/mod.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
8181
// contain non-lexical lifetimes. It will have a lifetime tied
8282
// to the inference context.
8383
let mut mir: Mir<'tcx> = input_mir.clone();
84-
let mir = &mut mir;
84+
let free_regions = {
85+
let mir = &mut mir;
8586

86-
// Replace all regions with fresh inference variables.
87-
let free_regions = nll::replace_regions_in_mir(infcx, src, mir);
87+
// Replace all regions with fresh inference variables.
88+
nll::replace_regions_in_mir(infcx, src, mir)
89+
};
90+
let mir = &mir;
8891

8992
let move_data: MoveData<'tcx> = match MoveData::gather_moves(mir, tcx, param_env) {
9093
Ok(move_data) => move_data,
@@ -1486,42 +1489,35 @@ impl<'b, 'gcx, 'tcx> InProgress<'b, 'gcx, 'tcx> {
14861489
}
14871490
}
14881491

1489-
macro_rules! has_any_child_of_impl {
1490-
($MaybeTLvals:ident) => {
1491-
impl<'b, 'gcx, 'tcx> FlowInProgress<$MaybeTLvals<'b, 'gcx, 'tcx>> {
1492-
fn has_any_child_of(&self, mpi: MovePathIndex) -> Option<MovePathIndex> {
1493-
let move_data = self.base_results.operator().move_data();
1494-
1495-
let mut todo = vec![mpi];
1496-
let mut push_siblings = false; // don't look at siblings of original `mpi`.
1497-
while let Some(mpi) = todo.pop() {
1498-
if self.curr_state.contains(&mpi) {
1499-
return Some(mpi);
1500-
}
1501-
let move_path = &move_data.move_paths[mpi];
1502-
if let Some(child) = move_path.first_child {
1503-
todo.push(child);
1504-
}
1505-
if push_siblings {
1506-
if let Some(sibling) = move_path.next_sibling {
1507-
todo.push(sibling);
1508-
}
1509-
} else {
1510-
// after we've processed the original `mpi`, we should
1511-
// always traverse the siblings of any of its
1512-
// children.
1513-
push_siblings = true;
1514-
}
1492+
impl<'tcx, T> FlowInProgress<T> where T: HasMoveData<'tcx> + BitDenotation<Idx = MovePathIndex> {
1493+
fn has_any_child_of(&self, mpi: T::Idx) -> Option<T::Idx> {
1494+
let move_data = self.base_results.operator().move_data();
1495+
1496+
let mut todo = vec![mpi];
1497+
let mut push_siblings = false; // don't look at siblings of original `mpi`.
1498+
while let Some(mpi) = todo.pop() {
1499+
if self.curr_state.contains(&mpi) {
1500+
return Some(mpi);
1501+
}
1502+
let move_path = &move_data.move_paths[mpi];
1503+
if let Some(child) = move_path.first_child {
1504+
todo.push(child);
1505+
}
1506+
if push_siblings {
1507+
if let Some(sibling) = move_path.next_sibling {
1508+
todo.push(sibling);
15151509
}
1516-
return None;
1510+
} else {
1511+
// after we've processed the original `mpi`, we should
1512+
// always traverse the siblings of any of its
1513+
// children.
1514+
push_siblings = true;
15171515
}
15181516
}
1519-
};
1517+
return None;
1518+
}
15201519
}
15211520

1522-
has_any_child_of_impl!(MaybeInitializedLvals);
1523-
has_any_child_of_impl!(MaybeUninitializedLvals);
1524-
15251521
impl<BD> FlowInProgress<BD> where BD: BitDenotation {
15261522
fn each_state_bit<F>(&self, f: F) where F: FnMut(BD::Idx) {
15271523
self.curr_state.each_bit(self.base_results.operator().bits_per_block(), f)

src/librustc_mir/borrow_check/nll/constraint_generation.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ use rustc_data_structures::fx::FxHashSet;
2323
use syntax::codemap::DUMMY_SP;
2424
use borrow_check::FlowInProgress;
2525
use dataflow::MaybeInitializedLvals;
26-
use dataflow::move_paths::{MoveData, LookupResult};
26+
use dataflow::move_paths::MoveData;
2727

2828
use super::LivenessResults;
2929
use super::ToRegionVid;
3030
use super::region_infer::RegionInferenceContext;
3131

32-
pub(super) fn generate_constraints<'a, 'gcx, 'tcx>(
33-
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
32+
pub(super) fn generate_constraints<'cx, 'gcx, 'tcx>(
33+
infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
3434
regioncx: &mut RegionInferenceContext<'tcx>,
3535
mir: &Mir<'tcx>,
3636
mir_source: MirSource,
3737
liveness: &LivenessResults,
38-
flow_inits: &FlowInProgress<MaybeInitializedLvals<'a, 'gcx, 'tcx>>,
38+
flow_inits: &FlowInProgress<MaybeInitializedLvals<'cx, 'gcx, 'tcx>>,
3939
move_data: &MoveData<'tcx>,
4040
) {
4141
ConstraintGeneration {
@@ -49,17 +49,18 @@ pub(super) fn generate_constraints<'a, 'gcx, 'tcx>(
4949
}.add_constraints();
5050
}
5151

52-
struct ConstraintGeneration<'a, 'cx: 'a, 'gcx: 'tcx, 'tcx: 'cx> {
53-
infcx: &'a InferCtxt<'cx, 'gcx, 'tcx>,
54-
regioncx: &'a mut RegionInferenceContext<'tcx>,
55-
mir: &'a Mir<'tcx>,
56-
liveness: &'a LivenessResults,
52+
/// 'cg = the duration of the constraint generation process itself.
53+
struct ConstraintGeneration<'cg, 'cx: 'cg, 'gcx: 'tcx, 'tcx: 'cx> {
54+
infcx: &'cg InferCtxt<'cx, 'gcx, 'tcx>,
55+
regioncx: &'cg mut RegionInferenceContext<'tcx>,
56+
mir: &'cg Mir<'tcx>,
57+
liveness: &'cg LivenessResults,
5758
mir_source: MirSource,
58-
flow_inits: &'a FlowInProgress<MaybeInitializedLvals<'cx, 'gcx, 'tcx>>,
59-
move_data: &'a MoveData<'tcx>,
59+
flow_inits: &'cg FlowInProgress<MaybeInitializedLvals<'cx, 'gcx, 'tcx>>,
60+
move_data: &'cg MoveData<'tcx>,
6061
}
6162

62-
impl<'a, 'cx, 'gcx, 'tcx> ConstraintGeneration<'a, 'cx, 'gcx, 'tcx> {
63+
impl<'cx, 'cg, 'gcx, 'tcx> ConstraintGeneration<'cx, 'cg, 'gcx, 'tcx> {
6364
fn add_constraints(&mut self) {
6465
self.add_liveness_constraints();
6566
self.add_borrow_constraints();
@@ -88,13 +89,10 @@ impl<'a, 'cx, 'gcx, 'tcx> ConstraintGeneration<'a, 'cx, 'gcx, 'tcx> {
8889
bb,
8990
|location, live_locals| {
9091
for live_local in live_locals.iter() {
91-
if let LookupResult::Exact(mpi) =
92-
self.move_data.rev_lookup.find(&Lvalue::Local(live_local))
93-
{
94-
if self.flow_inits.has_any_child_of(mpi).is_some() {
95-
let live_local_ty = self.mir.local_decls[live_local].ty;
96-
self.add_drop_live_constraint(live_local_ty, location);
97-
}
92+
let mpi = self.move_data.rev_lookup.find_local(live_local);
93+
if self.flow_inits.has_any_child_of(mpi).is_some() {
94+
let live_local_ty = self.mir.local_decls[live_local].ty;
95+
self.add_drop_live_constraint(live_local_ty, location);
9896
}
9997
}
10098
},
@@ -232,7 +230,7 @@ impl<'a, 'cx, 'gcx, 'tcx> ConstraintGeneration<'a, 'cx, 'gcx, 'tcx> {
232230
}
233231
}
234232

235-
impl<'a, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'a, 'cx, 'gcx, 'tcx> {
233+
impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx, 'tcx> {
236234
fn visit_rvalue(&mut self,
237235
rvalue: &Rvalue<'tcx>,
238236
location: Location) {

src/librustc_mir/dataflow/move_paths/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ impl<'tcx> MovePathLookup<'tcx> {
226226
}
227227
}
228228
}
229+
230+
pub fn find_local(&self, local: Local) -> MovePathIndex {
231+
self.locals[local]
232+
}
229233
}
230234

231235
#[derive(Debug)]

0 commit comments

Comments
 (0)