From 5e606c0bde35564df43a82d7c6ae32ad700c009c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 10 May 2024 14:27:48 -0400 Subject: [PATCH] Lift `Lift` --- compiler/rustc_macros/src/lift.rs | 2 +- compiler/rustc_middle/src/macros.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 32 ++++--------------- compiler/rustc_middle/src/ty/generic_args.rs | 2 +- .../rustc_middle/src/ty/structural_impls.rs | 4 +-- .../src/traits/query/type_op/normalize.rs | 4 ++- compiler/rustc_traits/src/type_op.rs | 2 +- compiler/rustc_type_ir/src/lib.rs | 2 ++ 8 files changed, 17 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_macros/src/lift.rs b/compiler/rustc_macros/src/lift.rs index f7a84ba1510..d41ceb29816 100644 --- a/compiler/rustc_macros/src/lift.rs +++ b/compiler/rustc_macros/src/lift.rs @@ -41,7 +41,7 @@ pub fn lift_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStre s.add_impl_generic(newtcx); s.bound_impl( - quote!(::rustc_middle::ty::Lift<'__lifted>), + quote!(::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>), quote! { type Lifted = #lifted; diff --git a/compiler/rustc_middle/src/macros.rs b/compiler/rustc_middle/src/macros.rs index f70ef51107f..817ac594627 100644 --- a/compiler/rustc_middle/src/macros.rs +++ b/compiler/rustc_middle/src/macros.rs @@ -57,7 +57,7 @@ macro_rules! span_bug { macro_rules! TrivialLiftImpls { ($($ty:ty),+ $(,)?) => { $( - impl<'tcx> $crate::ty::Lift<'tcx> for $ty { + impl<'tcx> $crate::ty::Lift<$crate::ty::TyCtxt<'tcx>> for $ty { type Lifted = Self; fn lift_to_tcx(self, _: $crate::ty::TyCtxt<'tcx>) -> Option { Some(self) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 1a55e492688..ed15f61c735 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -4,6 +4,8 @@ pub mod tls; +pub use rustc_type_ir::lift::Lift; + use crate::arena::Arena; use crate::dep_graph::{DepGraph, DepKindStruct}; use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarInfo, CanonicalVarInfos}; @@ -917,7 +919,7 @@ impl<'tcx> TyCtxt<'tcx> { ) } - pub fn lift>(self, value: T) -> Option { + pub fn lift>>(self, value: T) -> Option { value.lift_to_tcx(self) } @@ -1524,31 +1526,9 @@ impl<'tcx> TyCtxt<'tcx> { } } -/// A trait implemented for all `X<'a>` types that can be safely and -/// efficiently converted to `X<'tcx>` as long as they are part of the -/// provided `TyCtxt<'tcx>`. -/// This can be done, for example, for `Ty<'tcx>` or `GenericArgsRef<'tcx>` -/// by looking them up in their respective interners. -/// -/// However, this is still not the best implementation as it does -/// need to compare the components, even for interned values. -/// It would be more efficient if `TypedArena` provided a way to -/// determine whether the address is in the allocated range. -/// -/// `None` is returned if the value or one of the components is not part -/// of the provided context. -/// For `Ty`, `None` can be returned if either the type interner doesn't -/// contain the `TyKind` key or if the address of the interned -/// pointer differs. The latter case is possible if a primitive type, -/// e.g., `()` or `u8`, was interned in a different context. -pub trait Lift<'tcx>: fmt::Debug { - type Lifted: fmt::Debug + 'tcx; - fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option; -} - macro_rules! nop_lift { ($set:ident; $ty:ty => $lifted:ty) => { - impl<'a, 'tcx> Lift<'tcx> for $ty { + impl<'a, 'tcx> Lift> for $ty { type Lifted = $lifted; fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option { // Assert that the set has the right type. @@ -1583,7 +1563,7 @@ macro_rules! nop_lift { macro_rules! nop_list_lift { ($set:ident; $ty:ty => $lifted:ty) => { - impl<'a, 'tcx> Lift<'tcx> for &'a List<$ty> { + impl<'a, 'tcx> Lift> for &'a List<$ty> { type Lifted = &'tcx List<$lifted>; fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option { // Assert that the set has the right type. @@ -1621,7 +1601,7 @@ nop_list_lift! {args; GenericArg<'a> => GenericArg<'tcx>} macro_rules! nop_slice_lift { ($ty:ty => $lifted:ty) => { - impl<'a, 'tcx> Lift<'tcx> for &'a [$ty] { + impl<'a, 'tcx> Lift> for &'a [$ty] { type Lifted = &'tcx [$lifted]; fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option { if self.is_empty() { diff --git a/compiler/rustc_middle/src/ty/generic_args.rs b/compiler/rustc_middle/src/ty/generic_args.rs index 4a613083ef7..b9dc283e2eb 100644 --- a/compiler/rustc_middle/src/ty/generic_args.rs +++ b/compiler/rustc_middle/src/ty/generic_args.rs @@ -205,7 +205,7 @@ impl<'tcx> GenericArg<'tcx> { } } -impl<'a, 'tcx> Lift<'tcx> for GenericArg<'a> { +impl<'a, 'tcx> Lift> for GenericArg<'a> { type Lifted = GenericArg<'tcx>; fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option { diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 14a77d4b37e..78a70717a44 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -478,7 +478,7 @@ TrivialTypeTraversalAndLiftImpls! { /////////////////////////////////////////////////////////////////////////// // Lift implementations -impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option { +impl<'tcx, T: Lift>> Lift> for Option { type Lifted = Option; fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option { Some(match self { @@ -488,7 +488,7 @@ impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option { } } -impl<'a, 'tcx> Lift<'tcx> for Term<'a> { +impl<'a, 'tcx> Lift> for Term<'a> { type Lifted = ty::Term<'tcx>; fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option { Some( diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs index 279d96dec72..e9948bf1f71 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs @@ -34,7 +34,9 @@ where } } -pub trait Normalizable<'tcx>: fmt::Debug + TypeFoldable> + Lift<'tcx> + Copy { +pub trait Normalizable<'tcx>: + fmt::Debug + TypeFoldable> + Lift> + Copy +{ fn type_op_method( tcx: TyCtxt<'tcx>, canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize>>, diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs index 01b9a5640b8..85c98bfd5dc 100644 --- a/compiler/rustc_traits/src/type_op.rs +++ b/compiler/rustc_traits/src/type_op.rs @@ -54,7 +54,7 @@ fn type_op_normalize<'tcx, T>( key: ParamEnvAnd<'tcx, Normalize>, ) -> Result where - T: fmt::Debug + TypeFoldable> + Lift<'tcx>, + T: fmt::Debug + TypeFoldable>, { let (param_env, Normalize { value }) = key.into_parts(); let Normalized { value, obligations } = diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index ef7437c2542..a978a30e95f 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -26,6 +26,7 @@ pub mod fold; pub mod new; pub mod ty_info; pub mod ty_kind; +pub mod lift; #[macro_use] mod macros; @@ -57,6 +58,7 @@ pub use DynKind::*; pub use InferTy::*; pub use RegionKind::*; pub use TyKind::*; +pub use lift::*; rustc_index::newtype_index! { /// A [De Bruijn index][dbi] is a standard means of representing