Skip to content

Commit 66c5d5b

Browse files
committed
Privatize the fields of RegionInferenceContext
1 parent 786db73 commit 66c5d5b

File tree

4 files changed

+71
-38
lines changed

4 files changed

+71
-38
lines changed

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
113113
/// one (e.g., this is just some random part of the CFG).
114114
pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
115115
self.to_error_region_vid(r)
116-
.and_then(|r| self.nonlexical_regioncx.definitions[r].external_name)
116+
.and_then(|r| self.nonlexical_regioncx.region_definition(r).external_name)
117117
}
118118

119119
/// Returns the `RegionVid` corresponding to the region returned by
120120
/// `to_error_region`.
121121
pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
122-
if self.nonlexical_regioncx.universal_regions.is_universal_region(r) {
122+
if self.nonlexical_regioncx.universal_regions().is_universal_region(r) {
123123
Some(r)
124124
} else {
125-
let r_scc = self.nonlexical_regioncx.constraint_sccs.scc(r);
126125
let upper_bound = self.nonlexical_regioncx.universal_upper_bound(r);
127-
if self.nonlexical_regioncx.scc_values.contains(r_scc, upper_bound) {
126+
127+
if self.nonlexical_regioncx.upper_bound_in_region_scc(r, upper_bound) {
128128
self.to_error_region_vid(upper_bound)
129129
} else {
130130
None
@@ -137,7 +137,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
137137
if let Some(ty::ReFree(free_region)) = self.to_error_region(fr) {
138138
if let ty::BoundRegion::BrEnv = free_region.bound_region {
139139
if let DefiningTy::Closure(def_id, substs) =
140-
self.nonlexical_regioncx.universal_regions.defining_ty
140+
self.nonlexical_regioncx.universal_regions().defining_ty
141141
{
142142
let closure_kind_ty = substs.as_closure().kind_ty(def_id, self.infcx.tcx);
143143
return Some(ty::ClosureKind::FnMut) == closure_kind_ty.to_opt_closure_kind();
@@ -302,8 +302,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
302302
}
303303

304304
let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
305-
self.nonlexical_regioncx.universal_regions.is_local_free_region(fr),
306-
self.nonlexical_regioncx.universal_regions.is_local_free_region(outlived_fr),
305+
self.nonlexical_regioncx.universal_regions().is_local_free_region(fr),
306+
self.nonlexical_regioncx.universal_regions().is_local_free_region(outlived_fr),
307307
);
308308

309309
debug!(
@@ -378,7 +378,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
378378
// We should check if the return type of this closure is in fact a closure - in that
379379
// case, we can special case the error further.
380380
let return_type_is_closure =
381-
self.nonlexical_regioncx.universal_regions.unnormalized_output_ty.is_closure();
381+
self.nonlexical_regioncx.universal_regions().unnormalized_output_ty.is_closure();
382382
let message = if return_type_is_closure {
383383
"returns a closure that contains a reference to a captured variable, which then \
384384
escapes the closure body"
@@ -445,7 +445,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
445445
errci.outlived_fr,
446446
);
447447

448-
let escapes_from = match self.nonlexical_regioncx.universal_regions.defining_ty {
448+
let escapes_from = match self.nonlexical_regioncx.universal_regions().defining_ty {
449449
DefiningTy::Closure(..) => "closure",
450450
DefiningTy::Generator(..) => "generator",
451451
DefiningTy::FnDef(..) => "function",

src/librustc_mir/borrow_check/diagnostics/region_name.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
191191
) -> Option<RegionName> {
192192
debug!("give_region_a_name(fr={:?}, counter={:?})", fr, renctx.counter);
193193

194-
assert!(self.nonlexical_regioncx.universal_regions.is_universal_region(fr));
194+
assert!(self.nonlexical_regioncx.universal_regions().is_universal_region(fr));
195195

196196
if let Some(value) = renctx.get(&fr) {
197197
return Some(value.clone());
@@ -277,7 +277,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
277277
.hir()
278278
.as_local_hir_id(self.mir_def_id)
279279
.expect("non-local mir");
280-
let def_ty = self.nonlexical_regioncx.universal_regions.defining_ty;
280+
let def_ty = self.nonlexical_regioncx.universal_regions().defining_ty;
281281

282282
if let DefiningTy::Closure(def_id, substs) = def_ty {
283283
let args_span = if let hir::ExprKind::Closure(_, _, _, span, _) =
@@ -345,11 +345,11 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
345345
renctx: &mut RegionErrorNamingCtx,
346346
) -> Option<RegionName> {
347347
let implicit_inputs =
348-
self.nonlexical_regioncx.universal_regions.defining_ty.implicit_inputs();
348+
self.nonlexical_regioncx.universal_regions().defining_ty.implicit_inputs();
349349
let argument_index =
350350
self.nonlexical_regioncx.get_argument_index_for_region(self.infcx.tcx, fr)?;
351351

352-
let arg_ty = self.nonlexical_regioncx.universal_regions.unnormalized_input_tys
352+
let arg_ty = self.nonlexical_regioncx.universal_regions().unnormalized_input_tys
353353
[implicit_inputs + argument_index];
354354
if let Some(region_name) =
355355
self.give_name_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index, renctx)
@@ -684,7 +684,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
684684
) -> Option<RegionName> {
685685
let tcx = self.infcx.tcx;
686686

687-
let return_ty = self.nonlexical_regioncx.universal_regions.unnormalized_output_ty;
687+
let return_ty = self.nonlexical_regioncx.universal_regions().unnormalized_output_ty;
688688
debug!("give_name_if_anonymous_region_appears_in_output: return_ty = {:?}", return_ty);
689689
if !tcx.any_free_region_meets(&return_ty, |r| r.to_region_vid() == fr) {
690690
return None;
@@ -734,7 +734,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
734734
) -> Option<RegionName> {
735735
// Note: generators from `async fn` yield `()`, so we don't have to
736736
// worry about them here.
737-
let yield_ty = self.nonlexical_regioncx.universal_regions.yield_ty?;
737+
let yield_ty = self.nonlexical_regioncx.universal_regions().yield_ty?;
738738
debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty,);
739739

740740
let tcx = self.infcx.tcx;

src/librustc_mir/borrow_check/diagnostics/var_name.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
1616
fr: RegionVid,
1717
) -> Option<(Option<Symbol>, Span)> {
1818
debug!("get_var_name_and_span_for_region(fr={:?})", fr);
19-
assert!(self.universal_regions.is_universal_region(fr));
19+
assert!(self.universal_regions().is_universal_region(fr));
2020

2121
debug!("get_var_name_and_span_for_region: attempting upvar");
2222
self.get_upvar_index_for_region(tcx, fr)
@@ -35,7 +35,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
3535
/// Search the upvars (if any) to find one that references fr. Return its index.
3636
crate fn get_upvar_index_for_region(&self, tcx: TyCtxt<'tcx>, fr: RegionVid) -> Option<usize> {
3737
let upvar_index =
38-
self.universal_regions.defining_ty.upvar_tys(tcx).position(|upvar_ty| {
38+
self.universal_regions().defining_ty.upvar_tys(tcx).position(|upvar_ty| {
3939
debug!("get_upvar_index_for_region: upvar_ty={:?}", upvar_ty);
4040
tcx.any_free_region_meets(&upvar_ty, |r| {
4141
let r = r.to_region_vid();
@@ -44,7 +44,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
4444
})
4545
})?;
4646

47-
let upvar_ty = self.universal_regions.defining_ty.upvar_tys(tcx).nth(upvar_index);
47+
let upvar_ty = self.universal_regions().defining_ty.upvar_tys(tcx).nth(upvar_index);
4848

4949
debug!(
5050
"get_upvar_index_for_region: found {:?} in upvar {} which has type {:?}",
@@ -85,9 +85,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
8585
tcx: TyCtxt<'tcx>,
8686
fr: RegionVid,
8787
) -> Option<usize> {
88-
let implicit_inputs = self.universal_regions.defining_ty.implicit_inputs();
88+
let implicit_inputs = self.universal_regions().defining_ty.implicit_inputs();
8989
let argument_index =
90-
self.universal_regions.unnormalized_input_tys.iter().skip(implicit_inputs).position(
90+
self.universal_regions().unnormalized_input_tys.iter().skip(implicit_inputs).position(
9191
|arg_ty| {
9292
debug!("get_argument_index_for_region: arg_ty = {:?}", arg_ty);
9393
tcx.any_free_region_meets(arg_ty, |r| r.to_region_vid() == fr)
@@ -96,7 +96,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
9696

9797
debug!(
9898
"get_argument_index_for_region: found {:?} in argument {} which has type {:?}",
99-
fr, argument_index, self.universal_regions.unnormalized_input_tys[argument_index],
99+
fr,
100+
argument_index,
101+
self.universal_regions().unnormalized_input_tys[argument_index],
100102
);
101103

102104
Some(argument_index)
@@ -110,7 +112,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
110112
local_names: &IndexVec<Local, Option<Symbol>>,
111113
argument_index: usize,
112114
) -> (Option<Symbol>, Span) {
113-
let implicit_inputs = self.universal_regions.defining_ty.implicit_inputs();
115+
let implicit_inputs = self.universal_regions().defining_ty.implicit_inputs();
114116
let argument_local = Local::new(implicit_inputs + argument_index + 1);
115117
debug!("get_argument_name_and_span_for_region: argument_local={:?}", argument_local);
116118

src/librustc_mir/borrow_check/region_infer/mod.rs

+47-16
Original file line numberDiff line numberDiff line change
@@ -44,49 +44,48 @@ pub struct RegionInferenceContext<'tcx> {
4444
/// variables are identified by their index (`RegionVid`). The
4545
/// definition contains information about where the region came
4646
/// from as well as its final inferred value.
47-
pub(in crate::borrow_check) definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>,
47+
definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>,
4848

4949
/// The liveness constraints added to each region. For most
5050
/// regions, these start out empty and steadily grow, though for
5151
/// each universally quantified region R they start out containing
5252
/// the entire CFG and `end(R)`.
53-
pub(in crate::borrow_check) liveness_constraints: LivenessValues<RegionVid>,
53+
liveness_constraints: LivenessValues<RegionVid>,
5454

5555
/// The outlives constraints computed by the type-check.
56-
pub(in crate::borrow_check) constraints: Rc<OutlivesConstraintSet>,
56+
constraints: Rc<OutlivesConstraintSet>,
5757

5858
/// The constraint-set, but in graph form, making it easy to traverse
5959
/// the constraints adjacent to a particular region. Used to construct
6060
/// the SCC (see `constraint_sccs`) and for error reporting.
61-
pub(in crate::borrow_check) constraint_graph: Rc<NormalConstraintGraph>,
61+
constraint_graph: Rc<NormalConstraintGraph>,
6262

6363
/// The SCC computed from `constraints` and the constraint
6464
/// graph. We have an edge from SCC A to SCC B if `A: B`. Used to
6565
/// compute the values of each region.
66-
pub(in crate::borrow_check) constraint_sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>,
66+
constraint_sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>,
6767

6868
/// Reverse of the SCC constraint graph -- i.e., an edge `A -> B`
6969
/// exists if `B: A`. Computed lazilly.
70-
pub(in crate::borrow_check) rev_constraint_graph: Option<Rc<VecGraph<ConstraintSccIndex>>>,
70+
rev_constraint_graph: Option<Rc<VecGraph<ConstraintSccIndex>>>,
7171

7272
/// The "R0 member of [R1..Rn]" constraints, indexed by SCC.
73-
pub(in crate::borrow_check) member_constraints:
74-
Rc<MemberConstraintSet<'tcx, ConstraintSccIndex>>,
73+
member_constraints: Rc<MemberConstraintSet<'tcx, ConstraintSccIndex>>,
7574

7675
/// Records the member constraints that we applied to each scc.
7776
/// This is useful for error reporting. Once constraint
7877
/// propagation is done, this vector is sorted according to
7978
/// `member_region_scc`.
80-
pub(in crate::borrow_check) member_constraints_applied: Vec<AppliedMemberConstraint>,
79+
member_constraints_applied: Vec<AppliedMemberConstraint>,
8180

8281
/// Map closure bounds to a `Span` that should be used for error reporting.
83-
pub(in crate::borrow_check) closure_bounds_mapping:
82+
closure_bounds_mapping:
8483
FxHashMap<Location, FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>>,
8584

8685
/// Contains the minimum universe of any variable within the same
8786
/// SCC. We will ensure that no SCC contains values that are not
8887
/// visible from this index.
89-
pub(in crate::borrow_check) scc_universes: IndexVec<ConstraintSccIndex, ty::UniverseIndex>,
88+
scc_universes: IndexVec<ConstraintSccIndex, ty::UniverseIndex>,
9089

9190
/// Contains a "representative" from each SCC. This will be the
9291
/// minimal RegionVid belonging to that universe. It is used as a
@@ -95,23 +94,23 @@ pub struct RegionInferenceContext<'tcx> {
9594
/// of its SCC and be sure that -- if they have the same repr --
9695
/// they *must* be equal (though not having the same repr does not
9796
/// mean they are unequal).
98-
pub(in crate::borrow_check) scc_representatives: IndexVec<ConstraintSccIndex, ty::RegionVid>,
97+
scc_representatives: IndexVec<ConstraintSccIndex, ty::RegionVid>,
9998

10099
/// The final inferred values of the region variables; we compute
101100
/// one value per SCC. To get the value for any given *region*,
102101
/// you first find which scc it is a part of.
103-
pub(in crate::borrow_check) scc_values: RegionValues<ConstraintSccIndex>,
102+
scc_values: RegionValues<ConstraintSccIndex>,
104103

105104
/// Type constraints that we check after solving.
106-
pub(in crate::borrow_check) type_tests: Vec<TypeTest<'tcx>>,
105+
type_tests: Vec<TypeTest<'tcx>>,
107106

108107
/// Information about the universally quantified regions in scope
109108
/// on this function.
110-
pub(in crate::borrow_check) universal_regions: Rc<UniversalRegions<'tcx>>,
109+
universal_regions: Rc<UniversalRegions<'tcx>>,
111110

112111
/// Information about how the universally quantified regions in
113112
/// scope on this function relate to one another.
114-
pub(in crate::borrow_check) universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
113+
universal_region_relations: Rc<UniversalRegionRelations<'tcx>>,
115114
}
116115

117116
/// Each time that `apply_member_constraint` is successful, it appends
@@ -1840,6 +1839,38 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18401839
.unwrap()
18411840
}
18421841

1842+
/// Get the region outlived by `longer_fr` and live at `element`.
1843+
crate fn region_from_element(&self, longer_fr: RegionVid, element: RegionElement) -> RegionVid {
1844+
match element {
1845+
RegionElement::Location(l) => self.find_sub_region_live_at(longer_fr, l),
1846+
RegionElement::RootUniversalRegion(r) => r,
1847+
RegionElement::PlaceholderRegion(error_placeholder) => self
1848+
.definitions
1849+
.iter_enumerated()
1850+
.filter_map(|(r, definition)| match definition.origin {
1851+
NLLRegionVariableOrigin::Placeholder(p) if p == error_placeholder => Some(r),
1852+
_ => None,
1853+
})
1854+
.next()
1855+
.unwrap(),
1856+
}
1857+
}
1858+
1859+
/// Get the region definition of `r`.
1860+
crate fn region_definition(&self, r: RegionVid) -> &RegionDefinition<'tcx> {
1861+
&self.definitions[r]
1862+
}
1863+
1864+
/// Check if the SCC of `r` contains `upper`.
1865+
crate fn upper_bound_in_region_scc(&self, r: RegionVid, upper: RegionVid) -> bool {
1866+
let r_scc = self.constraint_sccs.scc(r);
1867+
self.scc_values.contains(r_scc, upper)
1868+
}
1869+
1870+
crate fn universal_regions(&self) -> Rc<UniversalRegions<'tcx>> {
1871+
self.universal_regions.clone()
1872+
}
1873+
18431874
/// Tries to find the best constraint to blame for the fact that
18441875
/// `R: from_region`, where `R` is some region that meets
18451876
/// `target_test`. This works by following the constraint graph,

0 commit comments

Comments
 (0)