Skip to content

Commit 48b7e38

Browse files
Move outlives env computation into methods
1 parent 2b8930c commit 48b7e38

File tree

8 files changed

+71
-49
lines changed

8 files changed

+71
-49
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(parent, param_env, 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/wfcheck.rs

+10-7
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,13 @@ 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 = infcx.implied_bounds_tys_compat(
130+
let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
131+
&infcx,
132132
body_def_id,
133133
param_env,
134134
assumed_wf_types.iter().copied(),
135135
false,
136136
);
137-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
138137

139138
lint_redundant_lifetimes(tcx, body_def_id, &outlives_env);
140139

@@ -176,9 +175,13 @@ where
176175
// but that does result in slightly more work when this option is set and
177176
// just obscures what we mean here anyways. Let's just be explicit.
178177
if is_bevy && !infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat {
179-
let implied_bounds =
180-
infcx_compat.implied_bounds_tys_compat(body_def_id, param_env, assumed_wf_types, true);
181-
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
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+
);
182185
let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
183186
if errors_compat.is_empty() {
184187
Ok(())

Diff for: compiler/rustc_infer/src/infer/outlives/env.rs

-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ pub struct OutlivesEnvironment<'tcx> {
5959
pub type RegionBoundPairs<'tcx> = FxIndexSet<ty::OutlivesPredicate<'tcx, GenericKind<'tcx>>>;
6060

6161
impl<'tcx> OutlivesEnvironment<'tcx> {
62-
/// Create a new `OutlivesEnvironment` without extra outlives bounds.
63-
#[inline]
64-
pub fn new(param_env: ty::ParamEnv<'tcx>) -> Self {
65-
Self::with_bounds(param_env, vec![])
66-
}
67-
6862
/// Create a new `OutlivesEnvironment` with extra outlives bounds.
6963
pub fn with_bounds(
7064
param_env: ty::ParamEnv<'tcx>,

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use rustc_span::{Span, Symbol};
2525
use rustc_trait_selection::errors::{
2626
AddPreciseCapturingForOvercapture, impl_trait_overcapture_suggestion,
2727
};
28+
use rustc_trait_selection::regions::OutlivesEnvironmentBuildExt;
2829
use rustc_trait_selection::traits::ObligationCtxt;
29-
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt;
3030

3131
use crate::{LateContext, LateLintPass, fluent_generated as fluent};
3232

@@ -190,9 +190,7 @@ fn check_fn(tcx: TyCtxt<'_>, parent_def_id: LocalDefId) {
190190
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
191191
let ocx = ObligationCtxt::new(&infcx);
192192
let assumed_wf_tys = ocx.assumed_wf_types(param_env, parent_def_id).unwrap_or_default();
193-
let implied_bounds =
194-
infcx.implied_bounds_tys_compat(parent_def_id, param_env, assumed_wf_tys, false);
195-
OutlivesEnvironment::with_bounds(param_env, implied_bounds)
193+
OutlivesEnvironment::new(&infcx, parent_def_id, param_env, assumed_wf_tys)
196194
}),
197195
});
198196
}

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

+44-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,46 @@ use rustc_middle::ty::{self, Ty};
99
use crate::traits::ScrubbedTraitError;
1010
use crate::traits::outlives_bounds::InferCtxtExt;
1111

12+
#[extension(pub trait OutlivesEnvironmentBuildExt<'tcx>)]
13+
impl<'tcx> OutlivesEnvironment<'tcx> {
14+
fn new(
15+
infcx: &InferCtxt<'tcx>,
16+
body_id: LocalDefId,
17+
param_env: ty::ParamEnv<'tcx>,
18+
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
19+
) -> Self {
20+
Self::new_with_implied_bounds_compat(
21+
infcx,
22+
body_id,
23+
param_env,
24+
assumed_wf_tys,
25+
!infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat,
26+
)
27+
}
28+
29+
fn new_with_implied_bounds_compat(
30+
infcx: &InferCtxt<'tcx>,
31+
body_id: LocalDefId,
32+
param_env: ty::ParamEnv<'tcx>,
33+
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
34+
implied_bounds_compat: bool,
35+
) -> Self {
36+
// FIXME: This needs to be modified so that we normalize the known type
37+
// outlives obligations then elaborate them into their region/type components.
38+
// Otherwise, `<W<'a> as Mirror>::Assoc: 'b` will not imply `'a: 'b` even
39+
// if we can normalize `'a`.
40+
OutlivesEnvironment::with_bounds(
41+
param_env,
42+
infcx.implied_bounds_tys_with_compat(
43+
body_id,
44+
param_env,
45+
assumed_wf_tys,
46+
implied_bounds_compat,
47+
),
48+
)
49+
}
50+
}
51+
1252
#[extension(pub trait InferCtxtRegionExt<'tcx>)]
1353
impl<'tcx> InferCtxt<'tcx> {
1454
/// Resolve regions, using the deep normalizer to normalize any type-outlives
@@ -23,9 +63,11 @@ impl<'tcx> InferCtxt<'tcx> {
2363
param_env: ty::ParamEnv<'tcx>,
2464
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
2565
) -> Vec<RegionResolutionError<'tcx>> {
26-
self.resolve_regions_with_outlives_env(&OutlivesEnvironment::with_bounds(
66+
self.resolve_regions_with_outlives_env(&OutlivesEnvironment::new(
67+
self,
68+
body_id,
2769
param_env,
28-
self.implied_bounds_tys(body_id, param_env, assumed_wf_tys),
70+
assumed_wf_tys,
2971
))
3072
}
3173

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ use std::iter;
66

77
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
88
use rustc_data_structures::unord::UnordSet;
9+
use rustc_hir::def_id::CRATE_DEF_ID;
910
use rustc_infer::infer::DefineOpaqueTypes;
1011
use rustc_middle::ty::{Region, RegionVid};
1112
use tracing::debug;
1213

1314
use super::*;
1415
use crate::errors::UnableToConstructConstantValue;
1516
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
17+
use crate::regions::OutlivesEnvironmentBuildExt;
1618
use crate::traits::project::ProjectAndUnifyResult;
1719

1820
// FIXME(twk): this is obviously not nice to duplicate like that
@@ -158,7 +160,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
158160
panic!("Unable to fulfill trait {trait_did:?} for '{ty:?}': {errors:?}");
159161
}
160162

161-
let outlives_env = OutlivesEnvironment::new(full_env);
163+
let outlives_env = OutlivesEnvironment::new(&infcx, CRATE_DEF_ID, full_env, []);
162164
let _ = infcx.process_registered_region_obligations(&outlives_env, |ty, _| Ok(ty));
163165

164166
let region_data = infcx.inner.borrow_mut().unwrap_region_constraints().data().clone();

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

+3-18
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ fn implied_outlives_bounds<'a, 'tcx>(
108108

109109
#[extension(pub trait InferCtxtExt<'tcx>)]
110110
impl<'tcx> InferCtxt<'tcx> {
111-
/// Do *NOT* call this directly.
112-
fn implied_bounds_tys_compat<Tys: IntoIterator<Item = Ty<'tcx>>>(
111+
/// Do *NOT* call this directly. You probably want to construct a `OutlivesEnvironment`
112+
/// instead if you're interested in the implied bounds for a given signature.
113+
fn implied_bounds_tys_with_compat<Tys: IntoIterator<Item = Ty<'tcx>>>(
113114
&self,
114115
body_id: LocalDefId,
115116
param_env: ParamEnv<'tcx>,
@@ -119,20 +120,4 @@ impl<'tcx> InferCtxt<'tcx> {
119120
tys.into_iter()
120121
.flat_map(move |ty| implied_outlives_bounds(self, param_env, body_id, ty, compat))
121122
}
122-
123-
/// If `-Z no-implied-bounds-compat` is set, calls `implied_bounds_tys_compat`
124-
/// with `compat` set to `true`, otherwise `false`.
125-
fn implied_bounds_tys(
126-
&self,
127-
body_id: LocalDefId,
128-
param_env: ParamEnv<'tcx>,
129-
tys: impl IntoIterator<Item = Ty<'tcx>>,
130-
) -> impl Iterator<Item = OutlivesBound<'tcx>> {
131-
self.implied_bounds_tys_compat(
132-
body_id,
133-
param_env,
134-
tys,
135-
!self.tcx.sess.opts.unstable_opts.no_implied_bounds_compat,
136-
)
137-
}
138123
}

Diff for: src/doc/rustc-dev-guide/src/traits/implied-bounds.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ requirements of impls and functions as explicit predicates.
4040
### using implicit implied bounds as assumptions
4141

4242
These bounds are not added to the `ParamEnv` of the affected item itself. For lexical
43-
region resolution they are added using [`fn OutlivesEnvironment::with_bounds`].
43+
region resolution they are added using [`fn OutlivesEnvironment::new`].
4444
Similarly, during MIR borrowck we add them using
4545
[`fn UniversalRegionRelationsBuilder::add_implied_bounds`].
4646

@@ -55,7 +55,7 @@ The assumed outlives constraints for implicit bounds are computed using the
5555
MIR borrowck adds the outlives constraints for both the normalized and unnormalized types,
5656
lexical region resolution [only uses the unnormalized types][notnorm].
5757

58-
[`fn OutlivesEnvironment::with_bounds`]: https://github.com/rust-lang/rust/blob/5b8bc568d28b2e922290c9a966b3231d0ce9398b/compiler/rustc_infer/src/infer/outlives/env.rs#L90-L97
58+
[`fn OutlivesEnvironment::new`]: TODO
5959
[`fn UniversalRegionRelationsBuilder::add_implied_bounds`]: https://github.com/rust-lang/rust/blob/5b8bc568d28b2e922290c9a966b3231d0ce9398b/compiler/rustc_borrowck/src/type_check/free_region_relations.rs#L316
6060
[mir]: https://github.com/rust-lang/rust/blob/91cae1dcdcf1a31bd8a92e4a63793d65cfe289bb/compiler/rustc_borrowck/src/type_check/free_region_relations.rs#L258-L332
6161
[`fn assumed_wf_types`]: https://github.com/rust-lang/rust/blob/5b8bc568d28b2e922290c9a966b3231d0ce9398b/compiler/rustc_ty_utils/src/implied_bounds.rs#L21

0 commit comments

Comments
 (0)