Skip to content

Commit 243bf3f

Browse files
committed
rename "free region" to "universally quantified region"
This has been bugging me. All the regions appear free in the source; the real difference is that some of them are universally quantified (those in the function signature) and some are existentially quantified (those for which we are inferring values).
1 parent fa813f7 commit 243bf3f

File tree

5 files changed

+88
-75
lines changed

5 files changed

+88
-75
lines changed

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use self::mir_util::PassWhere;
2626

2727
mod constraint_generation;
2828
mod subtype_constraint_generation;
29-
mod free_regions;
30-
use self::free_regions::FreeRegions;
29+
mod universal_regions;
30+
use self::universal_regions::UniversalRegions;
3131

3232
pub(crate) mod region_infer;
3333
use self::region_infer::RegionInferenceContext;
@@ -42,14 +42,14 @@ pub(in borrow_check) fn replace_regions_in_mir<'cx, 'gcx, 'tcx>(
4242
infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
4343
def_id: DefId,
4444
mir: &mut Mir<'tcx>,
45-
) -> FreeRegions<'tcx> {
45+
) -> UniversalRegions<'tcx> {
4646
// Compute named region information.
47-
let free_regions = free_regions::free_regions(infcx, def_id);
47+
let universal_regions = universal_regions::universal_regions(infcx, def_id);
4848

4949
// Replace all regions with fresh inference variables.
50-
renumber::renumber_mir(infcx, &free_regions, mir);
50+
renumber::renumber_mir(infcx, &universal_regions, mir);
5151

52-
free_regions
52+
universal_regions
5353
}
5454

5555
/// Computes the (non-lexical) regions from the input MIR.
@@ -58,7 +58,7 @@ pub(in borrow_check) fn replace_regions_in_mir<'cx, 'gcx, 'tcx>(
5858
pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
5959
infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
6060
def_id: DefId,
61-
free_regions: FreeRegions<'tcx>,
61+
universal_regions: UniversalRegions<'tcx>,
6262
mir: &Mir<'tcx>,
6363
param_env: ty::ParamEnv<'gcx>,
6464
flow_inits: &mut FlowInProgress<MaybeInitializedLvals<'cx, 'gcx, 'tcx>>,
@@ -71,8 +71,13 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
7171
// Create the region inference context, taking ownership of the region inference
7272
// data that was contained in `infcx`.
7373
let var_origins = infcx.take_region_var_origins();
74-
let mut regioncx = RegionInferenceContext::new(var_origins, &free_regions, mir);
75-
subtype_constraint_generation::generate(&mut regioncx, &free_regions, mir, constraint_sets);
74+
let mut regioncx = RegionInferenceContext::new(var_origins, &universal_regions, mir);
75+
subtype_constraint_generation::generate(
76+
&mut regioncx,
77+
&universal_regions,
78+
mir,
79+
constraint_sets,
80+
);
7681

7782
// Compute what is live where.
7883
let liveness = &LivenessResults {
@@ -178,8 +183,7 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
178183
writeln!(out, " | Live variables at {:?}: {}", location, s)?;
179184
}
180185

181-
PassWhere::AfterLocation(_) |
182-
PassWhere::AfterCFG => {}
186+
PassWhere::AfterLocation(_) | PassWhere::AfterCFG => {}
183187
}
184188
Ok(())
185189
});

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use super::free_regions::FreeRegions;
11+
use super::universal_regions::UniversalRegions;
1212
use rustc::infer::InferCtxt;
1313
use rustc::infer::RegionVariableOrigin;
1414
use rustc::infer::NLLRegionVariableOrigin;
@@ -33,8 +33,8 @@ pub struct RegionInferenceContext<'tcx> {
3333

3434
/// The liveness constraints added to each region. For most
3535
/// regions, these start out empty and steadily grow, though for
36-
/// each free region R they start out containing the entire CFG
37-
/// and `end(R)`.
36+
/// each universally quantified region R they start out containing
37+
/// the entire CFG and `end(R)`.
3838
///
3939
/// In this `BitMatrix` representation, the rows are the region
4040
/// variables and the columns are the free regions and MIR locations.
@@ -52,7 +52,10 @@ pub struct RegionInferenceContext<'tcx> {
5252
/// the free regions.)
5353
point_indices: BTreeMap<Location, usize>,
5454

55-
num_free_regions: usize,
55+
/// Number of universally quantified regions. This is used to
56+
/// determine the meaning of the bits in `inferred_values` and
57+
/// friends.
58+
num_universal_regions: usize,
5659

5760
free_region_map: &'tcx FreeRegionMap<'tcx>,
5861
}
@@ -92,10 +95,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
9295
/// Creates a new region inference context with a total of
9396
/// `num_region_variables` valid inference variables; the first N
9497
/// of those will be constant regions representing the free
95-
/// regions defined in `free_regions`.
96-
pub fn new(var_origins: VarOrigins, free_regions: &FreeRegions<'tcx>, mir: &Mir<'tcx>) -> Self {
98+
/// regions defined in `universal_regions`.
99+
pub fn new(
100+
var_origins: VarOrigins,
101+
universal_regions: &UniversalRegions<'tcx>,
102+
mir: &Mir<'tcx>,
103+
) -> Self {
97104
let num_region_variables = var_origins.len();
98-
let num_free_regions = free_regions.indices.len();
105+
let num_universal_regions = universal_regions.indices.len();
99106

100107
let mut num_points = 0;
101108
let mut point_indices = BTreeMap::new();
@@ -106,7 +113,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
106113
block,
107114
statement_index,
108115
};
109-
point_indices.insert(location, num_free_regions + num_points);
116+
point_indices.insert(location, num_universal_regions + num_points);
110117
num_points += 1;
111118
}
112119
}
@@ -121,25 +128,26 @@ impl<'tcx> RegionInferenceContext<'tcx> {
121128
definitions,
122129
liveness_constraints: BitMatrix::new(
123130
num_region_variables,
124-
num_free_regions + num_points,
131+
num_universal_regions + num_points,
125132
),
126133
inferred_values: None,
127134
constraints: Vec::new(),
128135
point_indices,
129-
num_free_regions,
130-
free_region_map: free_regions.free_region_map,
136+
num_universal_regions,
137+
free_region_map: universal_regions.free_region_map,
131138
};
132139

133-
result.init_free_regions(free_regions);
140+
result.init_universal_regions(universal_regions);
134141

135142
result
136143
}
137144

138-
/// Initializes the region variables for each free region
139-
/// (lifetime parameter). The first N variables always correspond
140-
/// to the free regions appearing in the function signature (both
141-
/// named and anonymous) and where clauses. This function iterates
142-
/// over those regions and initializes them with minimum values.
145+
/// Initializes the region variables for each universally
146+
/// quantified region (lifetime parameter). The first N variables
147+
/// always correspond to the regions appearing in the function
148+
/// signature (both named and anonymous) and where clauses. This
149+
/// function iterates over those regions and initializes them with
150+
/// minimum values.
143151
///
144152
/// For example:
145153
///
@@ -154,13 +162,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
154162
/// and (b) any free regions that it outlives, which in this case
155163
/// is just itself. R1 (`'b`) in contrast also outlives `'a` and
156164
/// hence contains R0 and R1.
157-
fn init_free_regions(&mut self, free_regions: &FreeRegions<'tcx>) {
158-
let FreeRegions {
165+
fn init_universal_regions(&mut self, universal_regions: &UniversalRegions<'tcx>) {
166+
let UniversalRegions {
159167
indices,
160168
free_region_map: _,
161-
} = free_regions;
169+
} = universal_regions;
162170

163-
// For each free region X:
171+
// For each universally quantified region X:
164172
for (free_region, &variable) in indices {
165173
// These should be free-region variables.
166174
assert!(match self.definitions[variable].origin {
@@ -218,7 +226,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
218226
&self,
219227
matrix: &BitMatrix,
220228
r: RegionVid,
221-
s: RegionVid
229+
s: RegionVid,
222230
) -> bool {
223231
matrix.contains(r.index(), s.index())
224232
}
@@ -240,7 +248,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
240248
}
241249
}
242250

243-
for fr in (0 .. self.num_free_regions).map(RegionVid::new) {
251+
for fr in (0..self.num_universal_regions).map(RegionVid::new) {
244252
if self.region_contains_region_in_matrix(inferred_values, r, fr) {
245253
result.push_str(&format!("{}{:?}", sep, fr));
246254
sep = ", ";
@@ -287,9 +295,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
287295
// Find the minimal regions that can solve the constraints. This is infallible.
288296
self.propagate_constraints(mir);
289297

290-
// Now, see whether any of the constraints were too strong. In particular,
291-
// we want to check for a case where a free region exceeded its bounds.
292-
// Consider:
298+
// Now, see whether any of the constraints were too strong. In
299+
// particular, we want to check for a case where a universally
300+
// quantified region exceeded its bounds. Consider:
293301
//
294302
// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
295303
//
@@ -300,7 +308,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
300308
// have no evidence that `'b` outlives `'a`, so we want to report
301309
// an error.
302310

303-
// The free regions are always found in a prefix of the full list.
311+
// The universal regions are always found in a prefix of the
312+
// full list.
304313
let free_region_definitions = self.definitions
305314
.iter_enumerated()
306315
.take_while(|(_, fr_definition)| fr_definition.name.is_some());
@@ -322,7 +331,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
322331

323332
// Find every region `o` such that `fr: o`
324333
// (because `fr` includes `end(o)`).
325-
for outlived_fr in fr_value.take_while(|&i| i < self.num_free_regions) {
334+
for outlived_fr in fr_value.take_while(|&i| i < self.num_universal_regions) {
326335
// `fr` includes `end(fr)`, that's not especially
327336
// interesting.
328337
if fr.index() == outlived_fr {
@@ -451,11 +460,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
451460
// If we reach the END point in the graph, then copy
452461
// over any skolemized end points in the `from_region`
453462
// and make sure they are included in the `to_region`.
454-
let free_region_indices = inferred_values
463+
let universal_region_indices = inferred_values
455464
.iter(from_region.index())
456-
.take_while(|&i| i < self.num_free_regions)
465+
.take_while(|&i| i < self.num_universal_regions)
457466
.collect::<Vec<_>>();
458-
for fr in &free_region_indices {
467+
for fr in &universal_region_indices {
459468
changed |= inferred_values.add(to_region.index(), *fr);
460469
}
461470
} else {
@@ -523,7 +532,7 @@ impl<'tcx> RegionDefinition<'tcx> {
523532
fn new(origin: RegionVariableOrigin) -> Self {
524533
// Create a new region definition. Note that, for free
525534
// regions, these fields get updated later in
526-
// `init_free_regions`.
535+
// `init_universal_regions`.
527536
Self { origin, name: None }
528537
}
529538
}

src/librustc_mir/borrow_check/nll/renumber.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ use rustc::mir::visit::{MutVisitor, TyContext};
1616
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
1717

1818
use super::ToRegionVid;
19-
use super::free_regions::FreeRegions;
19+
use super::universal_regions::UniversalRegions;
2020

2121
/// Replaces all free regions appearing in the MIR with fresh
2222
/// inference variables, returning the number of variables created.
2323
pub fn renumber_mir<'a, 'gcx, 'tcx>(
2424
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
25-
free_regions: &FreeRegions<'tcx>,
25+
universal_regions: &UniversalRegions<'tcx>,
2626
mir: &mut Mir<'tcx>,
2727
) {
2828
// Create inference variables for each of the free regions
2929
// declared on the function signature.
30-
let free_region_inference_vars = (0..free_regions.indices.len())
30+
let free_region_inference_vars = (0..universal_regions.indices.len())
3131
.map(RegionVid::new)
3232
.map(|vid_expected| {
3333
let r = infcx.next_nll_region_var(NLLRegionVariableOrigin::FreeRegion);
@@ -37,12 +37,12 @@ pub fn renumber_mir<'a, 'gcx, 'tcx>(
3737
.collect();
3838

3939
debug!("renumber_mir()");
40-
debug!("renumber_mir: free_regions={:#?}", free_regions);
40+
debug!("renumber_mir: universal_regions={:#?}", universal_regions);
4141
debug!("renumber_mir: mir.arg_count={:?}", mir.arg_count);
4242

4343
let mut visitor = NLLVisitor {
4444
infcx,
45-
free_regions,
45+
universal_regions,
4646
free_region_inference_vars,
4747
arg_count: mir.arg_count,
4848
};
@@ -51,7 +51,7 @@ pub fn renumber_mir<'a, 'gcx, 'tcx>(
5151

5252
struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
5353
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
54-
free_regions: &'a FreeRegions<'tcx>,
54+
universal_regions: &'a UniversalRegions<'tcx>,
5555
free_region_inference_vars: IndexVec<RegionVid, ty::Region<'tcx>>,
5656
arg_count: usize,
5757
}
@@ -76,16 +76,16 @@ impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> {
7676

7777
/// Renumbers the regions appearing in `value`, but those regions
7878
/// are expected to be free regions from the function signature.
79-
fn renumber_free_regions<T>(&mut self, value: &T) -> T
79+
fn renumber_universal_regions<T>(&mut self, value: &T) -> T
8080
where
8181
T: TypeFoldable<'tcx>,
8282
{
83-
debug!("renumber_free_regions(value={:?})", value);
83+
debug!("renumber_universal_regions(value={:?})", value);
8484

8585
self.infcx
8686
.tcx
8787
.fold_regions(value, &mut false, |region, _depth| {
88-
let index = self.free_regions.indices[&region];
88+
let index = self.universal_regions.indices[&region];
8989
self.free_region_inference_vars[index]
9090
})
9191
}
@@ -112,7 +112,7 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
112112

113113
let old_ty = *ty;
114114
*ty = if is_arg {
115-
self.renumber_free_regions(&old_ty)
115+
self.renumber_universal_regions(&old_ty)
116116
} else {
117117
self.renumber_regions(ty_context, &old_ty)
118118
};

src/librustc_mir/borrow_check/nll/subtype_constraint_generation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc::ty;
1515
use transform::type_check::MirTypeckRegionConstraints;
1616
use transform::type_check::OutlivesSet;
1717

18-
use super::free_regions::FreeRegions;
18+
use super::universal_regions::UniversalRegions;
1919
use super::region_infer::RegionInferenceContext;
2020

2121
/// When the MIR type-checker executes, it validates all the types in
@@ -25,20 +25,20 @@ use super::region_infer::RegionInferenceContext;
2525
/// them into the NLL `RegionInferenceContext`.
2626
pub(super) fn generate<'tcx>(
2727
regioncx: &mut RegionInferenceContext<'tcx>,
28-
free_regions: &FreeRegions<'tcx>,
28+
universal_regions: &UniversalRegions<'tcx>,
2929
mir: &Mir<'tcx>,
3030
constraints: &MirTypeckRegionConstraints<'tcx>,
3131
) {
3232
SubtypeConstraintGenerator {
3333
regioncx,
34-
free_regions,
34+
universal_regions,
3535
mir,
3636
}.generate(constraints);
3737
}
3838

3939
struct SubtypeConstraintGenerator<'cx, 'tcx: 'cx> {
4040
regioncx: &'cx mut RegionInferenceContext<'tcx>,
41-
free_regions: &'cx FreeRegions<'tcx>,
41+
universal_regions: &'cx UniversalRegions<'tcx>,
4242
mir: &'cx Mir<'tcx>,
4343
}
4444

@@ -102,11 +102,11 @@ impl<'cx, 'tcx> SubtypeConstraintGenerator<'cx, 'tcx> {
102102
// Every region that we see in the constraints came from the
103103
// MIR or from the parameter environment. If the former, it
104104
// will be a region variable. If the latter, it will be in
105-
// the set of free regions *somewhere*.
105+
// the set of universal regions *somewhere*.
106106
if let ty::ReVar(vid) = r {
107107
*vid
108108
} else {
109-
self.free_regions.indices[&r]
109+
self.universal_regions.indices[&r]
110110
}
111111
}
112112
}

0 commit comments

Comments
 (0)