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 | 189 |
1 files changed, 181 insertions, 8 deletions
diff --git a/crates/hir-ty/src/next_solver/def_id.rs b/crates/hir-ty/src/next_solver/def_id.rs index 1ae59beca2..b6167b4a09 100644 --- a/crates/hir-ty/src/next_solver/def_id.rs +++ b/crates/hir-ty/src/next_solver/def_id.rs @@ -1,8 +1,9 @@ //! Definition of `SolverDefId` use hir_def::{ - AdtId, CallableDefId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, - StaticId, StructId, TraitId, TypeAliasId, UnionId, + AdtId, AttrDefId, CallableDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, + GeneralConstId, GenericDefId, HasModule, ImplId, ModuleId, StaticId, StructId, TraitId, + TypeAliasId, UnionId, db::DefDatabase, }; use rustc_type_ir::inherent; use stdx::impl_from; @@ -29,6 +30,8 @@ pub enum SolverDefId { InternedClosureId(InternedClosureId), InternedCoroutineId(InternedCoroutineId), InternedOpaqueTyId(InternedOpaqueTyId), + EnumVariantId(EnumVariantId), + // FIXME(next-solver): Do we need the separation of `Ctor`? It duplicates some variants. Ctor(Ctor), } @@ -73,6 +76,16 @@ impl std::fmt::Debug for SolverDefId { SolverDefId::InternedOpaqueTyId(id) => { f.debug_tuple("InternedOpaqueTyId").field(&id).finish() } + SolverDefId::EnumVariantId(id) => { + let parent_enum = id.loc(db).parent; + f.debug_tuple("EnumVariantId") + .field(&format_args!( + "\"{}::{}\"", + db.enum_signature(parent_enum).name.as_str(), + parent_enum.enum_variants(db).variant_name_by_id(id).unwrap().as_str() + )) + .finish() + } SolverDefId::Ctor(Ctor::Struct(id)) => { f.debug_tuple("Ctor").field(&db.struct_signature(id).name.as_str()).finish() } @@ -101,6 +114,7 @@ impl_from!( InternedClosureId, InternedCoroutineId, InternedOpaqueTyId, + EnumVariantId, Ctor for SolverDefId ); @@ -119,8 +133,75 @@ impl From<GenericDefId> for SolverDefId { } } +impl From<GeneralConstId> for SolverDefId { + #[inline] + fn from(value: GeneralConstId) -> Self { + match value { + GeneralConstId::ConstId(const_id) => SolverDefId::ConstId(const_id), + GeneralConstId::StaticId(static_id) => SolverDefId::StaticId(static_id), + } + } +} + +impl From<DefWithBodyId> for SolverDefId { + #[inline] + fn from(value: DefWithBodyId) -> Self { + match value { + DefWithBodyId::FunctionId(id) => id.into(), + DefWithBodyId::StaticId(id) => id.into(), + DefWithBodyId::ConstId(id) => id.into(), + DefWithBodyId::VariantId(id) => id.into(), + } + } +} + +impl TryFrom<SolverDefId> for AttrDefId { + type Error = (); + #[inline] + fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + match value { + SolverDefId::AdtId(it) => Ok(it.into()), + SolverDefId::ConstId(it) => Ok(it.into()), + SolverDefId::FunctionId(it) => Ok(it.into()), + SolverDefId::ImplId(it) => Ok(it.into()), + SolverDefId::StaticId(it) => Ok(it.into()), + SolverDefId::TraitId(it) => Ok(it.into()), + SolverDefId::TypeAliasId(it) => Ok(it.into()), + SolverDefId::EnumVariantId(it) => Ok(it.into()), + SolverDefId::Ctor(Ctor::Struct(it)) => Ok(it.into()), + SolverDefId::Ctor(Ctor::Enum(it)) => Ok(it.into()), + SolverDefId::InternedClosureId(_) + | SolverDefId::InternedCoroutineId(_) + | SolverDefId::InternedOpaqueTyId(_) => Err(()), + } + } +} + +impl TryFrom<SolverDefId> for DefWithBodyId { + type Error = (); + + #[inline] + fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + let id = match value { + SolverDefId::ConstId(id) => id.into(), + SolverDefId::FunctionId(id) => id.into(), + SolverDefId::StaticId(id) => id.into(), + SolverDefId::EnumVariantId(id) | SolverDefId::Ctor(Ctor::Enum(id)) => id.into(), + SolverDefId::InternedOpaqueTyId(_) + | SolverDefId::TraitId(_) + | SolverDefId::TypeAliasId(_) + | SolverDefId::ImplId(_) + | SolverDefId::InternedClosureId(_) + | SolverDefId::InternedCoroutineId(_) + | SolverDefId::Ctor(Ctor::Struct(_)) + | SolverDefId::AdtId(_) => return Err(()), + }; + Ok(id) + } +} + impl TryFrom<SolverDefId> for GenericDefId { - type Error = SolverDefId; + type Error = (); fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { Ok(match value { @@ -131,14 +212,57 @@ impl TryFrom<SolverDefId> for GenericDefId { SolverDefId::StaticId(static_id) => GenericDefId::StaticId(static_id), SolverDefId::TraitId(trait_id) => GenericDefId::TraitId(trait_id), SolverDefId::TypeAliasId(type_alias_id) => GenericDefId::TypeAliasId(type_alias_id), - SolverDefId::InternedClosureId(_) => return Err(value), - SolverDefId::InternedCoroutineId(_) => return Err(value), - SolverDefId::InternedOpaqueTyId(_) => return Err(value), - SolverDefId::Ctor(_) => return Err(value), + SolverDefId::InternedClosureId(_) + | SolverDefId::InternedCoroutineId(_) + | SolverDefId::InternedOpaqueTyId(_) + | SolverDefId::EnumVariantId(_) + | SolverDefId::Ctor(_) => return Err(()), }) } } +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 HasModule for SolverDefId { + fn module(&self, db: &dyn DefDatabase) -> ModuleId { + match *self { + SolverDefId::AdtId(id) => id.module(db), + SolverDefId::ConstId(id) => id.module(db), + SolverDefId::FunctionId(id) => id.module(db), + SolverDefId::ImplId(id) => id.module(db), + SolverDefId::StaticId(id) => id.module(db), + SolverDefId::TraitId(id) => id.module(db), + SolverDefId::TypeAliasId(id) => id.module(db), + SolverDefId::InternedClosureId(id) => id.loc(db).0.module(db), + SolverDefId::InternedCoroutineId(id) => id.loc(db).0.module(db), + SolverDefId::InternedOpaqueTyId(id) => match id.loc(db) { + crate::ImplTraitId::ReturnTypeImplTrait(owner, _) => owner.module(db), + crate::ImplTraitId::TypeAliasImplTrait(owner, _) => owner.module(db), + }, + SolverDefId::Ctor(Ctor::Enum(id)) | SolverDefId::EnumVariantId(id) => id.module(db), + SolverDefId::Ctor(Ctor::Struct(id)) => id.module(db), + } + } +} + impl<'db> inherent::DefId<DbInterner<'db>> for SolverDefId { fn as_local(self) -> Option<SolverDefId> { Some(self) @@ -155,7 +279,7 @@ macro_rules! declare_id_wrapper { impl std::fmt::Debug for $name { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::fmt::Debug::fmt(&self.0, f) + std::fmt::Debug::fmt(&SolverDefId::from(self.0), f) } } @@ -211,6 +335,55 @@ declare_id_wrapper!(AdtIdWrapper, AdtId); declare_id_wrapper!(ImplIdWrapper, ImplId); #[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct GeneralConstIdWrapper(pub GeneralConstId); + +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 { + #[inline] + fn from(value: GeneralConstIdWrapper) -> GeneralConstId { + value.0 + } +} +impl From<GeneralConstId> for GeneralConstIdWrapper { + #[inline] + fn from(value: GeneralConstId) -> GeneralConstIdWrapper { + Self(value) + } +} +impl From<GeneralConstIdWrapper> for SolverDefId { + #[inline] + fn from(value: GeneralConstIdWrapper) -> SolverDefId { + match value.0 { + GeneralConstId::ConstId(id) => SolverDefId::ConstId(id), + GeneralConstId::StaticId(id) => SolverDefId::StaticId(id), + } + } +} +impl TryFrom<SolverDefId> for GeneralConstIdWrapper { + type Error = (); + #[inline] + fn try_from(value: SolverDefId) -> Result<Self, Self::Error> { + match value { + SolverDefId::ConstId(it) => Ok(Self(it.into())), + SolverDefId::StaticId(it) => Ok(Self(it.into())), + _ => Err(()), + } + } +} +impl<'db> inherent::DefId<DbInterner<'db>> for GeneralConstIdWrapper { + fn as_local(self) -> Option<SolverDefId> { + Some(self.into()) + } + fn is_local(self) -> bool { + true + } +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct CallableIdWrapper(pub CallableDefId); impl std::fmt::Debug for CallableIdWrapper { |