Skip to content

Commit 2b8930c

Browse files
Consolidate OutlivesEnv construction with resolve_regions
1 parent fdd1a3b commit 2b8930c

File tree

16 files changed

+75
-121
lines changed

16 files changed

+75
-121
lines changed

Diff for: compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'tcx> LazyOpaqueTyEnv<'tcx> {
439439
tcx.dcx().span_delayed_bug(tcx.def_span(def_id), "error getting implied bounds");
440440
Default::default()
441441
});
442-
let implied_bounds = infcx.implied_bounds_tys(param_env, parent, &wf_tys);
442+
let implied_bounds = infcx.implied_bounds_tys(parent, param_env, wf_tys);
443443
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
444444

445445
let mut seen = vec![tcx.lifetimes.re_static];

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

+10-11
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,17 @@ where
128128
let infcx_compat = infcx.fork();
129129

130130
// 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);
131+
let implied_bounds = infcx.implied_bounds_tys_compat(
132+
body_def_id,
133+
param_env,
134+
assumed_wf_types.iter().copied(),
135+
false,
136+
);
133137
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
134138

135139
lint_redundant_lifetimes(tcx, body_def_id, &outlives_env);
136140

137-
let errors = infcx.resolve_regions(&outlives_env);
141+
let errors = infcx.resolve_regions_with_outlives_env(&outlives_env);
138142
if errors.is_empty() {
139143
return Ok(());
140144
}
@@ -173,9 +177,9 @@ where
173177
// just obscures what we mean here anyways. Let's just be explicit.
174178
if is_bevy && !infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat {
175179
let implied_bounds =
176-
infcx_compat.implied_bounds_tys_compat(param_env, body_def_id, &assumed_wf_types, true);
180+
infcx_compat.implied_bounds_tys_compat(body_def_id, param_env, assumed_wf_types, true);
177181
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
178-
let errors_compat = infcx_compat.resolve_regions(&outlives_env);
182+
let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
179183
if errors_compat.is_empty() {
180184
Ok(())
181185
} else {
@@ -769,12 +773,7 @@ fn test_region_obligations<'tcx>(
769773

770774
add_constraints(&infcx);
771775

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);
776+
let errors = infcx.resolve_regions(id, param_env, wf_tys.iter().copied());
778777
debug!(?errors, "errors");
779778

780779
// 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 });

Diff for: compiler/rustc_lint/src/impl_trait_overcaptures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn check_fn(tcx: TyCtxt<'_>, parent_def_id: LocalDefId) {
191191
let ocx = ObligationCtxt::new(&infcx);
192192
let assumed_wf_tys = ocx.assumed_wf_types(param_env, parent_def_id).unwrap_or_default();
193193
let implied_bounds =
194-
infcx.implied_bounds_tys_compat(param_env, parent_def_id, &assumed_wf_tys, false);
194+
infcx.implied_bounds_tys_compat(parent_def_id, param_env, assumed_wf_tys, false);
195195
OutlivesEnvironment::with_bounds(param_env, implied_bounds)
196196
}),
197197
});

Diff for: compiler/rustc_trait_selection/src/regions.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use rustc_hir::def_id::LocalDefId;
12
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
23
use rustc_infer::infer::{InferCtxt, RegionResolutionError};
34
use rustc_macros::extension;
45
use rustc_middle::traits::ObligationCause;
56
use rustc_middle::traits::query::NoSolution;
7+
use rustc_middle::ty::{self, Ty};
68

79
use crate::traits::ScrubbedTraitError;
10+
use crate::traits::outlives_bounds::InferCtxtExt;
811

912
#[extension(pub trait InferCtxtRegionExt<'tcx>)]
1013
impl<'tcx> InferCtxt<'tcx> {
@@ -15,10 +18,23 @@ impl<'tcx> InferCtxt<'tcx> {
1518
/// Prefer this method over `resolve_regions_with_normalize`, unless you are
1619
/// doing something specific for normalization.
1720
fn resolve_regions(
21+
&self,
22+
body_id: LocalDefId,
23+
param_env: ty::ParamEnv<'tcx>,
24+
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
25+
) -> Vec<RegionResolutionError<'tcx>> {
26+
self.resolve_regions_with_outlives_env(&OutlivesEnvironment::with_bounds(
27+
param_env,
28+
self.implied_bounds_tys(body_id, param_env, assumed_wf_tys),
29+
))
30+
}
31+
32+
/// Don't call this directly unless you know what you're doing.
33+
fn resolve_regions_with_outlives_env(
1834
&self,
1935
outlives_env: &OutlivesEnvironment<'tcx>,
2036
) -> Vec<RegionResolutionError<'tcx>> {
21-
self.resolve_regions_with_normalize(outlives_env, |ty, origin| {
37+
self.resolve_regions_with_normalize(&outlives_env, |ty, origin| {
2238
let ty = self.resolve_vars_if_possible(ty);
2339

2440
if self.next_trait_solver() {

Diff for: compiler/rustc_trait_selection/src/traits/coherence.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::fmt::Debug;
99
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
1010
use rustc_errors::{Diag, EmissionGuarantee};
1111
use rustc_hir::def::DefKind;
12-
use rustc_hir::def_id::DefId;
12+
use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
1313
use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, TyCtxtInferExt};
1414
use rustc_infer::traits::PredicateObligations;
1515
use rustc_middle::bug;
@@ -27,7 +27,6 @@ use tracing::{debug, instrument, warn};
2727
use super::ObligationCtxt;
2828
use crate::error_reporting::traits::suggest_new_overflow_limit;
2929
use crate::infer::InferOk;
30-
use crate::infer::outlives::env::OutlivesEnvironment;
3130
use crate::solve::inspect::{InspectGoal, ProofTreeInferCtxtExt, ProofTreeVisitor};
3231
use crate::solve::{SolverDelegate, deeply_normalize_for_diagnostics, inspect};
3332
use crate::traits::query::evaluate_obligation::InferCtxtExt;
@@ -596,8 +595,7 @@ fn try_prove_negated_where_clause<'tcx>(
596595
// FIXME: We could use the assumed_wf_types from both impls, I think,
597596
// if that wasn't implemented just for LocalDefId, and we'd need to do
598597
// the normalization ourselves since this is totally fallible...
599-
let outlives_env = OutlivesEnvironment::new(param_env);
600-
let errors = ocx.resolve_regions(&outlives_env);
598+
let errors = ocx.resolve_regions(CRATE_DEF_ID, param_env, []);
601599
if !errors.is_empty() {
602600
return false;
603601
}

Diff for: compiler/rustc_trait_selection/src/traits/engine.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_infer::infer::at::ToTrace;
88
use rustc_infer::infer::canonical::{
99
Canonical, CanonicalQueryResponse, CanonicalVarValues, QueryResponse,
1010
};
11-
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1211
use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk, RegionResolutionError, TypeTrace};
1312
use rustc_infer::traits::PredicateObligations;
1413
use rustc_macros::extension;
@@ -217,14 +216,15 @@ where
217216
/// will result in region constraints getting ignored.
218217
pub fn resolve_regions_and_report_errors(
219218
self,
220-
generic_param_scope: LocalDefId,
221-
outlives_env: &OutlivesEnvironment<'tcx>,
219+
body_id: LocalDefId,
220+
param_env: ty::ParamEnv<'tcx>,
221+
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
222222
) -> Result<(), ErrorGuaranteed> {
223-
let errors = self.infcx.resolve_regions(outlives_env);
223+
let errors = self.infcx.resolve_regions(body_id, param_env, assumed_wf_tys);
224224
if errors.is_empty() {
225225
Ok(())
226226
} else {
227-
Err(self.infcx.err_ctxt().report_region_errors(generic_param_scope, &errors))
227+
Err(self.infcx.err_ctxt().report_region_errors(body_id, &errors))
228228
}
229229
}
230230

@@ -235,9 +235,11 @@ where
235235
#[must_use]
236236
pub fn resolve_regions(
237237
self,
238-
outlives_env: &OutlivesEnvironment<'tcx>,
238+
body_id: LocalDefId,
239+
param_env: ty::ParamEnv<'tcx>,
240+
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
239241
) -> Vec<RegionResolutionError<'tcx>> {
240-
self.infcx.resolve_regions(outlives_env)
242+
self.infcx.resolve_regions(body_id, param_env, assumed_wf_tys)
241243
}
242244
}
243245

0 commit comments

Comments
 (0)