Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 415429f

Browse files
Revert "Remove region_bound_pairs"
This reverts commit 485c6f4.
1 parent 485c6f4 commit 415429f

File tree

6 files changed

+119
-47
lines changed

6 files changed

+119
-47
lines changed

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_hir::def_id::DefId;
22
use rustc_infer::infer::canonical::QueryRegionConstraints;
3+
use rustc_infer::infer::outlives::env::RegionBoundPairs;
34
use rustc_infer::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
45
use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound};
56
use rustc_infer::infer::{self, InferCtxt, SubregionOrigin};
@@ -34,9 +35,10 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> {
3435
/// `process_registered_region_obligations` has some special-cased
3536
/// logic expecting to see (e.g.) `ReStatic`, and if we supplied
3637
/// our special inference variable there, we would mess that up.
38+
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
3739
implicit_region_bound: ty::Region<'tcx>,
3840
param_env: ty::ParamEnv<'tcx>,
39-
known_type_outlives: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
41+
known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
4042
locations: Locations,
4143
span: Span,
4244
category: ConstraintCategory<'tcx>,
@@ -48,9 +50,10 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
4850
pub(crate) fn new(
4951
infcx: &'a InferCtxt<'tcx>,
5052
universal_regions: &'a UniversalRegions<'tcx>,
53+
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
5154
implicit_region_bound: ty::Region<'tcx>,
5255
param_env: ty::ParamEnv<'tcx>,
53-
known_type_outlives: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
56+
known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
5457
locations: Locations,
5558
span: Span,
5659
category: ConstraintCategory<'tcx>,
@@ -60,9 +63,10 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
6063
infcx,
6164
tcx: infcx.tcx,
6265
universal_regions,
66+
region_bound_pairs,
6367
implicit_region_bound,
6468
param_env,
65-
known_type_outlives,
69+
known_type_outlives_obligations,
6670
locations,
6771
span,
6872
category,
@@ -131,8 +135,9 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
131135
let ConstraintConversion {
132136
tcx,
133137
infcx,
138+
region_bound_pairs,
134139
implicit_region_bound,
135-
known_type_outlives: known_type_outlives_obligations,
140+
known_type_outlives_obligations,
136141
..
137142
} = *self;
138143

@@ -174,6 +179,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
174179
TypeOutlives::new(
175180
&mut *self,
176181
tcx,
182+
region_bound_pairs,
177183
Some(implicit_region_bound),
178184
known_type_outlives_obligations,
179185
)

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use rustc_data_structures::frozen::Frozen;
22
use rustc_data_structures::transitive_relation::{TransitiveRelation, TransitiveRelationBuilder};
33
use rustc_hir::def::DefKind;
44
use rustc_infer::infer::canonical::QueryRegionConstraints;
5+
use rustc_infer::infer::outlives::env::RegionBoundPairs;
6+
use rustc_infer::infer::region_constraints::GenericKind;
57
use rustc_infer::infer::{InferCtxt, outlives};
68
use rustc_infer::traits::ScrubbedTraitError;
79
use rustc_middle::mir::ConstraintCategory;
@@ -42,7 +44,8 @@ type NormalizedInputsAndOutput<'tcx> = Vec<Ty<'tcx>>;
4244

4345
pub(crate) struct CreateResult<'tcx> {
4446
pub(crate) universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
45-
pub(crate) known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
47+
pub(crate) region_bound_pairs: RegionBoundPairs<'tcx>,
48+
pub(crate) known_type_outlives_obligations: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
4649
pub(crate) normalized_inputs_and_output: NormalizedInputsAndOutput<'tcx>,
4750
}
4851

@@ -59,7 +62,7 @@ pub(crate) fn create<'tcx>(
5962
implicit_region_bound,
6063
constraints,
6164
universal_regions,
62-
known_type_outlives: vec![],
65+
region_bound_pairs: Default::default(),
6366
outlives: Default::default(),
6467
inverse_outlives: Default::default(),
6568
}
@@ -186,8 +189,8 @@ struct UniversalRegionRelationsBuilder<'a, 'tcx> {
186189

187190
// outputs:
188191
outlives: TransitiveRelationBuilder<RegionVid>,
189-
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
190192
inverse_outlives: TransitiveRelationBuilder<RegionVid>,
193+
region_bound_pairs: RegionBoundPairs<'tcx>,
191194
}
192195

193196
impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
@@ -225,9 +228,15 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
225228

226229
// Normalize the assumptions we use to borrowck the program.
227230
let mut constraints = vec![];
231+
let mut known_type_outlives_obligations = vec![];
228232
for bound in param_env.caller_bounds() {
229233
if let Some(outlives) = bound.as_type_outlives_clause() {
230-
self.normalize_and_push_type_outlives_obligation(outlives, span, &mut constraints);
234+
self.normalize_and_push_type_outlives_obligation(
235+
outlives,
236+
span,
237+
&mut known_type_outlives_obligations,
238+
&mut constraints,
239+
);
231240
};
232241
}
233242

@@ -242,8 +251,8 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
242251
// - Normalize the type. This will create some region
243252
// constraints, which we buffer up because we are
244253
// not ready to process them yet.
245-
// - Then compute the implied bounds, updating the
246-
// known outlives types and free region regions.
254+
// - Then compute the implied bounds. This will adjust
255+
// the `region_bound_pairs` and so forth.
247256
// - After this is done, we'll process the constraints, once
248257
// the `relations` is built.
249258
let mut normalized_inputs_and_output =
@@ -314,9 +323,10 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
314323
constraint_conversion::ConstraintConversion::new(
315324
self.infcx,
316325
&self.universal_regions,
326+
&self.region_bound_pairs,
317327
self.implicit_region_bound,
318328
param_env,
319-
&self.known_type_outlives,
329+
&known_type_outlives_obligations,
320330
Locations::All(span),
321331
span,
322332
ConstraintCategory::Internal,
@@ -331,15 +341,17 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
331341
outlives: self.outlives.freeze(),
332342
inverse_outlives: self.inverse_outlives.freeze(),
333343
}),
334-
known_type_outlives: self.known_type_outlives,
344+
known_type_outlives_obligations,
345+
region_bound_pairs: self.region_bound_pairs,
335346
normalized_inputs_and_output,
336347
}
337348
}
338349

339350
fn normalize_and_push_type_outlives_obligation(
340-
&mut self,
351+
&self,
341352
mut outlives: ty::PolyTypeOutlivesPredicate<'tcx>,
342353
span: Span,
354+
known_type_outlives_obligations: &mut Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
343355
constraints: &mut Vec<&QueryRegionConstraints<'tcx>>,
344356
) {
345357
// In the new solver, normalize the type-outlives obligation assumptions.
@@ -370,7 +382,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
370382
}
371383
}
372384

373-
self.known_type_outlives.push(outlives);
385+
known_type_outlives_obligations.push(outlives);
374386
}
375387

376388
/// Update the type of a single local, which should represent
@@ -416,17 +428,13 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
416428
}
417429

418430
OutlivesBound::RegionSubParam(r_a, param_b) => {
419-
self.known_type_outlives.push(ty::Binder::dummy(ty::OutlivesPredicate(
420-
Ty::new_param(self.infcx.tcx, param_b.index, param_b.name),
421-
r_a,
422-
)));
431+
self.region_bound_pairs
432+
.insert(ty::OutlivesPredicate(GenericKind::Param(param_b), r_a));
423433
}
424434

425435
OutlivesBound::RegionSubAlias(r_a, alias_b) => {
426-
self.known_type_outlives.push(ty::Binder::dummy(ty::OutlivesPredicate(
427-
Ty::new_alias(self.infcx.tcx, alias_b.kind(self.infcx.tcx), alias_b),
428-
r_a,
429-
)));
436+
self.region_bound_pairs
437+
.insert(ty::OutlivesPredicate(GenericKind::Alias(alias_b), r_a));
430438
}
431439
}
432440
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_hir::def_id::LocalDefId;
1313
use rustc_hir::lang_items::LangItem;
1414
use rustc_index::{IndexSlice, IndexVec};
1515
use rustc_infer::infer::canonical::QueryRegionConstraints;
16+
use rustc_infer::infer::outlives::env::RegionBoundPairs;
1617
use rustc_infer::infer::region_constraints::RegionConstraintData;
1718
use rustc_infer::infer::{
1819
BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
@@ -128,8 +129,9 @@ pub(crate) fn type_check<'a, 'tcx>(
128129

129130
let CreateResult {
130131
universal_region_relations,
132+
region_bound_pairs,
131133
normalized_inputs_and_output,
132-
known_type_outlives,
134+
known_type_outlives_obligations,
133135
} = free_region_relations::create(
134136
infcx,
135137
infcx.param_env,
@@ -157,7 +159,8 @@ pub(crate) fn type_check<'a, 'tcx>(
157159
last_span: body.span,
158160
body,
159161
user_type_annotations: &body.user_type_annotations,
160-
known_type_outlives,
162+
region_bound_pairs,
163+
known_type_outlives_obligations,
161164
implicit_region_bound,
162165
reported_errors: Default::default(),
163166
universal_regions: &universal_region_relations.universal_regions,
@@ -552,7 +555,8 @@ struct TypeChecker<'a, 'tcx> {
552555
/// User type annotations are shared between the main MIR and the MIR of
553556
/// all of the promoted items.
554557
user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>,
555-
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
558+
region_bound_pairs: RegionBoundPairs<'tcx>,
559+
known_type_outlives_obligations: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
556560
implicit_region_bound: ty::Region<'tcx>,
557561
reported_errors: FxIndexSet<(Ty<'tcx>, Span)>,
558562
universal_regions: &'a UniversalRegions<'tcx>,
@@ -744,9 +748,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
744748
constraint_conversion::ConstraintConversion::new(
745749
self.infcx,
746750
self.universal_regions,
751+
&self.region_bound_pairs,
747752
self.implicit_region_bound,
748753
self.infcx.param_env,
749-
&self.known_type_outlives,
754+
&self.known_type_outlives_obligations,
750755
locations,
751756
locations.span(self.body),
752757
category,
@@ -2526,9 +2531,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25262531
constraint_conversion::ConstraintConversion::new(
25272532
self.infcx,
25282533
self.universal_regions,
2534+
&self.region_bound_pairs,
25292535
self.implicit_region_bound,
25302536
self.infcx.param_env,
2531-
&self.known_type_outlives,
2537+
&self.known_type_outlives_obligations,
25322538
locations,
25332539
self.body.span, // irrelevant; will be overridden.
25342540
ConstraintCategory::Boring, // same as above.

compiler/rustc_infer/src/infer/outlives/env.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
use rustc_data_structures::fx::FxIndexSet;
12
use rustc_data_structures::transitive_relation::TransitiveRelationBuilder;
23
use rustc_middle::{bug, ty};
34

5+
use crate::infer::GenericKind;
46
use crate::infer::free_regions::FreeRegionMap;
57

68
/// The `OutlivesEnvironment` collects information about what outlives
79
/// what in a given type-checking setting. For example, if we have a
810
/// where-clause like `where T: 'a` in scope, then the
9-
/// `OutlivesEnvironment` would record that. Similarly, it contains
10-
/// methods for processing and adding implied bounds into the outlives
11+
/// `OutlivesEnvironment` would record that (in its
12+
/// `region_bound_pairs` field). Similarly, it contains methods for
13+
/// processing and adding implied bounds into the outlives
1114
/// environment.
1215
///
1316
/// Other code at present does not typically take a
@@ -28,6 +31,11 @@ pub struct OutlivesEnvironment<'tcx> {
2831
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
2932
}
3033

34+
/// "Region-bound pairs" tracks outlives relations that are known to
35+
/// be true, either because of explicit where-clauses like `T: 'a` or
36+
/// because of implied bounds.
37+
pub type RegionBoundPairs<'tcx> = FxIndexSet<ty::OutlivesPredicate<'tcx, GenericKind<'tcx>>>;
38+
3139
impl<'tcx> OutlivesEnvironment<'tcx> {
3240
/// Create a new `OutlivesEnvironment` from normalized outlives bounds.
3341
pub fn from_normalized_bounds(
@@ -85,7 +93,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
8593
&self.free_region_map
8694
}
8795

88-
/// Borrows current `known_type_outlives`.
96+
/// Borrows current `region_bound_pairs`.
8997
pub fn known_type_outlives(&self) -> &[ty::PolyTypeOutlivesPredicate<'tcx>] {
9098
&self.known_type_outlives
9199
}

compiler/rustc_infer/src/infer/outlives/obligations.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use smallvec::smallvec;
7272
use tracing::{debug, instrument};
7373

7474
use super::env::OutlivesEnvironment;
75+
use crate::infer::outlives::env::RegionBoundPairs;
7576
use crate::infer::outlives::verify::VerifyBoundCx;
7677
use crate::infer::resolve::OpportunisticRegionResolver;
7778
use crate::infer::snapshot::undo_log::UndoLog;
@@ -168,9 +169,12 @@ impl<'tcx> InferCtxt<'tcx> {
168169

169170
debug!(?sup_type, ?sub_region, ?origin);
170171

172+
let region_bound_pairs = &Default::default();
171173
let outlives = &mut TypeOutlives::new(
172174
self,
173175
self.tcx,
176+
// TODO:
177+
region_bound_pairs,
174178
None,
175179
outlives_env.known_type_outlives(),
176180
);
@@ -225,13 +229,19 @@ where
225229
pub fn new(
226230
delegate: D,
227231
tcx: TyCtxt<'tcx>,
232+
region_bound_pairs: &'cx RegionBoundPairs<'tcx>,
228233
implicit_region_bound: Option<ty::Region<'tcx>>,
229-
known_type_outlives: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
234+
caller_bounds: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
230235
) -> Self {
231236
Self {
232237
delegate,
233238
tcx,
234-
verify_bound: VerifyBoundCx::new(tcx, implicit_region_bound, known_type_outlives),
239+
verify_bound: VerifyBoundCx::new(
240+
tcx,
241+
region_bound_pairs,
242+
implicit_region_bound,
243+
caller_bounds,
244+
),
235245
}
236246
}
237247

0 commit comments

Comments
 (0)