Skip to content

Commit

Permalink
Clean up UniversalRegions.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nnethercote committed Nov 19, 2024
1 parent ed11fbe commit 952c6d5
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 60 deletions.
8 changes: 2 additions & 6 deletions compiler/rustc_borrowck/src/nll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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,
Expand Down
15 changes: 4 additions & 11 deletions compiler/rustc_borrowck/src/polonius/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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:
Expand All @@ -138,17 +130,18 @@ 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={}",
universal_regions.len(),
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()));
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_borrowck/src/region_infer/dump_mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 23 additions & 25 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down Expand Up @@ -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>,
Expand All @@ -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);
Expand Down Expand Up @@ -453,7 +450,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
universe_causes,
scc_values,
type_tests,
universal_regions,
universal_region_relations,
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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`.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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)
)
})
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/reverse_sccs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_borrowck/src/type_check/free_region_relations.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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>,

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/type_check/liveness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 952c6d5

Please sign in to comment.