Skip to content

Commit 640195a

Browse files
committed
eagerly initialize definitions in sub-fn
1 parent 15e3f81 commit 640195a

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

compiler/rustc_borrowck/src/nll.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@ pub(crate) fn compute_regions<'a, 'tcx>(
117117
Rc::clone(&location_map),
118118
);
119119

120-
// Create the region inference context, taking ownership of the
121-
// region inference data that was contained in `infcx`, and the
122-
// base constraints generated by the type-check.
123-
let var_infos = infcx.get_region_var_infos();
124-
125120
// If requested, emit legacy polonius facts.
126121
polonius::legacy::emit_facts(
127122
&mut polonius_facts,
@@ -134,13 +129,8 @@ pub(crate) fn compute_regions<'a, 'tcx>(
134129
&constraints,
135130
);
136131

137-
let mut regioncx = RegionInferenceContext::new(
138-
infcx,
139-
var_infos,
140-
constraints,
141-
universal_region_relations,
142-
location_map,
143-
);
132+
let mut regioncx =
133+
RegionInferenceContext::new(infcx, constraints, universal_region_relations, location_map);
144134

145135
// If requested for `-Zpolonius=next`, convert NLL constraints to localized outlives constraints
146136
// and use them to compute loan liveness.

compiler/rustc_borrowck/src/region_infer/mod.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::Diag;
99
use rustc_hir::def_id::CRATE_DEF_ID;
1010
use rustc_index::IndexVec;
1111
use rustc_infer::infer::outlives::test_type_match;
12-
use rustc_infer::infer::region_constraints::{GenericKind, VarInfos, VerifyBound, VerifyIfEq};
12+
use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound, VerifyIfEq};
1313
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin};
1414
use rustc_middle::bug;
1515
use rustc_middle::mir::{
@@ -145,7 +145,7 @@ pub struct RegionInferenceContext<'tcx> {
145145
/// variables are identified by their index (`RegionVid`). The
146146
/// definition contains information about where the region came
147147
/// from as well as its final inferred value.
148-
pub(crate) definitions: IndexVec<RegionVid, RegionDefinition<'tcx>>,
148+
pub(crate) definitions: Frozen<IndexVec<RegionVid, RegionDefinition<'tcx>>>,
149149

150150
/// The liveness constraints added to each region. For most
151151
/// regions, these start out empty and steadily grow, though for
@@ -385,6 +385,26 @@ fn sccs_info<'tcx>(infcx: &BorrowckInferCtxt<'tcx>, sccs: &ConstraintSccs) {
385385
debug!("SCC edges {:#?}", scc_node_to_edges);
386386
}
387387

388+
fn create_definitions<'tcx>(
389+
infcx: &BorrowckInferCtxt<'tcx>,
390+
universal_regions: &UniversalRegions<'tcx>,
391+
) -> Frozen<IndexVec<RegionVid, RegionDefinition<'tcx>>> {
392+
// Create a RegionDefinition for each inference variable.
393+
let mut definitions: IndexVec<_, _> = infcx
394+
.get_region_var_infos()
395+
.iter()
396+
.map(|info| RegionDefinition::new(info.universe, info.origin))
397+
.collect();
398+
399+
// Add the external name for all universal regions.
400+
for (external_name, variable) in universal_regions.named_universal_regions_iter() {
401+
debug!("region {variable:?} has external name {external_name:?}");
402+
definitions[variable].external_name = Some(external_name);
403+
}
404+
405+
Frozen::freeze(definitions)
406+
}
407+
388408
impl<'tcx> RegionInferenceContext<'tcx> {
389409
/// Creates a new region inference context with a total of
390410
/// `num_region_variables` valid inference variables; the first N
@@ -395,7 +415,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
395415
/// of constraints produced by the MIR type check.
396416
pub(crate) fn new(
397417
infcx: &BorrowckInferCtxt<'tcx>,
398-
var_infos: VarInfos,
399418
constraints: MirTypeckRegionConstraints<'tcx>,
400419
universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
401420
location_map: Rc<DenseLocationMap>,
@@ -426,11 +445,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
426445
infcx.set_tainted_by_errors(guar);
427446
}
428447

429-
// Create a RegionDefinition for each inference variable.
430-
let definitions: IndexVec<_, _> = var_infos
431-
.iter()
432-
.map(|info| RegionDefinition::new(info.universe, info.origin))
433-
.collect();
448+
let definitions = create_definitions(infcx, &universal_regions);
434449

435450
let constraint_sccs =
436451
outlives_constraints.add_outlives_static(&universal_regions, &definitions);
@@ -526,18 +541,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
526541
/// means that the `R1: !1` constraint here will cause
527542
/// `R1` to become `'static`.
528543
fn init_free_and_bound_regions(&mut self) {
529-
// Update the names (if any)
530-
// This iterator has unstable order but we collect it all into an IndexVec
531-
for (external_name, variable) in
532-
self.universal_region_relations.universal_regions.named_universal_regions_iter()
533-
{
534-
debug!(
535-
"init_free_and_bound_regions: region {:?} has external name {:?}",
536-
variable, external_name
537-
);
538-
self.definitions[variable].external_name = Some(external_name);
539-
}
540-
541544
for variable in self.definitions.indices() {
542545
let scc = self.constraint_sccs.scc(variable);
543546

0 commit comments

Comments
 (0)