Skip to content

Commit 3212751

Browse files
committed
Move the placeholder-to-region vid mapping out of MirTypckRegionConstraints
1 parent 4958ec8 commit 3212751

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

compiler/rustc_borrowck/src/handle_placeholders.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ pub(crate) fn compute_sccs_applying_placeholder_outlives_constraints<'tcx>(
344344
mut member_constraints,
345345
universe_causes,
346346
type_tests,
347-
..
348347
} = constraints;
349348

350349
if let Some(guar) = universal_regions.tainted_by_errors() {

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tracing::{debug, instrument};
1515

1616
use crate::constraints::OutlivesConstraint;
1717
use crate::region_infer::TypeTest;
18-
use crate::type_check::{Locations, MirTypeckRegionConstraints};
18+
use crate::type_check::{Locations, MirTypeckRegionConstraints, PlaceholderToRegion};
1919
use crate::universal_regions::UniversalRegions;
2020
use crate::{ClosureOutlivesSubject, ClosureRegionRequirements, ConstraintCategory};
2121

@@ -40,6 +40,7 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> {
4040
category: ConstraintCategory<'tcx>,
4141
from_closure: bool,
4242
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
43+
placeholder_to_region: &'a mut PlaceholderToRegion<'tcx>,
4344
}
4445

4546
impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
@@ -53,6 +54,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
5354
span: Span,
5455
category: ConstraintCategory<'tcx>,
5556
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
57+
placeholder_to_region: &'a mut PlaceholderToRegion<'tcx>,
5658
) -> Self {
5759
Self {
5860
infcx,
@@ -65,6 +67,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
6567
category,
6668
constraints,
6769
from_closure: false,
70+
placeholder_to_region,
6871
}
6972
}
7073

@@ -206,7 +209,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
206209
if value.has_placeholders() {
207210
fold_regions(self.infcx.tcx, value, |r, _| match r.kind() {
208211
ty::RePlaceholder(placeholder) => {
209-
self.constraints.placeholder_region(self.infcx, placeholder)
212+
self.placeholder_to_region.placeholder_region(self.infcx, placeholder)
210213
}
211214
_ => r,
212215
})
@@ -227,7 +230,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
227230

228231
fn to_region_vid(&mut self, r: ty::Region<'tcx>) -> ty::RegionVid {
229232
if let ty::RePlaceholder(placeholder) = r.kind() {
230-
self.constraints.placeholder_region(self.infcx, placeholder).as_var()
233+
self.placeholder_to_region.placeholder_region(self.infcx, placeholder).as_var()
231234
} else {
232235
self.universal_regions.to_region_vid(r)
233236
}

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use rustc_trait_selection::traits::query::type_op::{self, TypeOp};
1414
use tracing::{debug, instrument};
1515
use type_op::TypeOpOutput;
1616

17-
use crate::type_check::{Locations, MirTypeckRegionConstraints, constraint_conversion};
17+
use crate::type_check::{
18+
Locations, MirTypeckRegionConstraints, PlaceholderToRegion, constraint_conversion,
19+
};
1820
use crate::universal_regions::UniversalRegions;
1921

2022
#[derive(Debug)]
@@ -51,6 +53,7 @@ pub(crate) fn create<'tcx>(
5153
param_env: ty::ParamEnv<'tcx>,
5254
universal_regions: UniversalRegions<'tcx>,
5355
constraints: &mut MirTypeckRegionConstraints<'tcx>,
56+
placeholder_to_region: &mut PlaceholderToRegion<'tcx>,
5457
) -> CreateResult<'tcx> {
5558
UniversalRegionRelationsBuilder {
5659
infcx,
@@ -60,6 +63,7 @@ pub(crate) fn create<'tcx>(
6063
region_bound_pairs: Default::default(),
6164
outlives: Default::default(),
6265
inverse_outlives: Default::default(),
66+
placeholder_to_region,
6367
}
6468
.create()
6569
}
@@ -181,6 +185,7 @@ struct UniversalRegionRelationsBuilder<'a, 'tcx> {
181185
param_env: ty::ParamEnv<'tcx>,
182186
universal_regions: UniversalRegions<'tcx>,
183187
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
188+
placeholder_to_region: &'a mut PlaceholderToRegion<'tcx>,
184189

185190
// outputs:
186191
outlives: TransitiveRelationBuilder<RegionVid>,
@@ -324,6 +329,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
324329
span,
325330
ConstraintCategory::Internal,
326331
self.constraints,
332+
self.placeholder_to_region,
327333
)
328334
.convert_all(c);
329335
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,31 @@ pub(crate) fn type_check<'tcx>(
110110
location_map: Rc<DenseLocationMap>,
111111
) -> MirTypeckResults<'tcx> {
112112
let mut constraints = MirTypeckRegionConstraints {
113-
placeholder_to_region: FxHashMap::default(),
114113
liveness_constraints: LivenessValues::with_specific_points(Rc::clone(&location_map)),
115114
outlives_constraints: OutlivesConstraintSet::default(),
116115
member_constraints: MemberConstraintSet::default(),
117116
type_tests: Vec::default(),
118117
universe_causes: FxIndexMap::default(),
119118
};
120119

120+
// FIXME: I strongly suspect this follows the case of being mutated for a while
121+
// and then settling, but I don't know enough about the type inference parts to know
122+
// when this happens and if this can be exploited to simplify some of the downstream
123+
// code. -- @amandasystems.
124+
let mut placeholder_to_region = Default::default();
125+
121126
let CreateResult {
122127
universal_region_relations,
123128
region_bound_pairs,
124129
normalized_inputs_and_output,
125130
known_type_outlives_obligations,
126-
} = free_region_relations::create(infcx, infcx.param_env, universal_regions, &mut constraints);
131+
} = free_region_relations::create(
132+
infcx,
133+
infcx.param_env,
134+
universal_regions,
135+
&mut constraints,
136+
&mut placeholder_to_region,
137+
);
127138

128139
let pre_obligations = infcx.take_registered_region_obligations();
129140
assert!(
@@ -155,6 +166,7 @@ pub(crate) fn type_check<'tcx>(
155166
borrow_set,
156167
constraints: &mut constraints,
157168
polonius_liveness,
169+
placeholder_to_region,
158170
};
159171

160172
typeck.check_user_type_annotations();
@@ -221,6 +233,7 @@ struct TypeChecker<'a, 'tcx> {
221233
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
222234
/// When using `-Zpolonius=next`, the liveness helper data used to create polonius constraints.
223235
polonius_liveness: Option<PoloniusLivenessContext>,
236+
placeholder_to_region: PlaceholderToRegion<'tcx>,
224237
}
225238

226239
/// Holder struct for passing results from MIR typeck to the rest of the non-lexical regions
@@ -232,14 +245,30 @@ pub(crate) struct MirTypeckResults<'tcx> {
232245
pub(crate) polonius_context: Option<PoloniusContext>,
233246
}
234247

248+
/// For each placeholder we create a corresponding representative region vid.
249+
/// This map tracks those. This way, when we convert the same `ty::RePlaceholder(p)`
250+
/// twice, we can map to the same underlying `RegionVid`.
251+
#[derive(Default)]
252+
pub(crate) struct PlaceholderToRegion<'tcx>(FxHashMap<ty::PlaceholderRegion, ty::Region<'tcx>>);
253+
254+
impl<'tcx> PlaceholderToRegion<'tcx> {
255+
/// Creates a `Region` for a given `PlaceholderRegion`, or returns the
256+
/// region that corresponds to a previously created one.
257+
fn placeholder_region(
258+
&mut self,
259+
infcx: &InferCtxt<'tcx>,
260+
placeholder: ty::PlaceholderRegion,
261+
) -> ty::Region<'tcx> {
262+
*self.0.entry(placeholder).or_insert_with(|| {
263+
let origin = NllRegionVariableOrigin::Placeholder(placeholder);
264+
infcx.next_nll_region_var_in_universe(origin, placeholder.universe)
265+
})
266+
}
267+
}
268+
235269
/// A collection of region constraints that must be satisfied for the
236270
/// program to be considered well-typed.
237271
pub(crate) struct MirTypeckRegionConstraints<'tcx> {
238-
/// For each placeholder we create a corresponding representative region vid.
239-
/// This map tracks those. This way, when we convert the same `ty::RePlaceholder(p)`
240-
/// twice, we can map to the same underlying `RegionVid`.
241-
placeholder_to_region: FxHashMap<ty::PlaceholderRegion, ty::Region<'tcx>>,
242-
243272
/// In general, the type-checker is not responsible for enforcing
244273
/// liveness constraints; this job falls to the region inferencer,
245274
/// which performs a liveness analysis. However, in some limited
@@ -258,21 +287,6 @@ pub(crate) struct MirTypeckRegionConstraints<'tcx> {
258287
pub(crate) type_tests: Vec<TypeTest<'tcx>>,
259288
}
260289

261-
impl<'tcx> MirTypeckRegionConstraints<'tcx> {
262-
/// Creates a `Region` for a given `PlaceholderRegion`, or returns the
263-
/// region that corresponds to a previously created one.
264-
fn placeholder_region(
265-
&mut self,
266-
infcx: &InferCtxt<'tcx>,
267-
placeholder: ty::PlaceholderRegion,
268-
) -> ty::Region<'tcx> {
269-
*self.placeholder_to_region.entry(placeholder).or_insert_with(|| {
270-
let origin = NllRegionVariableOrigin::Placeholder(placeholder);
271-
infcx.next_nll_region_var_in_universe(origin, placeholder.universe)
272-
})
273-
}
274-
}
275-
276290
/// The `Locations` type summarizes *where* region constraints are
277291
/// required to hold. Normally, this is at a particular point which
278292
/// created the obligation, but for constraints that the user gave, we
@@ -350,7 +364,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
350364

351365
fn to_region_vid(&mut self, r: ty::Region<'tcx>) -> RegionVid {
352366
if let ty::RePlaceholder(placeholder) = r.kind() {
353-
self.constraints.placeholder_region(self.infcx, placeholder).as_var()
367+
self.placeholder_to_region.placeholder_region(self.infcx, placeholder).as_var()
354368
} else {
355369
self.universal_regions.to_region_vid(r)
356370
}
@@ -398,6 +412,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
398412
locations.span(self.body),
399413
category,
400414
self.constraints,
415+
&mut self.placeholder_to_region,
401416
)
402417
.convert_all(data);
403418
}
@@ -2487,6 +2502,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24872502
self.body.span, // irrelevant; will be overridden.
24882503
ConstraintCategory::Boring, // same as above.
24892504
self.constraints,
2505+
&mut self.placeholder_to_region,
24902506
)
24912507
.apply_closure_requirements(closure_requirements, def_id, args);
24922508
}

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,10 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
258258

259259
#[instrument(skip(self), level = "debug")]
260260
fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
261-
let reg =
262-
self.type_checker.constraints.placeholder_region(self.type_checker.infcx, placeholder);
261+
let reg = self
262+
.type_checker
263+
.placeholder_to_region
264+
.placeholder_region(self.type_checker.infcx, placeholder);
263265

264266
let reg_info = match placeholder.bound.kind {
265267
ty::BoundRegionKind::Anon => sym::anon,

0 commit comments

Comments
 (0)