Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/next_solver/def_id.rs')
| -rw-r--r-- | crates/hir-ty/src/next_solver/def_id.rs | 303 |
1 files changed, 223 insertions, 80 deletions
diff --git a/crates/hir-ty/src/next_solver/def_id.rs b/crates/hir-ty/src/next_solver/def_id.rs index ce66dbc77c..f7d831d7fc 100644 --- a/crates/hir-ty/src/next_solver/def_id.rs +++ b/crates/hir-ty/src/next_solver/def_id.rs @@ -10,6 +10,7 @@ use hir_def::{ }, }; use rustc_type_ir::inherent; +use salsa::Update; use stdx::impl_from; use crate::{ @@ -28,26 +29,26 @@ pub enum Ctor { Enum(EnumVariantId), } -#[derive(PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash)] -pub enum SolverDefId { +#[derive(PartialOrd, Ord, Clone, Copy, PartialEq, Eq, Hash, Update)] +pub enum SolverDefId<'db> { AdtId(AdtId), ConstId(ConstId), FunctionId(FunctionId), ImplId(ImplId), BuiltinDeriveImplId(BuiltinDeriveImplId), StaticId(StaticId), - AnonConstId(AnonConstId), + AnonConstId(AnonConstId<'db>), TraitId(TraitId), TypeAliasId(TypeAliasId), - InternedClosureId(InternedClosureId), - InternedCoroutineId(InternedCoroutineId), - InternedCoroutineClosureId(InternedCoroutineClosureId), + InternedClosureId(InternedClosureId<'db>), + InternedCoroutineId(InternedCoroutineId<'db>), + InternedCoroutineClosureId(InternedCoroutineClosureId<'db>), InternedOpaqueTyId(InternedOpaqueTyId), EnumVariantId(EnumVariantId), Ctor(Ctor), } -impl std::fmt::Debug for SolverDefId { +impl std::fmt::Debug for SolverDefId<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let interner = DbInterner::conjure(); let db = interner.db; @@ -122,41 +123,34 @@ impl std::fmt::Debug for SolverDefId { } impl_from!( + impl<'db> AdtId(StructId, EnumId, UnionId), ConstId, FunctionId, ImplId, BuiltinDeriveImplId, StaticId, - AnonConstId, + AnonConstId<'db>, TraitId, TypeAliasId, - InternedClosureId, - InternedCoroutineId, - InternedCoroutineClosureId, + InternedClosureId<'db>, + InternedCoroutineId<'db>, + InternedCoroutineClosureId<'db>, InternedOpaqueTyId, EnumVariantId, Ctor - for SolverDefId + for SolverDefId<'db> ); -impl From<GenericDefId> for SolverDefId { - fn from(value: GenericDefId) -> Self { - match value { - GenericDefId::AdtId(adt_id) => SolverDefId::AdtId(adt_id), - GenericDefId::ConstId(const_id) => SolverDefId::ConstId(const_id), - GenericDefId::FunctionId(function_id) => SolverDefId::FunctionId(function_id), - GenericDefId::ImplId(impl_id) => SolverDefId::ImplId(impl_id), - GenericDefId::StaticId(static_id) => SolverDefId::StaticId(static_id), - GenericDefId::TraitId(trait_id) => SolverDefId::TraitId(trait_id), - GenericDefId::TypeAliasId(type_alias_id) => SolverDefId::TypeAliasId(type_alias_id), - } - } -} +impl_from!( + impl<'db> + GenericDefId { AdtId, ConstId, FunctionId, ImplId, StaticId, TraitId, TypeAliasId } + for SolverDefId<'db> +); -impl From<GeneralConstId> for SolverDefId { +impl<'db> From<GeneralConstId<'db>> for SolverDefId<'db> { #[inline] - fn from(value: GeneralConstId) -> Self { + fn from(value: GeneralConstId<'db>) -> Self { match value { GeneralConstId::ConstId(const_id) => SolverDefId::ConstId(const_id), GeneralConstId::StaticId(static_id) => SolverDefId::StaticId(static_id), @@ -165,7 +159,7 @@ impl From<GeneralConstId> for SolverDefId { } } -impl From<CallableDefId> for SolverDefId { +impl<'db> From<CallableDefId> for SolverDefId<'db> { #[inline] fn from(value: CallableDefId) -> Self { match value { @@ -176,7 +170,7 @@ impl From<CallableDefId> for SolverDefId { } } -impl From<DefWithBodyId> for SolverDefId { +impl<'db> From<DefWithBodyId> for SolverDefId<'db> { #[inline] fn from(value: DefWithBodyId) -> Self { match value { @@ -188,9 +182,9 @@ impl From<DefWithBodyId> for SolverDefId { } } -impl From<InferBodyId> for SolverDefId { +impl<'db> From<InferBodyId<'db>> for SolverDefId<'db> { #[inline] - fn from(value: InferBodyId) -> Self { + fn from(value: InferBodyId<'db>) -> Self { match value { InferBodyId::DefWithBodyId(id) => id.into(), InferBodyId::AnonConstId(id) => id.into(), @@ -198,7 +192,7 @@ impl From<InferBodyId> for SolverDefId { } } -impl From<VariantId> for SolverDefId { +impl<'db> From<VariantId> for SolverDefId<'db> { #[inline] fn from(value: VariantId) -> Self { match value { @@ -209,7 +203,7 @@ impl From<VariantId> for SolverDefId { } } -impl From<ExpressionStoreOwnerId> for SolverDefId { +impl<'db> From<ExpressionStoreOwnerId> for SolverDefId<'db> { #[inline] fn from(value: ExpressionStoreOwnerId) -> Self { match value { @@ -220,10 +214,10 @@ impl From<ExpressionStoreOwnerId> for SolverDefId { } } -impl TryFrom<SolverDefId> for AttrDefId { +impl TryFrom<SolverDefId<'_>> for AttrDefId { type Error = (); #[inline] - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> { match value { SolverDefId::AdtId(it) => Ok(it.into()), SolverDefId::ConstId(it) => Ok(it.into()), @@ -245,11 +239,11 @@ impl TryFrom<SolverDefId> for AttrDefId { } } -impl TryFrom<SolverDefId> for DefWithBodyId { +impl TryFrom<SolverDefId<'_>> for DefWithBodyId { type Error = (); #[inline] - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> { let id = match value { SolverDefId::ConstId(id) => id.into(), SolverDefId::FunctionId(id) => id.into(), @@ -271,11 +265,11 @@ impl TryFrom<SolverDefId> for DefWithBodyId { } } -impl TryFrom<SolverDefId> for InferBodyId { +impl<'db> TryFrom<SolverDefId<'db>> for InferBodyId<'db> { type Error = (); #[inline] - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { let id = match value { SolverDefId::ConstId(id) => id.into(), SolverDefId::FunctionId(id) => id.into(), @@ -297,10 +291,10 @@ impl TryFrom<SolverDefId> for InferBodyId { } } -impl TryFrom<SolverDefId> for GenericDefId { +impl TryFrom<SolverDefId<'_>> for GenericDefId { type Error = (); - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> { Ok(match value { SolverDefId::AdtId(adt_id) => GenericDefId::AdtId(adt_id), SolverDefId::ConstId(const_id) => GenericDefId::ConstId(const_id), @@ -321,8 +315,8 @@ impl TryFrom<SolverDefId> for GenericDefId { } } -impl<'db> inherent::DefId<DbInterner<'db>> for SolverDefId { - fn as_local(self) -> Option<SolverDefId> { +impl<'db> inherent::DefId<DbInterner<'db>> for SolverDefId<'db> { + fn as_local(self) -> Option<SolverDefId<'db>> { Some(self) } fn is_local(self) -> bool { @@ -332,17 +326,17 @@ impl<'db> inherent::DefId<DbInterner<'db>> for SolverDefId { macro_rules! declare_id_wrapper { ($name:ident, $wraps:ident) => { - declare_id_wrapper!($name, $wraps, SolverDefId); + declare_id_wrapper!($name, $wraps, SolverDefId<'db>); }; - ($name:ident, $wraps:ident, $local:ident) => { + ($name:ident, $wraps:ident, $local:ty) => { declare_id_wrapper!($name, $wraps, $local, no_try_from); - impl TryFrom<SolverDefId> for $name { + impl TryFrom<SolverDefId<'_>> for $name { type Error = (); #[inline] - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> { match value { SolverDefId::$wraps(it) => Ok(Self(it)), _ => Err(()), @@ -351,7 +345,7 @@ macro_rules! declare_id_wrapper { } }; - ($name:ident, $wraps:ident, $local:ident, no_try_from) => { + ($name:ident, $wraps:ident, $local:ty, no_try_from) => { #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct $name(pub $wraps); @@ -375,9 +369,9 @@ macro_rules! declare_id_wrapper { } } - impl From<$name> for SolverDefId { + impl<'db> From<$name> for SolverDefId<'db> { #[inline] - fn from(value: $name) -> SolverDefId { + fn from(value: $name) -> SolverDefId<'db> { value.0.into() } } @@ -395,9 +389,158 @@ macro_rules! declare_id_wrapper { declare_id_wrapper!(TraitIdWrapper, TraitId); declare_id_wrapper!(TypeAliasIdWrapper, TypeAliasId); -declare_id_wrapper!(ClosureIdWrapper, InternedClosureId); -declare_id_wrapper!(CoroutineIdWrapper, InternedCoroutineId); -declare_id_wrapper!(CoroutineClosureIdWrapper, InternedCoroutineClosureId); +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct ClosureIdWrapper<'db>(pub InternedClosureId<'db>); + +impl std::fmt::Debug for ClosureIdWrapper<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Debug::fmt(&SolverDefId::from(self.0), f) + } +} + +impl<'db> From<ClosureIdWrapper<'db>> for InternedClosureId<'db> { + #[inline] + fn from(value: ClosureIdWrapper<'db>) -> InternedClosureId<'db> { + value.0 + } +} + +impl<'db> From<InternedClosureId<'db>> for ClosureIdWrapper<'db> { + #[inline] + fn from(value: InternedClosureId<'db>) -> ClosureIdWrapper<'db> { + Self(value) + } +} + +impl<'db> From<ClosureIdWrapper<'db>> for SolverDefId<'db> { + #[inline] + fn from(value: ClosureIdWrapper<'db>) -> SolverDefId<'db> { + value.0.into() + } +} + +impl<'db> TryFrom<SolverDefId<'db>> for ClosureIdWrapper<'db> { + type Error = (); + + #[inline] + fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { + match value { + SolverDefId::InternedClosureId(it) => Ok(Self(it)), + _ => Err(()), + } + } +} + +impl<'db> inherent::DefId<DbInterner<'db>, SolverDefId<'db>> for ClosureIdWrapper<'db> { + fn as_local(self) -> Option<SolverDefId<'db>> { + Some(self.into()) + } + fn is_local(self) -> bool { + true + } +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct CoroutineIdWrapper<'db>(pub InternedCoroutineId<'db>); + +impl std::fmt::Debug for CoroutineIdWrapper<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Debug::fmt(&SolverDefId::from(self.0), f) + } +} + +impl<'db> From<CoroutineIdWrapper<'db>> for InternedCoroutineId<'db> { + #[inline] + fn from(value: CoroutineIdWrapper<'db>) -> InternedCoroutineId<'db> { + value.0 + } +} + +impl<'db> From<InternedCoroutineId<'db>> for CoroutineIdWrapper<'db> { + #[inline] + fn from(value: InternedCoroutineId<'db>) -> CoroutineIdWrapper<'db> { + Self(value) + } +} + +impl<'db> From<CoroutineIdWrapper<'db>> for SolverDefId<'db> { + #[inline] + fn from(value: CoroutineIdWrapper<'db>) -> SolverDefId<'db> { + value.0.into() + } +} + +impl<'db> TryFrom<SolverDefId<'db>> for CoroutineIdWrapper<'db> { + type Error = (); + + #[inline] + fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { + match value { + SolverDefId::InternedCoroutineId(it) => Ok(Self(it)), + _ => Err(()), + } + } +} + +impl<'db> inherent::DefId<DbInterner<'db>, SolverDefId<'db>> for CoroutineIdWrapper<'db> { + fn as_local(self) -> Option<SolverDefId<'db>> { + Some(self.into()) + } + fn is_local(self) -> bool { + true + } +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct CoroutineClosureIdWrapper<'db>(pub InternedCoroutineClosureId<'db>); + +impl std::fmt::Debug for CoroutineClosureIdWrapper<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Debug::fmt(&SolverDefId::from(self.0), f) + } +} + +impl<'db> From<CoroutineClosureIdWrapper<'db>> for InternedCoroutineClosureId<'db> { + #[inline] + fn from(value: CoroutineClosureIdWrapper<'db>) -> InternedCoroutineClosureId<'db> { + value.0 + } +} + +impl<'db> From<InternedCoroutineClosureId<'db>> for CoroutineClosureIdWrapper<'db> { + #[inline] + fn from(value: InternedCoroutineClosureId<'db>) -> CoroutineClosureIdWrapper<'db> { + Self(value) + } +} + +impl<'db> From<CoroutineClosureIdWrapper<'db>> for SolverDefId<'db> { + #[inline] + fn from(value: CoroutineClosureIdWrapper<'db>) -> SolverDefId<'db> { + value.0.into() + } +} + +impl<'db> TryFrom<SolverDefId<'db>> for CoroutineClosureIdWrapper<'db> { + type Error = (); + + #[inline] + fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { + match value { + SolverDefId::InternedCoroutineClosureId(it) => Ok(Self(it)), + _ => Err(()), + } + } +} + +impl<'db> inherent::DefId<DbInterner<'db>, SolverDefId<'db>> for CoroutineClosureIdWrapper<'db> { + fn as_local(self) -> Option<SolverDefId<'db>> { + Some(self.into()) + } + fn is_local(self) -> bool { + true + } +} declare_id_wrapper!(AdtIdWrapper, AdtId); declare_id_wrapper!(OpaqueTyIdWrapper, InternedOpaqueTyId, OpaqueTyIdWrapper); @@ -405,13 +548,13 @@ macro_rules! declare_ty_const_pair { ( $ty_id_name:ident, $const_id_name:ident, $term_id_name:ident ) => { declare_id_wrapper!($ty_id_name, TypeAliasId); declare_id_wrapper!($const_id_name, ConstId); - declare_id_wrapper!($term_id_name, TermId, SolverDefId, no_try_from); + declare_id_wrapper!($term_id_name, TermId, SolverDefId<'db>, no_try_from); - impl TryFrom<SolverDefId> for $term_id_name { + impl TryFrom<SolverDefId<'_>> for $term_id_name { type Error = (); #[inline] - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> { match value { SolverDefId::TypeAliasId(it) => Ok(Self(TermId::TypeAliasId(it))), SolverDefId::ConstId(it) => Ok(Self(TermId::ConstId(it))), @@ -454,7 +597,7 @@ macro_rules! declare_ty_const_pair { } } - impl From<$const_id_name> for GeneralConstIdWrapper { + impl<'db> From<$const_id_name> for GeneralConstIdWrapper<'db> { fn from(value: $const_id_name) -> Self { GeneralConstIdWrapper(GeneralConstId::ConstId(value.0)) } @@ -474,7 +617,7 @@ pub enum TermId { } impl_from!(TypeAliasId, ConstId for TermId); -impl From<TermId> for SolverDefId { +impl<'db> From<TermId> for SolverDefId<'db> { fn from(value: TermId) -> Self { match value { TermId::TypeAliasId(id) => id.into(), @@ -484,28 +627,28 @@ impl From<TermId> for SolverDefId { } #[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub struct GeneralConstIdWrapper(pub GeneralConstId); +pub struct GeneralConstIdWrapper<'db>(pub GeneralConstId<'db>); -impl std::fmt::Debug for GeneralConstIdWrapper { +impl std::fmt::Debug for GeneralConstIdWrapper<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Debug::fmt(&self.0, f) } } -impl From<GeneralConstIdWrapper> for GeneralConstId { +impl<'db> From<GeneralConstIdWrapper<'db>> for GeneralConstId<'db> { #[inline] - fn from(value: GeneralConstIdWrapper) -> GeneralConstId { + fn from(value: GeneralConstIdWrapper<'db>) -> GeneralConstId<'db> { value.0 } } -impl From<GeneralConstId> for GeneralConstIdWrapper { +impl<'db> From<GeneralConstId<'db>> for GeneralConstIdWrapper<'db> { #[inline] - fn from(value: GeneralConstId) -> GeneralConstIdWrapper { + fn from(value: GeneralConstId<'db>) -> GeneralConstIdWrapper<'db> { Self(value) } } -impl From<GeneralConstIdWrapper> for SolverDefId { +impl<'db> From<GeneralConstIdWrapper<'db>> for SolverDefId<'db> { #[inline] - fn from(value: GeneralConstIdWrapper) -> SolverDefId { + fn from(value: GeneralConstIdWrapper<'db>) -> SolverDefId<'db> { match value.0 { GeneralConstId::ConstId(id) => SolverDefId::ConstId(id), GeneralConstId::StaticId(id) => SolverDefId::StaticId(id), @@ -513,10 +656,10 @@ impl From<GeneralConstIdWrapper> for SolverDefId { } } } -impl TryFrom<SolverDefId> for GeneralConstIdWrapper { +impl<'db> TryFrom<SolverDefId<'db>> for GeneralConstIdWrapper<'db> { type Error = (); #[inline] - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'db>) -> Result<Self, Self::Error> { match value { SolverDefId::ConstId(it) => Ok(Self(it.into())), SolverDefId::StaticId(it) => Ok(Self(it.into())), @@ -525,8 +668,8 @@ impl TryFrom<SolverDefId> for GeneralConstIdWrapper { } } } -impl<'db> inherent::DefId<DbInterner<'db>> for GeneralConstIdWrapper { - fn as_local(self) -> Option<SolverDefId> { +impl<'db> inherent::DefId<DbInterner<'db>> for GeneralConstIdWrapper<'db> { + fn as_local(self) -> Option<SolverDefId<'db>> { Some(self.into()) } fn is_local(self) -> bool { @@ -554,9 +697,9 @@ impl From<CallableDefId> for CallableIdWrapper { Self(value) } } -impl From<CallableIdWrapper> for SolverDefId { +impl<'db> From<CallableIdWrapper> for SolverDefId<'db> { #[inline] - fn from(value: CallableIdWrapper) -> SolverDefId { + fn from(value: CallableIdWrapper) -> SolverDefId<'db> { match value.0 { CallableDefId::FunctionId(it) => it.into(), CallableDefId::StructId(it) => Ctor::Struct(it).into(), @@ -564,10 +707,10 @@ impl From<CallableIdWrapper> for SolverDefId { } } } -impl TryFrom<SolverDefId> for CallableIdWrapper { +impl TryFrom<SolverDefId<'_>> for CallableIdWrapper { type Error = (); #[inline] - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> { match value { SolverDefId::FunctionId(it) => Ok(Self(it.into())), SolverDefId::Ctor(Ctor::Struct(it)) => Ok(Self(it.into())), @@ -577,7 +720,7 @@ impl TryFrom<SolverDefId> for CallableIdWrapper { } } impl<'db> inherent::DefId<DbInterner<'db>> for CallableIdWrapper { - fn as_local(self) -> Option<SolverDefId> { + fn as_local(self) -> Option<SolverDefId<'db>> { Some(self.into()) } fn is_local(self) -> bool { @@ -593,19 +736,19 @@ pub enum AnyImplId { impl_from!(ImplId, BuiltinDeriveImplId for AnyImplId); -impl From<AnyImplId> for SolverDefId { +impl<'db> From<AnyImplId> for SolverDefId<'db> { #[inline] - fn from(value: AnyImplId) -> SolverDefId { + fn from(value: AnyImplId) -> SolverDefId<'db> { match value { AnyImplId::ImplId(it) => it.into(), AnyImplId::BuiltinDeriveImplId(it) => it.into(), } } } -impl TryFrom<SolverDefId> for AnyImplId { +impl TryFrom<SolverDefId<'_>> for AnyImplId { type Error = (); #[inline] - fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + fn try_from(value: SolverDefId<'_>) -> Result<Self, Self::Error> { match value { SolverDefId::ImplId(it) => Ok(it.into()), SolverDefId::BuiltinDeriveImplId(it) => Ok(it.into()), @@ -614,7 +757,7 @@ impl TryFrom<SolverDefId> for AnyImplId { } } impl<'db> inherent::DefId<DbInterner<'db>> for AnyImplId { - fn as_local(self) -> Option<SolverDefId> { + fn as_local(self) -> Option<SolverDefId<'db>> { Some(self.into()) } fn is_local(self) -> bool { |