Skip to content

Commit ef55dc8

Browse files
committed
Clean up UniversalRegions.
There is an `Rc<UniversalRegions>` within `UniversalRegionRelations`, and yet the two types get passed around in tandem a lot. This commit makes `UniversalRegionRelations` own `UniversalRegions`, removing the `Rc` (which wasn't truly needed) and the tandem-passing. This requires adding a `universal_relations` method to `UniversalRegionRelations`, and renaming a couple of existing methods producing iterators to avoid a name clash.
1 parent c6eeeb0 commit ef55dc8

File tree

10 files changed

+46
-60
lines changed

10 files changed

+46
-60
lines changed

compiler/rustc_borrowck/src/nll.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ pub(crate) fn compute_regions<'a, 'tcx>(
9696
let mut all_facts =
9797
(polonius_input || AllFacts::enabled(infcx.tcx)).then_some(AllFacts::default());
9898

99-
let universal_regions = Rc::new(universal_regions);
100-
10199
let elements = Rc::new(DenseLocationMap::new(body));
102100

103101
// Run the MIR type-checker.
@@ -107,7 +105,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
107105
param_env,
108106
body,
109107
promoted,
110-
Rc::clone(&universal_regions),
108+
universal_regions,
111109
location_table,
112110
borrow_set,
113111
&mut all_facts,
@@ -140,11 +138,10 @@ pub(crate) fn compute_regions<'a, 'tcx>(
140138
body,
141139
borrow_set,
142140
move_data,
143-
&universal_regions,
144141
&universal_region_relations,
145142
);
146143

147-
if let Some(guar) = universal_regions.tainted_by_errors() {
144+
if let Some(guar) = universal_region_relations.universal_regions.tainted_by_errors() {
148145
// Suppress unhelpful extra errors in `infer_opaque_types` by clearing out all
149146
// outlives bounds that we may end up checking.
150147
outlives_constraints = Default::default();
@@ -157,7 +154,6 @@ pub(crate) fn compute_regions<'a, 'tcx>(
157154
let mut regioncx = RegionInferenceContext::new(
158155
infcx,
159156
var_origins,
160-
universal_regions,
161157
placeholder_indices,
162158
universal_region_relations,
163159
outlives_constraints,

compiler/rustc_borrowck/src/polonius/mod.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::borrow_set::BorrowSet;
1212
use crate::facts::{AllFacts, PoloniusRegionVid};
1313
use crate::location::LocationTable;
1414
use crate::type_check::free_region_relations::UniversalRegionRelations;
15-
use crate::universal_regions::UniversalRegions;
1615

1716
mod loan_invalidations;
1817
mod loan_kills;
@@ -32,7 +31,6 @@ pub(crate) fn emit_facts<'tcx>(
3231
body: &Body<'tcx>,
3332
borrow_set: &BorrowSet<'tcx>,
3433
move_data: &MoveData<'_>,
35-
universal_regions: &UniversalRegions<'_>,
3634
universal_region_relations: &UniversalRegionRelations<'_>,
3735
) {
3836
let Some(all_facts) = all_facts else {
@@ -41,12 +39,7 @@ pub(crate) fn emit_facts<'tcx>(
4139
};
4240
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
4341
emit_move_facts(all_facts, move_data, location_table, body);
44-
emit_universal_region_facts(
45-
all_facts,
46-
borrow_set,
47-
universal_regions,
48-
universal_region_relations,
49-
);
42+
emit_universal_region_facts(all_facts, borrow_set, universal_region_relations);
5043
emit_cfg_and_loan_kills_facts(all_facts, tcx, location_table, body, borrow_set);
5144
emit_loan_invalidations_facts(all_facts, tcx, location_table, body, borrow_set);
5245
}
@@ -129,7 +122,6 @@ fn emit_move_facts(
129122
fn emit_universal_region_facts(
130123
all_facts: &mut AllFacts,
131124
borrow_set: &BorrowSet<'_>,
132-
universal_regions: &UniversalRegions<'_>,
133125
universal_region_relations: &UniversalRegionRelations<'_>,
134126
) {
135127
// 1: universal regions are modeled in Polonius as a pair:
@@ -138,17 +130,18 @@ fn emit_universal_region_facts(
138130
// the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
139131
// added to the existing number of loans, as if they succeeded them in the set.
140132
//
133+
let universal_regions = &universal_region_relations.universal_regions;
141134
all_facts
142135
.universal_region
143-
.extend(universal_regions.universal_regions().map(PoloniusRegionVid::from));
136+
.extend(universal_regions.universal_regions_iter().map(PoloniusRegionVid::from));
144137
let borrow_count = borrow_set.len();
145138
debug!(
146139
"emit_universal_region_facts: polonius placeholders, num_universals={}, borrow_count={}",
147140
universal_regions.len(),
148141
borrow_count
149142
);
150143

151-
for universal_region in universal_regions.universal_regions() {
144+
for universal_region in universal_regions.universal_regions_iter() {
152145
let universal_region_idx = universal_region.index();
153146
let placeholder_loan_idx = borrow_count + universal_region_idx;
154147
all_facts.placeholder.push((universal_region.into(), placeholder_loan_idx.into()));

compiler/rustc_borrowck/src/region_infer/dump_mir.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
2323

2424
for region in self.regions() {
2525
if let NllRegionVariableOrigin::FreeRegion = self.definitions[region].origin {
26-
let classification = self.universal_regions.region_classification(region).unwrap();
26+
let classification =
27+
self.universal_regions().region_classification(region).unwrap();
2728
let outlived_by = self.universal_region_relations.regions_outlived_by(region);
2829
writeln!(
2930
out,

compiler/rustc_borrowck/src/region_infer/mod.rs

+23-25
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,6 @@ pub struct RegionInferenceContext<'tcx> {
191191
/// Type constraints that we check after solving.
192192
type_tests: Vec<TypeTest<'tcx>>,
193193

194-
/// Information about the universally quantified regions in scope
195-
/// on this function.
196-
universal_regions: Rc<UniversalRegions<'tcx>>,
197-
198194
/// Information about how the universally quantified regions in
199195
/// scope on this function relate to one another.
200196
universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
@@ -399,7 +395,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
399395
pub(crate) fn new(
400396
infcx: &BorrowckInferCtxt<'tcx>,
401397
var_infos: VarInfos,
402-
universal_regions: Rc<UniversalRegions<'tcx>>,
403398
placeholder_indices: Rc<PlaceholderIndices>,
404399
universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
405400
mut outlives_constraints: OutlivesConstraintSet<'tcx>,
@@ -409,7 +404,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
409404
liveness_constraints: LivenessValues,
410405
elements: Rc<DenseLocationMap>,
411406
) -> Self {
412-
debug!("universal_regions: {:#?}", universal_regions);
407+
let universal_regions = &universal_region_relations.universal_regions;
408+
409+
debug!("universal_regions: {:#?}", universal_region_relations.universal_regions);
413410
debug!("outlives constraints: {:#?}", outlives_constraints);
414411
debug!("placeholder_indices: {:#?}", placeholder_indices);
415412
debug!("type tests: {:#?}", type_tests);
@@ -453,7 +450,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
453450
universe_causes,
454451
scc_values,
455452
type_tests,
456-
universal_regions,
457453
universal_region_relations,
458454
};
459455

@@ -518,7 +514,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
518514
fn init_free_and_bound_regions(&mut self) {
519515
// Update the names (if any)
520516
// This iterator has unstable order but we collect it all into an IndexVec
521-
for (external_name, variable) in self.universal_regions.named_universal_regions() {
517+
for (external_name, variable) in
518+
self.universal_region_relations.universal_regions.named_universal_regions_iter()
519+
{
522520
debug!(
523521
"init_free_and_bound_regions: region {:?} has external name {:?}",
524522
variable, external_name
@@ -562,7 +560,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
562560
///
563561
/// (Panics if `r` is not a registered universal region.)
564562
pub(crate) fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
565-
self.universal_regions.to_region_vid(r)
563+
self.universal_regions().to_region_vid(r)
566564
}
567565

568566
/// Returns an iterator over all the outlives constraints.
@@ -574,7 +572,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
574572

575573
/// Adds annotations for `#[rustc_regions]`; see `UniversalRegions::annotate`.
576574
pub(crate) fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut Diag<'_, ()>) {
577-
self.universal_regions.annotate(tcx, err)
575+
self.universal_regions().annotate(tcx, err)
578576
}
579577

580578
/// Returns `true` if the region `r` contains the point `p`.
@@ -686,7 +684,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
686684
if outlives_requirements.is_empty() {
687685
(None, errors_buffer)
688686
} else {
689-
let num_external_vids = self.universal_regions.num_global_and_external_regions();
687+
let num_external_vids = self.universal_regions().num_global_and_external_regions();
690688
(
691689
Some(ClosureRegionRequirements { num_external_vids, outlives_requirements }),
692690
errors_buffer,
@@ -989,7 +987,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
989987
// always be in the root universe.
990988
if let Some(p) = self.scc_values.placeholders_contained_in(r_scc).next() {
991989
debug!("encountered placeholder in higher universe: {:?}, requiring 'static", p);
992-
let static_r = self.universal_regions.fr_static;
990+
let static_r = self.universal_regions().fr_static;
993991
propagated_outlives_requirements.push(ClosureOutlivesRequirement {
994992
subject,
995993
outlived_free_region: static_r,
@@ -1032,8 +1030,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10321030
// avoid potential non-determinism we approximate this by requiring
10331031
// T: '1 and T: '2.
10341032
for upper_bound in non_local_ub {
1035-
debug_assert!(self.universal_regions.is_universal_region(upper_bound));
1036-
debug_assert!(!self.universal_regions.is_local_free_region(upper_bound));
1033+
debug_assert!(self.universal_regions().is_universal_region(upper_bound));
1034+
debug_assert!(!self.universal_regions().is_local_free_region(upper_bound));
10371035

10381036
let requirement = ClosureOutlivesRequirement {
10391037
subject,
@@ -1101,7 +1099,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
11011099
// To do so, we simply check every candidate `u_r` for equality.
11021100
self.scc_values
11031101
.universal_regions_outlived_by(r_scc)
1104-
.filter(|&u_r| !self.universal_regions.is_local_free_region(u_r))
1102+
.filter(|&u_r| !self.universal_regions().is_local_free_region(u_r))
11051103
.find(|&u_r| self.eval_equal(u_r, r_vid))
11061104
.map(|u_r| ty::Region::new_var(tcx, u_r))
11071105
// In case we could not find a named region to map to,
@@ -1139,9 +1137,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
11391137

11401138
// Find the smallest universal region that contains all other
11411139
// universal regions within `region`.
1142-
let mut lub = self.universal_regions.fr_fn_body;
1140+
let mut lub = self.universal_regions().fr_fn_body;
11431141
let r_scc = self.constraint_sccs.scc(r);
1144-
let static_r = self.universal_regions.fr_static;
1142+
let static_r = self.universal_regions().fr_static;
11451143
for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
11461144
let new_lub = self.universal_region_relations.postdom_upper_bound(lub, ur);
11471145
debug!(?ur, ?lub, ?new_lub);
@@ -1288,12 +1286,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
12881286
debug!(
12891287
"sup_region's value = {:?} universal={:?}",
12901288
self.region_value_str(sup_region),
1291-
self.universal_regions.is_universal_region(sup_region),
1289+
self.universal_regions().is_universal_region(sup_region),
12921290
);
12931291
debug!(
12941292
"sub_region's value = {:?} universal={:?}",
12951293
self.region_value_str(sub_region),
1296-
self.universal_regions.is_universal_region(sub_region),
1294+
self.universal_regions().is_universal_region(sub_region),
12971295
);
12981296

12991297
let sub_region_scc = self.constraint_sccs.scc(sub_region);
@@ -1308,7 +1306,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
13081306
by super `{sup_region_scc:?}`, promoting to static",
13091307
);
13101308

1311-
return self.eval_outlives(sup_region, self.universal_regions.fr_static);
1309+
return self.eval_outlives(sup_region, self.universal_regions().fr_static);
13121310
}
13131311

13141312
// Both the `sub_region` and `sup_region` consist of the union
@@ -1332,7 +1330,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
13321330
// Now we have to compare all the points in the sub region and make
13331331
// sure they exist in the sup region.
13341332

1335-
if self.universal_regions.is_universal_region(sup_region) {
1333+
if self.universal_regions().is_universal_region(sup_region) {
13361334
// Micro-opt: universal regions contain all points.
13371335
debug!("super is universal and hence contains all points");
13381336
return true;
@@ -1736,7 +1734,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17361734
debug!("provides_universal_region(r={:?}, fr1={:?}, fr2={:?})", r, fr1, fr2);
17371735
let result = {
17381736
r == fr2 || {
1739-
fr2 == self.universal_regions.fr_static && self.cannot_name_placeholder(fr1, r)
1737+
fr2 == self.universal_regions().fr_static && self.cannot_name_placeholder(fr1, r)
17401738
}
17411739
};
17421740
debug!("provides_universal_region: result = {:?}", result);
@@ -1837,7 +1835,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18371835

18381836
// A constraint like `'r: 'x` can come from our constraint
18391837
// graph.
1840-
let fr_static = self.universal_regions.fr_static;
1838+
let fr_static = self.universal_regions().fr_static;
18411839
let outgoing_edges_from_graph =
18421840
self.constraint_graph.outgoing_edges(r, &self.constraints, fr_static);
18431841

@@ -1952,7 +1950,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
19521950
}
19531951

19541952
pub(crate) fn universal_regions(&self) -> &UniversalRegions<'tcx> {
1955-
self.universal_regions.as_ref()
1953+
&self.universal_region_relations.universal_regions
19561954
}
19571955

19581956
/// Tries to find the best constraint to blame for the fact that
@@ -2212,7 +2210,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22122210

22132211
/// Access to the region graph, built from the outlives constraints.
22142212
pub(crate) fn region_graph(&self) -> RegionGraph<'_, 'tcx, graph::Normal> {
2215-
self.constraint_graph.region_graph(&self.constraints, self.universal_regions.fr_static)
2213+
self.constraint_graph.region_graph(&self.constraints, self.universal_regions().fr_static)
22162214
}
22172215

22182216
/// Returns whether the given region is considered live at all points: whether it is a

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
7878
debug!(?opaque_type_key, ?concrete_type);
7979

8080
let mut arg_regions: Vec<(ty::RegionVid, ty::Region<'_>)> =
81-
vec![(self.universal_regions.fr_static, infcx.tcx.lifetimes.re_static)];
81+
vec![(self.universal_regions().fr_static, infcx.tcx.lifetimes.re_static)];
8282

8383
let opaque_type_key =
8484
opaque_type_key.fold_captured_lifetime_args(infcx.tcx, |region| {
@@ -92,12 +92,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
9292
// the same name and simplifies subsequent handling.
9393
// See [rustc-dev-guide chapter] § "Semantic lifetime equality".
9494
NllRegionVariableOrigin::FreeRegion => self
95-
.universal_regions
9695
.universal_regions()
96+
.universal_regions_iter()
9797
.filter(|&ur| {
9898
// See [rustc-dev-guide chapter] § "Closure restrictions".
9999
!matches!(
100-
self.universal_regions.region_classification(ur),
100+
self.universal_regions().region_classification(ur),
101101
Some(RegionClassification::External)
102102
)
103103
})

compiler/rustc_borrowck/src/region_infer/reverse_sccs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ impl RegionInferenceContext<'_> {
4545

4646
let graph = self.constraint_sccs.reverse();
4747
let mut paired_scc_regions = self
48-
.universal_regions
4948
.universal_regions()
49+
.universal_regions_iter()
5050
.map(|region| (self.constraint_sccs.scc(region), region))
5151
.collect::<Vec<_>>();
5252
paired_scc_regions.sort();

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::rc::Rc;
2-
31
use rustc_data_structures::frozen::Frozen;
42
use rustc_data_structures::transitive_relation::{TransitiveRelation, TransitiveRelationBuilder};
53
use rustc_hir::def::DefKind;
@@ -23,7 +21,7 @@ use crate::universal_regions::UniversalRegions;
2321

2422
#[derive(Debug)]
2523
pub(crate) struct UniversalRegionRelations<'tcx> {
26-
universal_regions: Rc<UniversalRegions<'tcx>>,
24+
pub(crate) universal_regions: UniversalRegions<'tcx>,
2725

2826
/// Stores the outlives relations that are known to hold from the
2927
/// implied bounds, in-scope where-clauses, and that sort of
@@ -54,7 +52,7 @@ pub(crate) fn create<'tcx>(
5452
infcx: &InferCtxt<'tcx>,
5553
param_env: ty::ParamEnv<'tcx>,
5654
implicit_region_bound: ty::Region<'tcx>,
57-
universal_regions: Rc<UniversalRegions<'tcx>>,
55+
universal_regions: UniversalRegions<'tcx>,
5856
constraints: &mut MirTypeckRegionConstraints<'tcx>,
5957
) -> CreateResult<'tcx> {
6058
UniversalRegionRelationsBuilder {
@@ -184,7 +182,7 @@ impl UniversalRegionRelations<'_> {
184182
struct UniversalRegionRelationsBuilder<'a, 'tcx> {
185183
infcx: &'a InferCtxt<'tcx>,
186184
param_env: ty::ParamEnv<'tcx>,
187-
universal_regions: Rc<UniversalRegions<'tcx>>,
185+
universal_regions: UniversalRegions<'tcx>,
188186
implicit_region_bound: ty::Region<'tcx>,
189187
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
190188

@@ -220,7 +218,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
220218
// region `'r`, all of which are provided by our caller
221219
let fr_static = self.universal_regions.fr_static;
222220
let fr_fn_body = self.universal_regions.fr_fn_body;
223-
for fr in self.universal_regions.universal_regions() {
221+
for fr in self.universal_regions.universal_regions_iter() {
224222
debug!("build: relating free region {:?} to itself and to 'static", fr);
225223
self.relate_universal_regions(fr, fr);
226224
self.relate_universal_regions(fr_static, fr);

compiler/rustc_borrowck/src/type_check/liveness/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(super) fn generate<'a, 'tcx>(
3939

4040
let free_regions = regions_that_outlive_free_regions(
4141
typeck.infcx.num_region_vars(),
42-
typeck.universal_regions,
42+
&typeck.universal_regions,
4343
&typeck.constraints.outlives_constraints,
4444
);
4545
let (relevant_live_locals, boring_locals) =
@@ -107,7 +107,7 @@ fn regions_that_outlive_free_regions<'tcx>(
107107
let rev_region_graph = rev_constraint_graph.region_graph(constraint_set, fr_static);
108108

109109
// Stack for the depth-first search. Start out with all the free regions.
110-
let mut stack: Vec<_> = universal_regions.universal_regions().collect();
110+
let mut stack: Vec<_> = universal_regions.universal_regions_iter().collect();
111111

112112
// Set of all free regions, plus anything that outlives them. Initially
113113
// just contains the free regions.

0 commit comments

Comments
 (0)