From 66e5ced894e430117d4d040e13721313237936ab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 25 Aug 2020 14:44:15 +0200 Subject: Remove AttrDef --- crates/hir/src/attrs.rs | 81 +++++++++++++++++++++++--------------------- crates/hir/src/code_model.rs | 10 +++--- crates/hir/src/from_id.rs | 25 +++----------- crates/hir/src/lib.rs | 2 +- 4 files changed, 52 insertions(+), 66 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs index 953960e14..36027d183 100644 --- a/crates/hir/src/attrs.rs +++ b/crates/hir/src/attrs.rs @@ -4,60 +4,63 @@ use hir_def::{ db::DefDatabase, docs::Documentation, resolver::{HasResolver, Resolver}, - AdtId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId, + AdtId, AttrDefId, FunctionId, GenericDefId, ModuleId, StaticId, TraitId, VariantId, }; use hir_ty::db::HirDatabase; -use stdx::impl_from; use crate::{ doc_links::Resolvable, Adt, Const, Enum, EnumVariant, Field, Function, GenericDef, ImplDef, Local, MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, }; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum AttrDef { - Module(Module), - Field(Field), - Adt(Adt), - Function(Function), - EnumVariant(EnumVariant), - Static(Static), - Const(Const), - Trait(Trait), - TypeAlias(TypeAlias), - MacroDef(MacroDef), -} - -impl_from!( - Module, - Field, - Adt(Struct, Enum, Union), - EnumVariant, - Static, - Const, - Function, - Trait, - TypeAlias, - MacroDef - for AttrDef -); - pub trait HasAttrs { fn attrs(self, db: &dyn HirDatabase) -> Attrs; fn docs(self, db: &dyn HirDatabase) -> Option; } -impl> HasAttrs for T { - fn attrs(self, db: &dyn HirDatabase) -> Attrs { - let def: AttrDef = self.into(); - db.attrs(def.into()) - } - fn docs(self, db: &dyn HirDatabase) -> Option { - let def: AttrDef = self.into(); - db.documentation(def.into()) - } +macro_rules! impl_has_attrs { + ($(($def:ident, $def_id:ident),)*) => {$( + impl HasAttrs for $def { + fn attrs(self, db: &dyn HirDatabase) -> Attrs { + let def = AttrDefId::$def_id(self.into()); + db.attrs(def) + } + fn docs(self, db: &dyn HirDatabase) -> Option { + let def = AttrDefId::$def_id(self.into()); + db.documentation(def) + } + } + )*}; +} + +impl_has_attrs![ + (Field, FieldId), + (EnumVariant, EnumVariantId), + (Static, StaticId), + (Const, ConstId), + (Trait, TraitId), + (TypeAlias, TypeAliasId), + (MacroDef, MacroDefId), + (Function, FunctionId), + (Adt, AdtId), + (Module, ModuleId), +]; + +macro_rules! impl_has_attrs_adt { + ($($adt:ident),*) => {$( + impl HasAttrs for $adt { + fn attrs(self, db: &dyn HirDatabase) -> Attrs { + Adt::$adt(self).attrs(db) + } + fn docs(self, db: &dyn HirDatabase) -> Option { + Adt::$adt(self).docs(db) + } + } + )*}; } +impl_has_attrs_adt![Struct, Union, Enum]; + impl Resolvable for ModuleDef { fn resolver(&self, db: &D) -> Option { Some(match self { diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 59d8b3073..c2ee20dbb 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -17,9 +17,9 @@ use hir_def::{ resolver::{HasResolver, Resolver}, src::HasSource as _, type_ref::{Mutability, TypeRef}, - AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, - ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, - TraitId, TypeAliasId, TypeParamId, UnionId, + AdtId, AssocContainerId, AttrDefId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, + HasModule, ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, + StructId, TraitId, TypeAliasId, TypeParamId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -43,7 +43,7 @@ use tt::{Ident, Leaf, Literal, TokenTree}; use crate::{ db::{DefDatabase, HirDatabase}, has_source::HasSource, - AttrDef, HirDisplay, InFile, Name, + HirDisplay, InFile, Name, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -126,7 +126,7 @@ impl Crate { /// Try to get the root URL of the documentation of a crate. pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option { // Look for #![doc(html_root_url = "...")] - let attrs = db.attrs(AttrDef::from(self.root_module(db)).into()); + let attrs = db.attrs(AttrDefId::ModuleId(self.root_module(db).into())); let doc_attr_q = attrs.by_key("doc"); if !doc_attr_q.exists() { diff --git a/crates/hir/src/from_id.rs b/crates/hir/src/from_id.rs index a53ac1e08..033f53ac2 100644 --- a/crates/hir/src/from_id.rs +++ b/crates/hir/src/from_id.rs @@ -4,13 +4,13 @@ //! are splitting the hir. use hir_def::{ - expr::PatId, AdtId, AssocItemId, AttrDefId, DefWithBodyId, EnumVariantId, FieldId, - GenericDefId, ModuleDefId, VariantId, + expr::PatId, AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, + ModuleDefId, VariantId, }; use crate::{ - code_model::ItemInNs, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, Field, GenericDef, - Local, MacroDef, ModuleDef, VariantDef, + code_model::ItemInNs, Adt, AssocItem, DefWithBody, EnumVariant, Field, GenericDef, Local, + MacroDef, ModuleDef, VariantDef, }; macro_rules! from_id { @@ -196,23 +196,6 @@ impl From for Field { } } -impl From for AttrDefId { - fn from(def: AttrDef) -> Self { - match def { - AttrDef::Module(it) => AttrDefId::ModuleId(it.id), - AttrDef::Field(it) => AttrDefId::FieldId(it.into()), - AttrDef::Adt(it) => AttrDefId::AdtId(it.into()), - AttrDef::Function(it) => AttrDefId::FunctionId(it.id), - AttrDef::EnumVariant(it) => AttrDefId::EnumVariantId(it.into()), - AttrDef::Static(it) => AttrDefId::StaticId(it.id), - AttrDef::Const(it) => AttrDefId::ConstId(it.id), - AttrDef::Trait(it) => AttrDefId::TraitId(it.id), - AttrDef::TypeAlias(it) => AttrDefId::TypeAliasId(it.id), - AttrDef::MacroDef(it) => AttrDefId::MacroDefId(it.id), - } - } -} - impl From for GenericDefId { fn from(item: AssocItem) -> Self { match item { diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d1c198bff..03915ea1b 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -32,7 +32,7 @@ mod attrs; mod has_source; pub use crate::{ - attrs::{AttrDef, HasAttrs}, + attrs::HasAttrs, code_model::{ Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function, -- cgit v1.2.3