Skip to content

Commit a164ae0

Browse files
committed
Take ownership of RegionInferenceContext in Borrows struct
1 parent d59af53 commit a164ae0

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

src/librustc_mir/borrow_check/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
8484
let mir = &mut mir;
8585

8686
// Replace all regions with fresh inference variables.
87-
let num_region_variables = nll::renumber::renumber_mir(infcx, mir);
87+
let free_regions = nll::replace_regions_in_mir(infcx, src, mir);
8888

8989
let move_data: MoveData<'tcx> = match MoveData::gather_moves(mir, tcx, param_env) {
9090
Ok(move_data) => move_data,
@@ -127,7 +127,13 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
127127
let opt_regioncx = if !tcx.sess.opts.debugging_opts.nll {
128128
None
129129
} else {
130-
Some(nll::compute_regions(infcx, src, param_env, mir, num_region_variables, &flow_inits, &mdpe.move_data))
130+
Some(nll::compute_regions(infcx,
131+
src,
132+
&free_regions,
133+
mir,
134+
param_env,
135+
&flow_inits,
136+
&mdpe.move_data))
131137
};
132138

133139
let mut mbcx = MirBorrowckCtxt {
@@ -139,7 +145,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
139145
};
140146

141147
let flow_borrows = FlowInProgress::new(do_dataflow(tcx, mir, id, &attributes, &dead_unwinds,
142-
Borrows::new(tcx, mir, opt_regioncx.as_ref()),
148+
Borrows::new(tcx, mir, opt_regioncx),
143149
|bd, i| bd.location(i)));
144150

145151
let mut state = InProgress::new(flow_borrows,

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,53 @@ use self::mir_util::PassWhere;
2626
mod constraint_generation;
2727
mod subtype_constraint_generation;
2828
mod free_regions;
29+
use self::free_regions::FreeRegions;
2930

3031
pub(crate) mod region_infer;
3132
use self::region_infer::RegionInferenceContext;
3233

33-
pub(super) mod renumber;
34+
mod renumber;
35+
36+
/// Rewrites the regions in the MIR to use NLL variables, also
37+
/// scraping out the set of free regions (e.g., region parameters)
38+
/// declared on the function. That set will need to be given to
39+
/// `compute_regions`.
40+
pub(super) fn replace_regions_in_mir<'a, 'gcx, 'tcx>(
41+
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
42+
source: MirSource,
43+
mir: &mut Mir<'tcx>
44+
) -> FreeRegions<'tcx> {
45+
// Compute named region information.
46+
let free_regions = free_regions::free_regions(infcx, source);
47+
48+
// Replace all regions with fresh inference variables.
49+
renumber::renumber_mir(infcx, &free_regions, mir);
50+
51+
free_regions
52+
}
3453

3554
/// Computes the (non-lexical) regions from the input MIR.
3655
///
3756
/// This may result in errors being reported.
3857
pub(super) fn compute_regions<'a, 'gcx, 'tcx>(
3958
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
4059
source: MirSource,
60+
free_regions: &FreeRegions<'tcx>,
4161
mir: &Mir<'tcx>,
4262
param_env: ty::ParamEnv<'gcx>,
43-
num_region_variables: usize,
4463
flow_inits: &FlowInProgress<MaybeInitializedLvals<'a, 'gcx, 'tcx>>,
4564
move_data: &MoveData<'tcx>,
46-
) -> RegionInferenceContext {
65+
) -> RegionInferenceContext<'tcx> {
66+
// Run the MIR type-checker.
67+
let body_id = source.item_id();
68+
let constraint_sets = &type_check::type_check(infcx, body_id, param_env, mir);
69+
70+
// Create the region inference context, taking ownership of the region inference
71+
// data that was contained in `infcx`.
72+
let var_origins = infcx.take_region_var_origins();
73+
let mut regioncx = RegionInferenceContext::new(var_origins, free_regions, mir);
74+
subtype_constraint_generation::generate(&mut regioncx, free_regions, mir, constraint_sets);
75+
4776
// Compute what is live where.
4877
let liveness = &LivenessResults {
4978
regular: liveness::liveness_of_locals(

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct Borrows<'a, 'gcx: 'tcx, 'tcx: 'a> {
3838
location_map: FxHashMap<Location, BorrowIndex>,
3939
region_map: FxHashMap<Region<'tcx>, FxHashSet<BorrowIndex>>,
4040
region_span_map: FxHashMap<RegionKind, Span>,
41-
nonlexical_regioncx: Option<&'a RegionInferenceContext<'tcx>>,
41+
nonlexical_regioncx: Option<RegionInferenceContext<'tcx>>,
4242
}
4343

4444
// temporarily allow some dead fields: `kind` and `region` will be
@@ -69,7 +69,7 @@ impl<'tcx> fmt::Display for BorrowData<'tcx> {
6969
impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
7070
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
7171
mir: &'a Mir<'tcx>,
72-
nonlexical_regioncx: Option<&'a RegionInferenceContext<'tcx>>)
72+
nonlexical_regioncx: Option<RegionInferenceContext<'tcx>>)
7373
-> Self {
7474
let mut visitor = GatherBorrows { idx_vec: IndexVec::new(),
7575
location_map: FxHashMap(),
@@ -137,7 +137,7 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
137137
fn kill_loans_out_of_scope_at_location(&self,
138138
sets: &mut BlockSets<BorrowIndex>,
139139
location: Location) {
140-
if let Some(regioncx) = self.nonlexical_regioncx {
140+
if let Some(ref regioncx) = self.nonlexical_regioncx {
141141
for (borrow_index, borrow_data) in self.borrows.iter_enumerated() {
142142
let borrow_region = borrow_data.region.to_region_vid();
143143
if !regioncx.region_contains_point(borrow_region, location) {

0 commit comments

Comments
 (0)