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

Commit dced150

Browse files
committed
Auto merge of rust-lang#136038 - compiler-errors:outlives, r=<try>
Simplify and consolidate the way we handle construct `OutlivesEnvironment` for lexical region resolution ... and also remove `region_bound_pairs` which is just totally redundant. This is best reviewed commit-by-commit. I tried to consolidate the API for lexical region resolution *first*, then change the API when it was finally behind a single surface. Finally, I removed `region_bound_pairs` from borrowck + type outlives. r? lcnr or reassign
2 parents 203e6c1 + 93b52c8 commit dced150

File tree

25 files changed

+263
-341
lines changed

25 files changed

+263
-341
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_data_structures::fx::FxIndexMap;
22
use rustc_errors::ErrorGuaranteed;
3+
use rustc_hir::OpaqueTyOrigin;
34
use rustc_hir::def_id::LocalDefId;
5+
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
46
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, TyCtxtInferExt as _};
57
use rustc_macros::extension;
68
use rustc_middle::ty::fold::fold_regions;
@@ -10,6 +12,7 @@ use rustc_middle::ty::{
1012
TypingMode,
1113
};
1214
use rustc_span::Span;
15+
use rustc_trait_selection::regions::OutlivesEnvironmentBuildExt;
1316
use rustc_trait_selection::traits::ObligationCtxt;
1417
use tracing::{debug, instrument};
1518

@@ -406,20 +409,16 @@ impl<'tcx> LazyOpaqueTyEnv<'tcx> {
406409
}
407410

408411
fn get_canonical_args(&self) -> ty::GenericArgsRef<'tcx> {
409-
use rustc_hir as hir;
410-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
411-
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
412-
413412
if let Some(&canonical_args) = self.canonical_args.get() {
414413
return canonical_args;
415414
}
416415

417416
let &Self { tcx, def_id, .. } = self;
418417
let origin = tcx.local_opaque_ty_origin(def_id);
419418
let parent = match origin {
420-
hir::OpaqueTyOrigin::FnReturn { parent, .. }
421-
| hir::OpaqueTyOrigin::AsyncFn { parent, .. }
422-
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
419+
OpaqueTyOrigin::FnReturn { parent, .. }
420+
| OpaqueTyOrigin::AsyncFn { parent, .. }
421+
| OpaqueTyOrigin::TyAlias { parent, .. } => parent,
423422
};
424423
let param_env = tcx.param_env(parent);
425424
let args = GenericArgs::identity_for_item(tcx, parent).extend_to(
@@ -439,8 +438,7 @@ impl<'tcx> LazyOpaqueTyEnv<'tcx> {
439438
tcx.dcx().span_delayed_bug(tcx.def_span(def_id), "error getting implied bounds");
440439
Default::default()
441440
});
442-
let implied_bounds = infcx.implied_bounds_tys(param_env, parent, &wf_tys);
443-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
441+
let outlives_env = OutlivesEnvironment::new(&infcx, parent, param_env, wf_tys);
444442

445443
let mut seen = vec![tcx.lifetimes.re_static];
446444
let canonical_args = fold_regions(tcx, args, |r1, _| {

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use rustc_data_structures::fx::FxIndexSet;
12
use rustc_hir::def_id::DefId;
23
use rustc_infer::infer::canonical::QueryRegionConstraints;
3-
use rustc_infer::infer::outlives::env::RegionBoundPairs;
44
use rustc_infer::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
55
use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound};
66
use rustc_infer::infer::{self, InferCtxt, SubregionOrigin};
@@ -35,10 +35,9 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> {
3535
/// `process_registered_region_obligations` has some special-cased
3636
/// logic expecting to see (e.g.) `ReStatic`, and if we supplied
3737
/// our special inference variable there, we would mess that up.
38-
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
3938
implicit_region_bound: ty::Region<'tcx>,
4039
param_env: ty::ParamEnv<'tcx>,
41-
known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
40+
known_type_outlives: &'a FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
4241
locations: Locations,
4342
span: Span,
4443
category: ConstraintCategory<'tcx>,
@@ -50,10 +49,9 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
5049
pub(crate) fn new(
5150
infcx: &'a InferCtxt<'tcx>,
5251
universal_regions: &'a UniversalRegions<'tcx>,
53-
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
5452
implicit_region_bound: ty::Region<'tcx>,
5553
param_env: ty::ParamEnv<'tcx>,
56-
known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
54+
known_type_outlives: &'a FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
5755
locations: Locations,
5856
span: Span,
5957
category: ConstraintCategory<'tcx>,
@@ -63,10 +61,9 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
6361
infcx,
6462
tcx: infcx.tcx,
6563
universal_regions,
66-
region_bound_pairs,
6764
implicit_region_bound,
6865
param_env,
69-
known_type_outlives_obligations,
66+
known_type_outlives,
7067
locations,
7168
span,
7269
category,
@@ -135,9 +132,8 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
135132
let ConstraintConversion {
136133
tcx,
137134
infcx,
138-
region_bound_pairs,
139135
implicit_region_bound,
140-
known_type_outlives_obligations,
136+
known_type_outlives: known_type_outlives_obligations,
141137
..
142138
} = *self;
143139

@@ -179,7 +175,6 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
179175
TypeOutlives::new(
180176
&mut *self,
181177
tcx,
182-
region_bound_pairs,
183178
Some(implicit_region_bound),
184179
known_type_outlives_obligations,
185180
)

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use rustc_data_structures::frozen::Frozen;
2+
use rustc_data_structures::fx::FxIndexSet;
23
use rustc_data_structures::transitive_relation::{TransitiveRelation, TransitiveRelationBuilder};
34
use rustc_hir::def::DefKind;
45
use rustc_infer::infer::canonical::QueryRegionConstraints;
5-
use rustc_infer::infer::outlives::env::RegionBoundPairs;
6-
use rustc_infer::infer::region_constraints::GenericKind;
76
use rustc_infer::infer::{InferCtxt, outlives};
87
use rustc_infer::traits::ScrubbedTraitError;
98
use rustc_middle::mir::ConstraintCategory;
@@ -44,8 +43,7 @@ type NormalizedInputsAndOutput<'tcx> = Vec<Ty<'tcx>>;
4443

4544
pub(crate) struct CreateResult<'tcx> {
4645
pub(crate) universal_region_relations: Frozen<UniversalRegionRelations<'tcx>>,
47-
pub(crate) region_bound_pairs: RegionBoundPairs<'tcx>,
48-
pub(crate) known_type_outlives_obligations: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
46+
pub(crate) known_type_outlives: FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
4947
pub(crate) normalized_inputs_and_output: NormalizedInputsAndOutput<'tcx>,
5048
}
5149

@@ -62,7 +60,7 @@ pub(crate) fn create<'tcx>(
6260
implicit_region_bound,
6361
constraints,
6462
universal_regions,
65-
region_bound_pairs: Default::default(),
63+
known_type_outlives: FxIndexSet::default(),
6664
outlives: Default::default(),
6765
inverse_outlives: Default::default(),
6866
}
@@ -189,8 +187,8 @@ struct UniversalRegionRelationsBuilder<'a, 'tcx> {
189187

190188
// outputs:
191189
outlives: TransitiveRelationBuilder<RegionVid>,
190+
known_type_outlives: FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
192191
inverse_outlives: TransitiveRelationBuilder<RegionVid>,
193-
region_bound_pairs: RegionBoundPairs<'tcx>,
194192
}
195193

196194
impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
@@ -228,15 +226,9 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
228226

229227
// Normalize the assumptions we use to borrowck the program.
230228
let mut constraints = vec![];
231-
let mut known_type_outlives_obligations = vec![];
232229
for bound in param_env.caller_bounds() {
233230
if let Some(outlives) = bound.as_type_outlives_clause() {
234-
self.normalize_and_push_type_outlives_obligation(
235-
outlives,
236-
span,
237-
&mut known_type_outlives_obligations,
238-
&mut constraints,
239-
);
231+
self.normalize_and_push_type_outlives_obligation(outlives, span, &mut constraints);
240232
};
241233
}
242234

@@ -251,8 +243,8 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
251243
// - Normalize the type. This will create some region
252244
// constraints, which we buffer up because we are
253245
// not ready to process them yet.
254-
// - Then compute the implied bounds. This will adjust
255-
// the `region_bound_pairs` and so forth.
246+
// - Then compute the implied bounds, updating the
247+
// known outlives types and free region regions.
256248
// - After this is done, we'll process the constraints, once
257249
// the `relations` is built.
258250
let mut normalized_inputs_and_output =
@@ -323,10 +315,9 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
323315
constraint_conversion::ConstraintConversion::new(
324316
self.infcx,
325317
&self.universal_regions,
326-
&self.region_bound_pairs,
327318
self.implicit_region_bound,
328319
param_env,
329-
&known_type_outlives_obligations,
320+
&self.known_type_outlives,
330321
Locations::All(span),
331322
span,
332323
ConstraintCategory::Internal,
@@ -341,17 +332,15 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
341332
outlives: self.outlives.freeze(),
342333
inverse_outlives: self.inverse_outlives.freeze(),
343334
}),
344-
known_type_outlives_obligations,
345-
region_bound_pairs: self.region_bound_pairs,
335+
known_type_outlives: self.known_type_outlives,
346336
normalized_inputs_and_output,
347337
}
348338
}
349339

350340
fn normalize_and_push_type_outlives_obligation(
351-
&self,
341+
&mut self,
352342
mut outlives: ty::PolyTypeOutlivesPredicate<'tcx>,
353343
span: Span,
354-
known_type_outlives_obligations: &mut Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
355344
constraints: &mut Vec<&QueryRegionConstraints<'tcx>>,
356345
) {
357346
// In the new solver, normalize the type-outlives obligation assumptions.
@@ -382,7 +371,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
382371
}
383372
}
384373

385-
known_type_outlives_obligations.push(outlives);
374+
self.known_type_outlives.insert(outlives);
386375
}
387376

388377
/// Update the type of a single local, which should represent
@@ -428,13 +417,17 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
428417
}
429418

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

435426
OutlivesBound::RegionSubAlias(r_a, alias_b) => {
436-
self.region_bound_pairs
437-
.insert(ty::OutlivesPredicate(GenericKind::Alias(alias_b), r_a));
427+
self.known_type_outlives.insert(ty::Binder::dummy(ty::OutlivesPredicate(
428+
Ty::new_alias(self.infcx.tcx, alias_b.kind(self.infcx.tcx), alias_b),
429+
r_a,
430+
)));
438431
}
439432
}
440433
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ 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;
1716
use rustc_infer::infer::region_constraints::RegionConstraintData;
1817
use rustc_infer::infer::{
1918
BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
@@ -129,9 +128,8 @@ pub(crate) fn type_check<'a, 'tcx>(
129128

130129
let CreateResult {
131130
universal_region_relations,
132-
region_bound_pairs,
133131
normalized_inputs_and_output,
134-
known_type_outlives_obligations,
132+
known_type_outlives,
135133
} = free_region_relations::create(
136134
infcx,
137135
infcx.param_env,
@@ -159,8 +157,7 @@ pub(crate) fn type_check<'a, 'tcx>(
159157
last_span: body.span,
160158
body,
161159
user_type_annotations: &body.user_type_annotations,
162-
region_bound_pairs,
163-
known_type_outlives_obligations,
160+
known_type_outlives,
164161
implicit_region_bound,
165162
reported_errors: Default::default(),
166163
universal_regions: &universal_region_relations.universal_regions,
@@ -555,8 +552,7 @@ struct TypeChecker<'a, 'tcx> {
555552
/// User type annotations are shared between the main MIR and the MIR of
556553
/// all of the promoted items.
557554
user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>,
558-
region_bound_pairs: RegionBoundPairs<'tcx>,
559-
known_type_outlives_obligations: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
555+
known_type_outlives: FxIndexSet<ty::PolyTypeOutlivesPredicate<'tcx>>,
560556
implicit_region_bound: ty::Region<'tcx>,
561557
reported_errors: FxIndexSet<(Ty<'tcx>, Span)>,
562558
universal_regions: &'a UniversalRegions<'tcx>,
@@ -748,10 +744,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
748744
constraint_conversion::ConstraintConversion::new(
749745
self.infcx,
750746
self.universal_regions,
751-
&self.region_bound_pairs,
752747
self.implicit_region_bound,
753748
self.infcx.param_env,
754-
&self.known_type_outlives_obligations,
749+
&self.known_type_outlives,
755750
locations,
756751
locations.span(self.body),
757752
category,
@@ -2531,10 +2526,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25312526
constraint_conversion::ConstraintConversion::new(
25322527
self.infcx,
25332528
self.universal_regions,
2534-
&self.region_bound_pairs,
25352529
self.implicit_region_bound,
25362530
self.infcx.param_env,
2537-
&self.known_type_outlives_obligations,
2531+
&self.known_type_outlives,
25382532
locations,
25392533
self.body.span, // irrelevant; will be overridden.
25402534
ConstraintCategory::Boring, // same as above.

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use rustc_session::lint::builtin::UNINHABITED_STATIC;
2727
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
2828
use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedDirective;
2929
use rustc_trait_selection::traits;
30-
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
3130
use rustc_type_ir::fold::TypeFoldable;
3231
use tracing::{debug, instrument};
3332
use ty::TypingMode;
@@ -417,9 +416,7 @@ fn check_opaque_meets_bounds<'tcx>(
417416
}
418417

419418
let wf_tys = ocx.assumed_wf_types_and_report_errors(param_env, defining_use_anchor)?;
420-
let implied_bounds = infcx.implied_bounds_tys(param_env, def_id, &wf_tys);
421-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
422-
ocx.resolve_regions_and_report_errors(defining_use_anchor, &outlives_env)?;
419+
ocx.resolve_regions_and_report_errors(defining_use_anchor, param_env, wf_tys)?;
423420

424421
if infcx.next_trait_solver() {
425422
Ok(())

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_errors::{Applicability, ErrorGuaranteed, pluralize, struct_span_code_e
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::intravisit::VisitorExt;
1111
use rustc_hir::{self as hir, AmbigArg, GenericParamKind, ImplItemKind, intravisit};
12-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1312
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
1413
use rustc_infer::traits::util;
1514
use rustc_middle::ty::error::{ExpectedFound, TypeError};
@@ -24,7 +23,6 @@ use rustc_span::Span;
2423
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
2524
use rustc_trait_selection::infer::InferCtxtExt;
2625
use rustc_trait_selection::regions::InferCtxtRegionExt;
27-
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
2826
use rustc_trait_selection::traits::{
2927
self, FulfillmentError, ObligationCause, ObligationCauseCode, ObligationCtxt,
3028
};
@@ -416,11 +414,7 @@ fn compare_method_predicate_entailment<'tcx>(
416414

417415
// Finally, resolve all regions. This catches wily misuses of
418416
// lifetime parameters.
419-
let outlives_env = OutlivesEnvironment::with_bounds(
420-
param_env,
421-
infcx.implied_bounds_tys(param_env, impl_m_def_id, &wf_tys),
422-
);
423-
let errors = infcx.resolve_regions(&outlives_env);
417+
let errors = infcx.resolve_regions(impl_m_def_id, param_env, wf_tys);
424418
if !errors.is_empty() {
425419
return Err(infcx
426420
.tainted_by_errors()
@@ -725,11 +719,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
725719

726720
// Finally, resolve all regions. This catches wily misuses of
727721
// lifetime parameters.
728-
let outlives_env = OutlivesEnvironment::with_bounds(
729-
param_env,
730-
infcx.implied_bounds_tys(param_env, impl_m_def_id, &wf_tys),
731-
);
732-
ocx.resolve_regions_and_report_errors(impl_m_def_id, &outlives_env)?;
722+
ocx.resolve_regions_and_report_errors(impl_m_def_id, param_env, wf_tys)?;
733723

734724
let mut remapped_types = DefIdMap::default();
735725
for (def_id, (ty, args)) in collected_types {
@@ -1883,8 +1873,7 @@ fn compare_const_predicate_entailment<'tcx>(
18831873
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
18841874
}
18851875

1886-
let outlives_env = OutlivesEnvironment::new(param_env);
1887-
ocx.resolve_regions_and_report_errors(impl_ct_def_id, &outlives_env)
1876+
ocx.resolve_regions_and_report_errors(impl_ct_def_id, param_env, [])
18881877
}
18891878

18901879
#[instrument(level = "debug", skip(tcx))]
@@ -2017,8 +2006,7 @@ fn compare_type_predicate_entailment<'tcx>(
20172006

20182007
// Finally, resolve all regions. This catches wily misuses of
20192008
// lifetime parameters.
2020-
let outlives_env = OutlivesEnvironment::new(param_env);
2021-
ocx.resolve_regions_and_report_errors(impl_ty_def_id, &outlives_env)
2009+
ocx.resolve_regions_and_report_errors(impl_ty_def_id, param_env, [])
20222010
}
20232011

20242012
/// Validate that `ProjectionCandidate`s created for this associated type will
@@ -2147,9 +2135,7 @@ pub(super) fn check_type_bounds<'tcx>(
21472135

21482136
// Finally, resolve all regions. This catches wily misuses of
21492137
// lifetime parameters.
2150-
let implied_bounds = infcx.implied_bounds_tys(param_env, impl_ty_def_id, &assumed_wf_types);
2151-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
2152-
ocx.resolve_regions_and_report_errors(impl_ty_def_id, &outlives_env)
2138+
ocx.resolve_regions_and_report_errors(impl_ty_def_id, param_env, assumed_wf_types)
21532139
}
21542140

21552141
struct ReplaceTy<'tcx> {

0 commit comments

Comments
 (0)