Skip to content

Commit 5a45ab9

Browse files
committed
Auto merge of rust-lang#136038 - compiler-errors:outlives, r=lcnr
Simplify and consolidate the way we handle construct `OutlivesEnvironment` for lexical region resolution 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. r? lcnr or reassign
2 parents e6f12c8 + 8e0909d commit 5a45ab9

File tree

20 files changed

+165
-197
lines changed

20 files changed

+165
-197
lines changed

Diff for: 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, _| {

Diff for: 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(())

Diff for: 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> {

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use rustc_data_structures::fx::FxIndexSet;
33
use rustc_hir as hir;
44
use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_infer::infer::TyCtxtInferExt;
6-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
76
use rustc_lint_defs::builtin::{REFINING_IMPL_TRAIT_INTERNAL, REFINING_IMPL_TRAIT_REACHABLE};
87
use rustc_middle::span_bug;
98
use rustc_middle::traits::ObligationCause;
@@ -13,7 +12,6 @@ use rustc_middle::ty::{
1312
};
1413
use rustc_span::Span;
1514
use rustc_trait_selection::regions::InferCtxtRegionExt;
16-
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt;
1715
use rustc_trait_selection::traits::{ObligationCtxt, elaborate, normalize_param_env_or_error};
1816

1917
/// Check that an implementation does not refine an RPITIT from a trait method signature.
@@ -170,11 +168,7 @@ pub(crate) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
170168
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (selection)");
171169
return;
172170
}
173-
let outlives_env = OutlivesEnvironment::with_bounds(
174-
param_env,
175-
infcx.implied_bounds_tys(param_env, impl_m.def_id.expect_local(), &implied_wf_types),
176-
);
177-
let errors = infcx.resolve_regions(&outlives_env);
171+
let errors = infcx.resolve_regions(impl_m.def_id.expect_local(), param_env, implied_wf_types);
178172
if !errors.is_empty() {
179173
tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (regions)");
180174
return;

Diff for: compiler/rustc_hir_analysis/src/check/dropck.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use rustc_data_structures::fx::FxHashSet;
66
use rustc_errors::codes::*;
77
use rustc_errors::{ErrorGuaranteed, struct_span_code_err};
8-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
98
use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt};
109
use rustc_infer::traits::{ObligationCause, ObligationCauseCode};
1110
use rustc_middle::ty::util::CheckRegions;
@@ -192,7 +191,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
192191
return Err(guar.unwrap());
193192
}
194193

195-
let errors = ocx.infcx.resolve_regions(&OutlivesEnvironment::new(adt_env));
194+
let errors = ocx.infcx.resolve_regions(adt_def_id, adt_env, []);
196195
if !errors.is_empty() {
197196
let mut guar = None;
198197
for error in errors {

Diff for: compiler/rustc_hir_analysis/src/check/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ use rustc_errors::{Diag, ErrorGuaranteed, pluralize, struct_span_code_err};
8080
use rustc_hir::def_id::{DefId, LocalDefId};
8181
use rustc_hir::intravisit::Visitor;
8282
use rustc_index::bit_set::DenseBitSet;
83-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
8483
use rustc_infer::infer::{self, TyCtxtInferExt as _};
8584
use rustc_infer::traits::ObligationCause;
8685
use rustc_middle::query::Providers;
@@ -655,8 +654,7 @@ pub fn check_function_signature<'tcx>(
655654
}
656655
}
657656

658-
let outlives_env = OutlivesEnvironment::new(param_env);
659-
if let Err(e) = ocx.resolve_regions_and_report_errors(local_id, &outlives_env) {
657+
if let Err(e) = ocx.resolve_regions_and_report_errors(local_id, param_env, []) {
660658
return Err(e);
661659
}
662660

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ use rustc_middle::{bug, span_bug};
2525
use rustc_session::parse::feature_err;
2626
use rustc_span::{DUMMY_SP, Ident, Span, sym};
2727
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
28-
use rustc_trait_selection::regions::InferCtxtRegionExt;
28+
use rustc_trait_selection::regions::{InferCtxtRegionExt, OutlivesEnvironmentBuildExt};
2929
use rustc_trait_selection::traits::misc::{
3030
ConstParamTyImplementationError, type_allowed_to_implement_const_param_ty,
3131
};
32-
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
3332
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
3433
use rustc_trait_selection::traits::{
3534
self, FulfillmentError, Obligation, ObligationCause, ObligationCauseCode, ObligationCtxt,
@@ -128,13 +127,17 @@ where
128127
let infcx_compat = infcx.fork();
129128

130129
// We specifically want to call the non-compat version of `implied_bounds_tys`; we do this always.
131-
let implied_bounds =
132-
infcx.implied_bounds_tys_compat(param_env, body_def_id, &assumed_wf_types, false);
133-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
130+
let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
131+
&infcx,
132+
body_def_id,
133+
param_env,
134+
assumed_wf_types.iter().copied(),
135+
false,
136+
);
134137

135138
lint_redundant_lifetimes(tcx, body_def_id, &outlives_env);
136139

137-
let errors = infcx.resolve_regions(&outlives_env);
140+
let errors = infcx.resolve_regions_with_outlives_env(&outlives_env);
138141
if errors.is_empty() {
139142
return Ok(());
140143
}
@@ -172,10 +175,14 @@ where
172175
// but that does result in slightly more work when this option is set and
173176
// just obscures what we mean here anyways. Let's just be explicit.
174177
if is_bevy && !infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat {
175-
let implied_bounds =
176-
infcx_compat.implied_bounds_tys_compat(param_env, body_def_id, &assumed_wf_types, true);
177-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
178-
let errors_compat = infcx_compat.resolve_regions(&outlives_env);
178+
let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
179+
&infcx,
180+
body_def_id,
181+
param_env,
182+
assumed_wf_types,
183+
true,
184+
);
185+
let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
179186
if errors_compat.is_empty() {
180187
Ok(())
181188
} else {
@@ -769,12 +776,7 @@ fn test_region_obligations<'tcx>(
769776

770777
add_constraints(&infcx);
771778

772-
let outlives_environment = OutlivesEnvironment::with_bounds(
773-
param_env,
774-
infcx.implied_bounds_tys(param_env, id, wf_tys),
775-
);
776-
777-
let errors = infcx.resolve_regions(&outlives_environment);
779+
let errors = infcx.resolve_regions(id, param_env, wf_tys.iter().copied());
778780
debug!(?errors, "errors");
779781

780782
// If we were able to prove that the type outlives the region without

Diff for: compiler/rustc_hir_analysis/src/coherence/builtin.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_hir as hir;
1010
use rustc_hir::ItemKind;
1111
use rustc_hir::def_id::{DefId, LocalDefId};
1212
use rustc_hir::lang_items::LangItem;
13-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1413
use rustc_infer::infer::{self, RegionResolutionError, TyCtxtInferExt};
1514
use rustc_infer::traits::Obligation;
1615
use rustc_middle::ty::adjustment::CoerceUnsizedInfo;
@@ -346,8 +345,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
346345
}
347346

348347
// Finally, resolve all regions.
349-
let outlives_env = OutlivesEnvironment::new(param_env);
350-
res = res.and(ocx.resolve_regions_and_report_errors(impl_did, &outlives_env));
348+
res = res.and(ocx.resolve_regions_and_report_errors(impl_did, param_env, []));
351349
}
352350
res
353351
}
@@ -564,8 +562,7 @@ pub(crate) fn coerce_unsized_info<'tcx>(
564562
}
565563

566564
// Finally, resolve all regions.
567-
let outlives_env = OutlivesEnvironment::new(param_env);
568-
let _ = ocx.resolve_regions_and_report_errors(impl_did, &outlives_env);
565+
let _ = ocx.resolve_regions_and_report_errors(impl_did, param_env, []);
569566

570567
Ok(CoerceUnsizedInfo { custom_kind: kind })
571568
}

Diff for: compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
use rustc_data_structures::fx::FxHashSet;
6969
use rustc_hir::def_id::{DefId, LocalDefId};
7070
use rustc_infer::infer::TyCtxtInferExt;
71-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
7271
use rustc_infer::traits::ObligationCause;
7372
use rustc_infer::traits::specialization_graph::Node;
7473
use rustc_middle::ty::trait_def::TraitSpecializationKind;
@@ -77,7 +76,6 @@ use rustc_middle::ty::{
7776
};
7877
use rustc_span::{ErrorGuaranteed, Span};
7978
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
80-
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
8179
use rustc_trait_selection::traits::{self, ObligationCtxt, translate_args_with_cause, wf};
8280
use tracing::{debug, instrument};
8381

@@ -176,7 +174,6 @@ fn get_impl_args(
176174
let ocx = ObligationCtxt::new_with_diagnostics(infcx);
177175
let param_env = tcx.param_env(impl1_def_id);
178176
let impl1_span = tcx.def_span(impl1_def_id);
179-
let assumed_wf_types = ocx.assumed_wf_types_and_report_errors(param_env, impl1_def_id)?;
180177

181178
let impl1_args = GenericArgs::identity_for_item(tcx, impl1_def_id);
182179
let impl2_args = translate_args_with_cause(
@@ -194,9 +191,8 @@ fn get_impl_args(
194191
return Err(guar);
195192
}
196193

197-
let implied_bounds = infcx.implied_bounds_tys(param_env, impl1_def_id, &assumed_wf_types);
198-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
199-
let _ = ocx.resolve_regions_and_report_errors(impl1_def_id, &outlives_env);
194+
let assumed_wf_types = ocx.assumed_wf_types_and_report_errors(param_env, impl1_def_id)?;
195+
let _ = ocx.resolve_regions_and_report_errors(impl1_def_id, param_env, assumed_wf_types);
200196
let Ok(impl2_args) = infcx.fully_resolve(impl2_args) else {
201197
let span = tcx.def_span(impl1_def_id);
202198
let guar = tcx.dcx().emit_err(GenericArgsOnOverriddenImpl { span });

0 commit comments

Comments
 (0)