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.
This commit is contained in:
Nicholas Nethercote 2024-10-29 14:21:40 +11:00
parent ed11fbe5df
commit 952c6d5c96
10 changed files with 46 additions and 60 deletions

View File

@ -96,8 +96,6 @@ pub(crate) fn compute_regions<'a, 'tcx>(
let mut all_facts =
(polonius_input || AllFacts::enabled(infcx.tcx)).then_some(AllFacts::default());
let universal_regions = Rc::new(universal_regions);
let elements = Rc::new(DenseLocationMap::new(body));
// Run the MIR type-checker.
@ -107,7 +105,7 @@ pub(crate) fn compute_regions<'a, 'tcx>(
param_env,
body,
promoted,
Rc::clone(&universal_regions),
universal_regions,
location_table,
borrow_set,
&mut all_facts,
@ -140,11 +138,10 @@ pub(crate) fn compute_regions<'a, 'tcx>(
body,
borrow_set,
move_data,
&universal_regions,
&universal_region_relations,
);
if let Some(guar) = universal_regions.tainted_by_errors() {
if let Some(guar) = universal_region_relations.universal_regions.tainted_by_errors() {
// Suppress unhelpful extra errors in `infer_opaque_types` by clearing out all
// outlives bounds that we may end up checking.
outlives_constraints = Default::default();
@ -157,7 +154,6 @@ pub(crate) fn compute_regions<'a, 'tcx>(
let mut regioncx = RegionInferenceContext::new(
infcx,
var_origins,
universal_regions,
placeholder_indices,
universal_region_relations,
outlives_constraints,

View File

@ -12,7 +12,6 @@ use crate::borrow_set::BorrowSet;
use crate::facts::{AllFacts, PoloniusRegionVid};
use crate::location::LocationTable;
use crate::type_check::free_region_relations::UniversalRegionRelations;
use crate::universal_regions::UniversalRegions;
mod loan_invalidations;
mod loan_kills;
@ -32,7 +31,6 @@ pub(crate) fn emit_facts<'tcx>(
body: &Body<'tcx>,
borrow_set: &BorrowSet<'tcx>,
move_data: &MoveData<'_>,
universal_regions: &UniversalRegions<'_>,
universal_region_relations: &UniversalRegionRelations<'_>,
) {
let Some(all_facts) = all_facts else {
@ -41,12 +39,7 @@ pub(crate) fn emit_facts<'tcx>(
};
let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
emit_move_facts(all_facts, move_data, location_table, body);
emit_universal_region_facts(
all_facts,
borrow_set,
universal_regions,
universal_region_relations,
);
emit_universal_region_facts(all_facts, borrow_set, universal_region_relations);
emit_cfg_and_loan_kills_facts(all_facts, tcx, location_table, body, borrow_set);
emit_loan_invalidations_facts(all_facts, tcx, location_table, body, borrow_set);
}
@ -129,7 +122,6 @@ fn emit_move_facts(
fn emit_universal_region_facts(
all_facts: &mut AllFacts,
borrow_set: &BorrowSet<'_>,
universal_regions: &UniversalRegions<'_>,
universal_region_relations: &UniversalRegionRelations<'_>,
) {
// 1: universal regions are modeled in Polonius as a pair:
@ -138,9 +130,10 @@ fn emit_universal_region_facts(
// the `borrow_set`, their `BorrowIndex` are synthesized as the universal region index
// added to the existing number of loans, as if they succeeded them in the set.
//
let universal_regions = &universal_region_relations.universal_regions;
all_facts
.universal_region
.extend(universal_regions.universal_regions().map(PoloniusRegionVid::from));
.extend(universal_regions.universal_regions_iter().map(PoloniusRegionVid::from));
let borrow_count = borrow_set.len();
debug!(
"emit_universal_region_facts: polonius placeholders, num_universals={}, borrow_count={}",
@ -148,7 +141,7 @@ fn emit_universal_region_facts(
borrow_count
);
for universal_region in universal_regions.universal_regions() {
for universal_region in universal_regions.universal_regions_iter() {
let universal_region_idx = universal_region.index();
let placeholder_loan_idx = borrow_count + universal_region_idx;
all_facts.placeholder.push((universal_region.into(), placeholder_loan_idx.into()));

View File

@ -23,7 +23,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
for region in self.regions() {
if let NllRegionVariableOrigin::FreeRegion = self.definitions[region].origin {
let classification = self.universal_regions.region_classification(region).unwrap();
let classification =
self.universal_regions().region_classification(region).unwrap();
let outlived_by = self.universal_region_relations.regions_outlived_by(region);
writeln!(
out,

View File

@ -191,10 +191,6 @@ pub struct RegionInferenceContext<'tcx> {
/// Type constraints that we check after solving.
type_tests: Vec<TypeTest<'tcx>>,
/// Information about the universally quantified regions in scope
/// on this function.
universal_regions: Rc<UniversalRegions<'tcx>>,
/// Information about how the universally quantified regions in
/// scope on this function relate to one another.
universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
@ -399,7 +395,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
pub(crate) fn new(
infcx: &BorrowckInferCtxt<'tcx>,
var_infos: VarInfos,
universal_regions: Rc<UniversalRegions<'tcx>>,
placeholder_indices: Rc<PlaceholderIndices>,
universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
mut outlives_constraints: OutlivesConstraintSet<'tcx>,
@ -409,7 +404,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
liveness_constraints: LivenessValues,
elements: Rc<DenseLocationMap>,
) -> Self {
debug!("universal_regions: {:#?}", universal_regions);
let universal_regions = &universal_region_relations.universal_regions;
debug!("universal_regions: {:#?}", universal_region_relations.universal_regions);
debug!("outlives constraints: {:#?}", outlives_constraints);
debug!("placeholder_indices: {:#?}", placeholder_indices);
debug!("type tests: {:#?}", type_tests);
@ -453,7 +450,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
universe_causes,
scc_values,
type_tests,
universal_regions,
universal_region_relations,
};
@ -518,7 +514,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
fn init_free_and_bound_regions(&mut self) {
// Update the names (if any)
// This iterator has unstable order but we collect it all into an IndexVec
for (external_name, variable) in self.universal_regions.named_universal_regions() {
for (external_name, variable) in
self.universal_region_relations.universal_regions.named_universal_regions_iter()
{
debug!(
"init_free_and_bound_regions: region {:?} has external name {:?}",
variable, external_name
@ -562,7 +560,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
///
/// (Panics if `r` is not a registered universal region.)
pub(crate) fn to_region_vid(&self, r: ty::Region<'tcx>) -> RegionVid {
self.universal_regions.to_region_vid(r)
self.universal_regions().to_region_vid(r)
}
/// Returns an iterator over all the outlives constraints.
@ -574,7 +572,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// Adds annotations for `#[rustc_regions]`; see `UniversalRegions::annotate`.
pub(crate) fn annotate(&self, tcx: TyCtxt<'tcx>, err: &mut Diag<'_, ()>) {
self.universal_regions.annotate(tcx, err)
self.universal_regions().annotate(tcx, err)
}
/// Returns `true` if the region `r` contains the point `p`.
@ -686,7 +684,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
if outlives_requirements.is_empty() {
(None, errors_buffer)
} else {
let num_external_vids = self.universal_regions.num_global_and_external_regions();
let num_external_vids = self.universal_regions().num_global_and_external_regions();
(
Some(ClosureRegionRequirements { num_external_vids, outlives_requirements }),
errors_buffer,
@ -989,7 +987,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// always be in the root universe.
if let Some(p) = self.scc_values.placeholders_contained_in(r_scc).next() {
debug!("encountered placeholder in higher universe: {:?}, requiring 'static", p);
let static_r = self.universal_regions.fr_static;
let static_r = self.universal_regions().fr_static;
propagated_outlives_requirements.push(ClosureOutlivesRequirement {
subject,
outlived_free_region: static_r,
@ -1032,8 +1030,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// avoid potential non-determinism we approximate this by requiring
// T: '1 and T: '2.
for upper_bound in non_local_ub {
debug_assert!(self.universal_regions.is_universal_region(upper_bound));
debug_assert!(!self.universal_regions.is_local_free_region(upper_bound));
debug_assert!(self.universal_regions().is_universal_region(upper_bound));
debug_assert!(!self.universal_regions().is_local_free_region(upper_bound));
let requirement = ClosureOutlivesRequirement {
subject,
@ -1101,7 +1099,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// To do so, we simply check every candidate `u_r` for equality.
self.scc_values
.universal_regions_outlived_by(r_scc)
.filter(|&u_r| !self.universal_regions.is_local_free_region(u_r))
.filter(|&u_r| !self.universal_regions().is_local_free_region(u_r))
.find(|&u_r| self.eval_equal(u_r, r_vid))
.map(|u_r| ty::Region::new_var(tcx, u_r))
// In case we could not find a named region to map to,
@ -1139,9 +1137,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// Find the smallest universal region that contains all other
// universal regions within `region`.
let mut lub = self.universal_regions.fr_fn_body;
let mut lub = self.universal_regions().fr_fn_body;
let r_scc = self.constraint_sccs.scc(r);
let static_r = self.universal_regions.fr_static;
let static_r = self.universal_regions().fr_static;
for ur in self.scc_values.universal_regions_outlived_by(r_scc) {
let new_lub = self.universal_region_relations.postdom_upper_bound(lub, ur);
debug!(?ur, ?lub, ?new_lub);
@ -1288,12 +1286,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
debug!(
"sup_region's value = {:?} universal={:?}",
self.region_value_str(sup_region),
self.universal_regions.is_universal_region(sup_region),
self.universal_regions().is_universal_region(sup_region),
);
debug!(
"sub_region's value = {:?} universal={:?}",
self.region_value_str(sub_region),
self.universal_regions.is_universal_region(sub_region),
self.universal_regions().is_universal_region(sub_region),
);
let sub_region_scc = self.constraint_sccs.scc(sub_region);
@ -1308,7 +1306,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
by super `{sup_region_scc:?}`, promoting to static",
);
return self.eval_outlives(sup_region, self.universal_regions.fr_static);
return self.eval_outlives(sup_region, self.universal_regions().fr_static);
}
// Both the `sub_region` and `sup_region` consist of the union
@ -1332,7 +1330,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// Now we have to compare all the points in the sub region and make
// sure they exist in the sup region.
if self.universal_regions.is_universal_region(sup_region) {
if self.universal_regions().is_universal_region(sup_region) {
// Micro-opt: universal regions contain all points.
debug!("super is universal and hence contains all points");
return true;
@ -1736,7 +1734,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
debug!("provides_universal_region(r={:?}, fr1={:?}, fr2={:?})", r, fr1, fr2);
let result = {
r == fr2 || {
fr2 == self.universal_regions.fr_static && self.cannot_name_placeholder(fr1, r)
fr2 == self.universal_regions().fr_static && self.cannot_name_placeholder(fr1, r)
}
};
debug!("provides_universal_region: result = {:?}", result);
@ -1837,7 +1835,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// A constraint like `'r: 'x` can come from our constraint
// graph.
let fr_static = self.universal_regions.fr_static;
let fr_static = self.universal_regions().fr_static;
let outgoing_edges_from_graph =
self.constraint_graph.outgoing_edges(r, &self.constraints, fr_static);
@ -1952,7 +1950,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
}
pub(crate) fn universal_regions(&self) -> &UniversalRegions<'tcx> {
self.universal_regions.as_ref()
&self.universal_region_relations.universal_regions
}
/// Tries to find the best constraint to blame for the fact that
@ -2212,7 +2210,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// Access to the region graph, built from the outlives constraints.
pub(crate) fn region_graph(&self) -> RegionGraph<'_, 'tcx, graph::Normal> {
self.constraint_graph.region_graph(&self.constraints, self.universal_regions.fr_static)
self.constraint_graph.region_graph(&self.constraints, self.universal_regions().fr_static)
}
/// Returns whether the given region is considered live at all points: whether it is a

View File

@ -74,7 +74,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
debug!(?opaque_type_key, ?concrete_type);
let mut arg_regions: Vec<(ty::RegionVid, ty::Region<'_>)> =
vec![(self.universal_regions.fr_static, infcx.tcx.lifetimes.re_static)];
vec![(self.universal_regions().fr_static, infcx.tcx.lifetimes.re_static)];
let opaque_type_key =
opaque_type_key.fold_captured_lifetime_args(infcx.tcx, |region| {
@ -88,12 +88,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// the same name and simplifies subsequent handling.
// See [rustc-dev-guide chapter] § "Semantic lifetime equality".
NllRegionVariableOrigin::FreeRegion => self
.universal_regions
.universal_regions()
.universal_regions_iter()
.filter(|&ur| {
// See [rustc-dev-guide chapter] § "Closure restrictions".
!matches!(
self.universal_regions.region_classification(ur),
self.universal_regions().region_classification(ur),
Some(RegionClassification::External)
)
})

View File

@ -45,8 +45,8 @@ impl RegionInferenceContext<'_> {
let graph = self.constraint_sccs.reverse();
let mut paired_scc_regions = self
.universal_regions
.universal_regions()
.universal_regions_iter()
.map(|region| (self.constraint_sccs.scc(region), region))
.collect::<Vec<_>>();
paired_scc_regions.sort();

View File

@ -1,5 +1,3 @@
use std::rc::Rc;
use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::transitive_relation::{TransitiveRelation, TransitiveRelationBuilder};
use rustc_hir::def::DefKind;
@ -23,7 +21,7 @@ use crate::universal_regions::UniversalRegions;
#[derive(Debug)]
pub(crate) struct UniversalRegionRelations<'tcx> {
universal_regions: Rc<UniversalRegions<'tcx>>,
pub(crate) universal_regions: UniversalRegions<'tcx>,
/// Stores the outlives relations that are known to hold from the
/// implied bounds, in-scope where-clauses, and that sort of
@ -54,7 +52,7 @@ pub(crate) fn create<'tcx>(
infcx: &InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
implicit_region_bound: ty::Region<'tcx>,
universal_regions: Rc<UniversalRegions<'tcx>>,
universal_regions: UniversalRegions<'tcx>,
constraints: &mut MirTypeckRegionConstraints<'tcx>,
) -> CreateResult<'tcx> {
UniversalRegionRelationsBuilder {
@ -184,7 +182,7 @@ impl UniversalRegionRelations<'_> {
struct UniversalRegionRelationsBuilder<'a, 'tcx> {
infcx: &'a InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
universal_regions: Rc<UniversalRegions<'tcx>>,
universal_regions: UniversalRegions<'tcx>,
implicit_region_bound: ty::Region<'tcx>,
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
@ -220,7 +218,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
// region `'r`, all of which are provided by our caller
let fr_static = self.universal_regions.fr_static;
let fr_fn_body = self.universal_regions.fr_fn_body;
for fr in self.universal_regions.universal_regions() {
for fr in self.universal_regions.universal_regions_iter() {
debug!("build: relating free region {:?} to itself and to 'static", fr);
self.relate_universal_regions(fr, fr);
self.relate_universal_regions(fr_static, fr);

View File

@ -39,7 +39,7 @@ pub(super) fn generate<'a, 'tcx>(
let free_regions = regions_that_outlive_free_regions(
typeck.infcx.num_region_vars(),
typeck.universal_regions,
&typeck.universal_regions,
&typeck.constraints.outlives_constraints,
);
let (relevant_live_locals, boring_locals) =
@ -107,7 +107,7 @@ fn regions_that_outlive_free_regions<'tcx>(
let rev_region_graph = rev_constraint_graph.region_graph(constraint_set, fr_static);
// Stack for the depth-first search. Start out with all the free regions.
let mut stack: Vec<_> = universal_regions.universal_regions().collect();
let mut stack: Vec<_> = universal_regions.universal_regions_iter().collect();
// Set of all free regions, plus anything that outlives them. Initially
// just contains the free regions.

View File

@ -121,7 +121,7 @@ pub(crate) fn type_check<'a, 'tcx>(
param_env: ty::ParamEnv<'tcx>,
body: &Body<'tcx>,
promoted: &IndexSlice<Promoted, Body<'tcx>>,
universal_regions: Rc<UniversalRegions<'tcx>>,
universal_regions: UniversalRegions<'tcx>,
location_table: &LocationTable,
borrow_set: &BorrowSet<'tcx>,
all_facts: &mut Option<AllFacts>,
@ -150,7 +150,7 @@ pub(crate) fn type_check<'a, 'tcx>(
infcx,
param_env,
implicit_region_bound,
Rc::clone(&universal_regions),
universal_regions,
&mut constraints,
);
@ -166,7 +166,7 @@ pub(crate) fn type_check<'a, 'tcx>(
known_type_outlives_obligations,
implicit_region_bound,
reported_errors: Default::default(),
universal_regions: &universal_regions,
universal_regions: &universal_region_relations.universal_regions,
location_table,
all_facts,
borrow_set,

View File

@ -312,7 +312,7 @@ impl<'tcx> UniversalRegions<'tcx> {
/// Returns an iterator over all the RegionVids corresponding to
/// universally quantified free regions.
pub(crate) fn universal_regions(&self) -> impl Iterator<Item = RegionVid> {
pub(crate) fn universal_regions_iter(&self) -> impl Iterator<Item = RegionVid> {
(FIRST_GLOBAL_INDEX..self.num_universals).map(RegionVid::from_usize)
}
@ -336,7 +336,7 @@ impl<'tcx> UniversalRegions<'tcx> {
}
/// Gets an iterator over all the early-bound regions that have names.
pub(crate) fn named_universal_regions<'s>(
pub(crate) fn named_universal_regions_iter<'s>(
&'s self,
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {
self.indices.indices.iter().map(|(&r, &v)| (r, v))