Skip to content

Commit 59a7fd4

Browse files
committed
Auto merge of #136038 - compiler-errors:outlives, r=
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 814ebca + 485c6f4 commit 59a7fd4

File tree

24 files changed

+253
-341
lines changed

24 files changed

+253
-341
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+7-9
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

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_hir::def_id::DefId;
22
use rustc_infer::infer::canonical::QueryRegionConstraints;
3-
use rustc_infer::infer::outlives::env::RegionBoundPairs;
43
use rustc_infer::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
54
use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound};
65
use rustc_infer::infer::{self, InferCtxt, SubregionOrigin};
@@ -35,10 +34,9 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> {
3534
/// `process_registered_region_obligations` has some special-cased
3635
/// logic expecting to see (e.g.) `ReStatic`, and if we supplied
3736
/// our special inference variable there, we would mess that up.
38-
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
3937
implicit_region_bound: ty::Region<'tcx>,
4038
param_env: ty::ParamEnv<'tcx>,
41-
known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
39+
known_type_outlives: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
4240
locations: Locations,
4341
span: Span,
4442
category: ConstraintCategory<'tcx>,
@@ -50,10 +48,9 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
5048
pub(crate) fn new(
5149
infcx: &'a InferCtxt<'tcx>,
5250
universal_regions: &'a UniversalRegions<'tcx>,
53-
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
5451
implicit_region_bound: ty::Region<'tcx>,
5552
param_env: ty::ParamEnv<'tcx>,
56-
known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
53+
known_type_outlives: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
5754
locations: Locations,
5855
span: Span,
5956
category: ConstraintCategory<'tcx>,
@@ -63,10 +60,9 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
6360
infcx,
6461
tcx: infcx.tcx,
6562
universal_regions,
66-
region_bound_pairs,
6763
implicit_region_bound,
6864
param_env,
69-
known_type_outlives_obligations,
65+
known_type_outlives,
7066
locations,
7167
span,
7268
category,
@@ -135,9 +131,8 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
135131
let ConstraintConversion {
136132
tcx,
137133
infcx,
138-
region_bound_pairs,
139134
implicit_region_bound,
140-
known_type_outlives_obligations,
135+
known_type_outlives: known_type_outlives_obligations,
141136
..
142137
} = *self;
143138

@@ -179,7 +174,6 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
179174
TypeOutlives::new(
180175
&mut *self,
181176
tcx,
182-
region_bound_pairs,
183177
Some(implicit_region_bound),
184178
known_type_outlives_obligations,
185179
)

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+18-26
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ 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;
75
use rustc_infer::infer::{InferCtxt, outlives};
86
use rustc_infer::traits::ScrubbedTraitError;
97
use rustc_middle::mir::ConstraintCategory;
@@ -44,8 +42,7 @@ type NormalizedInputsAndOutput<'tcx> = Vec<Ty<'tcx>>;
4442

4543
pub(crate) struct CreateResult<'tcx> {
4644
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>>,
45+
pub(crate) known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
4946
pub(crate) normalized_inputs_and_output: NormalizedInputsAndOutput<'tcx>,
5047
}
5148

@@ -62,7 +59,7 @@ pub(crate) fn create<'tcx>(
6259
implicit_region_bound,
6360
constraints,
6461
universal_regions,
65-
region_bound_pairs: Default::default(),
62+
known_type_outlives: vec![],
6663
outlives: Default::default(),
6764
inverse_outlives: Default::default(),
6865
}
@@ -189,8 +186,8 @@ struct UniversalRegionRelationsBuilder<'a, 'tcx> {
189186

190187
// outputs:
191188
outlives: TransitiveRelationBuilder<RegionVid>,
189+
known_type_outlives: Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
192190
inverse_outlives: TransitiveRelationBuilder<RegionVid>,
193-
region_bound_pairs: RegionBoundPairs<'tcx>,
194191
}
195192

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

229226
// Normalize the assumptions we use to borrowck the program.
230227
let mut constraints = vec![];
231-
let mut known_type_outlives_obligations = vec![];
232228
for bound in param_env.caller_bounds() {
233229
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-
);
230+
self.normalize_and_push_type_outlives_obligation(outlives, span, &mut constraints);
240231
};
241232
}
242233

@@ -251,8 +242,8 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
251242
// - Normalize the type. This will create some region
252243
// constraints, which we buffer up because we are
253244
// not ready to process them yet.
254-
// - Then compute the implied bounds. This will adjust
255-
// the `region_bound_pairs` and so forth.
245+
// - Then compute the implied bounds, updating the
246+
// known outlives types and free region regions.
256247
// - After this is done, we'll process the constraints, once
257248
// the `relations` is built.
258249
let mut normalized_inputs_and_output =
@@ -323,10 +314,9 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
323314
constraint_conversion::ConstraintConversion::new(
324315
self.infcx,
325316
&self.universal_regions,
326-
&self.region_bound_pairs,
327317
self.implicit_region_bound,
328318
param_env,
329-
&known_type_outlives_obligations,
319+
&self.known_type_outlives,
330320
Locations::All(span),
331321
span,
332322
ConstraintCategory::Internal,
@@ -341,17 +331,15 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
341331
outlives: self.outlives.freeze(),
342332
inverse_outlives: self.inverse_outlives.freeze(),
343333
}),
344-
known_type_outlives_obligations,
345-
region_bound_pairs: self.region_bound_pairs,
334+
known_type_outlives: self.known_type_outlives,
346335
normalized_inputs_and_output,
347336
}
348337
}
349338

350339
fn normalize_and_push_type_outlives_obligation(
351-
&self,
340+
&mut self,
352341
mut outlives: ty::PolyTypeOutlivesPredicate<'tcx>,
353342
span: Span,
354-
known_type_outlives_obligations: &mut Vec<ty::PolyTypeOutlivesPredicate<'tcx>>,
355343
constraints: &mut Vec<&QueryRegionConstraints<'tcx>>,
356344
) {
357345
// In the new solver, normalize the type-outlives obligation assumptions.
@@ -382,7 +370,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
382370
}
383371
}
384372

385-
known_type_outlives_obligations.push(outlives);
373+
self.known_type_outlives.push(outlives);
386374
}
387375

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

430418
OutlivesBound::RegionSubParam(r_a, param_b) => {
431-
self.region_bound_pairs
432-
.insert(ty::OutlivesPredicate(GenericKind::Param(param_b), r_a));
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+
)));
433423
}
434424

435425
OutlivesBound::RegionSubAlias(r_a, alias_b) => {
436-
self.region_bound_pairs
437-
.insert(ty::OutlivesPredicate(GenericKind::Alias(alias_b), r_a));
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+
)));
438430
}
439431
}
440432
}

compiler/rustc_borrowck/src/type_check/mod.rs

+5-11
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: Vec<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

+1-4
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

+5-19
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)