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 | 137 |
1 files changed, 104 insertions, 33 deletions
diff --git a/crates/hir-ty/src/next_solver/def_id.rs b/crates/hir-ty/src/next_solver/def_id.rs index 41b00b67cd..63337b297d 100644 --- a/crates/hir-ty/src/next_solver/def_id.rs +++ b/crates/hir-ty/src/next_solver/def_id.rs @@ -310,26 +310,6 @@ impl TryFrom<SolverDefId> for GenericDefId { } } -impl SolverDefId { - #[inline] - #[track_caller] - pub fn expect_opaque_ty(self) -> InternedOpaqueTyId { - match self { - SolverDefId::InternedOpaqueTyId(it) => it, - _ => panic!("expected opaque type, found {self:?}"), - } - } - - #[inline] - #[track_caller] - pub fn expect_type_alias(self) -> TypeAliasId { - match self { - SolverDefId::TypeAliasId(it) => it, - _ => panic!("expected type alias, found {self:?}"), - } - } -} - impl<'db> inherent::DefId<DbInterner<'db>> for SolverDefId { fn as_local(self) -> Option<SolverDefId> { Some(self) @@ -341,6 +321,26 @@ impl<'db> inherent::DefId<DbInterner<'db>> for SolverDefId { macro_rules! declare_id_wrapper { ($name:ident, $wraps:ident) => { + declare_id_wrapper!($name, $wraps, SolverDefId); + }; + + ($name:ident, $wraps:ident, $local:ident) => { + declare_id_wrapper!($name, $wraps, $local, no_try_from); + + impl TryFrom<SolverDefId> for $name { + type Error = (); + + #[inline] + fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + match value { + SolverDefId::$wraps(it) => Ok(Self(it)), + _ => Err(()), + } + } + } + }; + + ($name:ident, $wraps:ident, $local:ident, no_try_from) => { #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct $name(pub $wraps); @@ -371,35 +371,106 @@ macro_rules! declare_id_wrapper { } } - impl TryFrom<SolverDefId> for $name { + impl<'db> inherent::DefId<DbInterner<'db>, $local> for $name { + fn as_local(self) -> Option<$local> { + Some(self.into()) + } + fn is_local(self) -> bool { + true + } + } + }; +} + +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); +declare_id_wrapper!(AdtIdWrapper, AdtId); +declare_id_wrapper!(OpaqueTyIdWrapper, InternedOpaqueTyId, OpaqueTyIdWrapper); + +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); + + impl TryFrom<SolverDefId> for $term_id_name { type Error = (); #[inline] fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { match value { - SolverDefId::$wraps(it) => Ok(Self(it)), + SolverDefId::TypeAliasId(it) => Ok(Self(TermId::TypeAliasId(it))), + SolverDefId::ConstId(it) => Ok(Self(TermId::ConstId(it))), _ => Err(()), } } } - impl<'db> inherent::DefId<DbInterner<'db>> for $name { - fn as_local(self) -> Option<SolverDefId> { - Some(self.into()) + impl From<$ty_id_name> for $term_id_name { + fn from(value: $ty_id_name) -> Self { + $term_id_name(TermId::TypeAliasId(value.0)) } - fn is_local(self) -> bool { - true + } + + impl From<$const_id_name> for $term_id_name { + fn from(value: $const_id_name) -> Self { + $term_id_name(TermId::ConstId(value.0)) + } + } + + impl TryFrom<$term_id_name> for $ty_id_name { + type Error = (); + + fn try_from(value: $term_id_name) -> Result<Self, Self::Error> { + match value.0 { + TermId::TypeAliasId(id) => Ok($ty_id_name(id)), + TermId::ConstId(_) => Err(()), + } + } + } + + impl TryFrom<$term_id_name> for $const_id_name { + type Error = (); + + fn try_from(value: $term_id_name) -> Result<Self, Self::Error> { + match value.0 { + TermId::ConstId(id) => Ok($const_id_name(id)), + TermId::TypeAliasId(_) => Err(()), + } + } + } + + impl From<$const_id_name> for GeneralConstIdWrapper { + fn from(value: $const_id_name) -> Self { + GeneralConstIdWrapper(GeneralConstId::ConstId(value.0)) } } }; } -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); -declare_id_wrapper!(AdtIdWrapper, AdtId); +declare_ty_const_pair!(TraitAssocTyId, TraitAssocConstId, TraitAssocTermId); +declare_ty_const_pair!(ImplOrTraitAssocTyId, ImplOrTraitAssocConstId, ImplOrTraitAssocTermId); +declare_ty_const_pair!(FreeTyAliasId, FreeConstAliasId, FreeTermAliasId); +declare_ty_const_pair!(InherentAssocTyId, InherentAssocConstId, InherentAssocTermId); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum TermId { + TypeAliasId(TypeAliasId), + ConstId(ConstId), +} +impl_from!(TypeAliasId, ConstId for TermId); + +impl From<TermId> for SolverDefId { + fn from(value: TermId) -> Self { + match value { + TermId::TypeAliasId(id) => id.into(), + TermId::ConstId(id) => id.into(), + } + } +} #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct GeneralConstIdWrapper(pub GeneralConstId); |