diff options
| author | Michael Goulet <michael@errs.io> | 2025-01-25 04:26:32 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2025-01-28 18:55:03 +0000 |
| commit | 48b7e38c0607e856dbbb932e60ecc85e45a1427d (patch) | |
| tree | 8940572ec00be905372772a0ce230cacab287750 /compiler/rustc_trait_selection | |
| parent | 2b8930c71c8040d338f99e1b1be6f1056edd6638 (diff) | |
| download | rust-48b7e38c0607e856dbbb932e60ecc85e45a1427d.tar.gz rust-48b7e38c0607e856dbbb932e60ecc85e45a1427d.zip | |
Move outlives env computation into methods
Diffstat (limited to 'compiler/rustc_trait_selection')
| -rw-r--r-- | compiler/rustc_trait_selection/src/regions.rs | 46 | ||||
| -rw-r--r-- | compiler/rustc_trait_selection/src/traits/auto_trait.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_trait_selection/src/traits/outlives_bounds.rs | 21 |
3 files changed, 50 insertions, 21 deletions
diff --git a/compiler/rustc_trait_selection/src/regions.rs b/compiler/rustc_trait_selection/src/regions.rs index 1bae6c80cfa..fc9fa44b4c6 100644 --- a/compiler/rustc_trait_selection/src/regions.rs +++ b/compiler/rustc_trait_selection/src/regions.rs @@ -9,6 +9,46 @@ use rustc_middle::ty::{self, Ty}; use crate::traits::ScrubbedTraitError; use crate::traits::outlives_bounds::InferCtxtExt; +#[extension(pub trait OutlivesEnvironmentBuildExt<'tcx>)] +impl<'tcx> OutlivesEnvironment<'tcx> { + fn new( + infcx: &InferCtxt<'tcx>, + body_id: LocalDefId, + param_env: ty::ParamEnv<'tcx>, + assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>, + ) -> Self { + Self::new_with_implied_bounds_compat( + infcx, + body_id, + param_env, + assumed_wf_tys, + !infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat, + ) + } + + fn new_with_implied_bounds_compat( + infcx: &InferCtxt<'tcx>, + body_id: LocalDefId, + param_env: ty::ParamEnv<'tcx>, + assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>, + implied_bounds_compat: bool, + ) -> Self { + // FIXME: This needs to be modified so that we normalize the known type + // outlives obligations then elaborate them into their region/type components. + // Otherwise, `<W<'a> as Mirror>::Assoc: 'b` will not imply `'a: 'b` even + // if we can normalize `'a`. + OutlivesEnvironment::with_bounds( + param_env, + infcx.implied_bounds_tys_with_compat( + body_id, + param_env, + assumed_wf_tys, + implied_bounds_compat, + ), + ) + } +} + #[extension(pub trait InferCtxtRegionExt<'tcx>)] impl<'tcx> InferCtxt<'tcx> { /// Resolve regions, using the deep normalizer to normalize any type-outlives @@ -23,9 +63,11 @@ impl<'tcx> InferCtxt<'tcx> { param_env: ty::ParamEnv<'tcx>, assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>, ) -> Vec<RegionResolutionError<'tcx>> { - self.resolve_regions_with_outlives_env(&OutlivesEnvironment::with_bounds( + self.resolve_regions_with_outlives_env(&OutlivesEnvironment::new( + self, + body_id, param_env, - self.implied_bounds_tys(body_id, param_env, assumed_wf_tys), + assumed_wf_tys, )) } diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index 9a53e8a5d51..1fca2f4da7e 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -6,6 +6,7 @@ use std::iter; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry}; use rustc_data_structures::unord::UnordSet; +use rustc_hir::def_id::CRATE_DEF_ID; use rustc_infer::infer::DefineOpaqueTypes; use rustc_middle::ty::{Region, RegionVid}; use tracing::debug; @@ -13,6 +14,7 @@ use tracing::debug; use super::*; use crate::errors::UnableToConstructConstantValue; use crate::infer::region_constraints::{Constraint, RegionConstraintData}; +use crate::regions::OutlivesEnvironmentBuildExt; use crate::traits::project::ProjectAndUnifyResult; // FIXME(twk): this is obviously not nice to duplicate like that @@ -158,7 +160,7 @@ impl<'tcx> AutoTraitFinder<'tcx> { panic!("Unable to fulfill trait {trait_did:?} for '{ty:?}': {errors:?}"); } - let outlives_env = OutlivesEnvironment::new(full_env); + let outlives_env = OutlivesEnvironment::new(&infcx, CRATE_DEF_ID, full_env, []); let _ = infcx.process_registered_region_obligations(&outlives_env, |ty, _| Ok(ty)); let region_data = infcx.inner.borrow_mut().unwrap_region_constraints().data().clone(); diff --git a/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs b/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs index eecc499f384..18932695807 100644 --- a/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs +++ b/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs @@ -108,8 +108,9 @@ fn implied_outlives_bounds<'a, 'tcx>( #[extension(pub trait InferCtxtExt<'tcx>)] impl<'tcx> InferCtxt<'tcx> { - /// Do *NOT* call this directly. - fn implied_bounds_tys_compat<Tys: IntoIterator<Item = Ty<'tcx>>>( + /// Do *NOT* call this directly. You probably want to construct a `OutlivesEnvironment` + /// instead if you're interested in the implied bounds for a given signature. + fn implied_bounds_tys_with_compat<Tys: IntoIterator<Item = Ty<'tcx>>>( &self, body_id: LocalDefId, param_env: ParamEnv<'tcx>, @@ -119,20 +120,4 @@ impl<'tcx> InferCtxt<'tcx> { tys.into_iter() .flat_map(move |ty| implied_outlives_bounds(self, param_env, body_id, ty, compat)) } - - /// If `-Z no-implied-bounds-compat` is set, calls `implied_bounds_tys_compat` - /// with `compat` set to `true`, otherwise `false`. - fn implied_bounds_tys( - &self, - body_id: LocalDefId, - param_env: ParamEnv<'tcx>, - tys: impl IntoIterator<Item = Ty<'tcx>>, - ) -> impl Iterator<Item = OutlivesBound<'tcx>> { - self.implied_bounds_tys_compat( - body_id, - param_env, - tys, - !self.tcx.sess.opts.unstable_opts.no_implied_bounds_compat, - ) - } } |
