From ec448ca4b33202e384eb034b4e15ed751355488c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 2 Jan 2021 00:42:07 +0100 Subject: Impl HasAttrs for GenericParam --- crates/hir/src/attrs.rs | 13 ++++++++++--- crates/hir/src/from_id.rs | 28 ++++++++++++++++++++++++---- crates/hir/src/lib.rs | 5 +++-- crates/hir_def/src/attr.rs | 21 ++++++++++++++++++++- crates/hir_def/src/lib.rs | 22 +++++++++++++++++++++- 5 files changed, 78 insertions(+), 11 deletions(-) diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs index d32ce37ed..a027f881e 100644 --- a/crates/hir/src/attrs.rs +++ b/crates/hir/src/attrs.rs @@ -3,15 +3,15 @@ use hir_def::{ attr::{Attrs, Documentation}, path::ModPath, resolver::HasResolver, - AttrDefId, ModuleDefId, + AttrDefId, GenericParamId, ModuleDefId, }; use hir_expand::hygiene::Hygiene; use hir_ty::db::HirDatabase; use syntax::ast; use crate::{ - Adt, Const, Enum, Field, Function, MacroDef, Module, ModuleDef, Static, Struct, Trait, - TypeAlias, Union, Variant, + Adt, Const, Enum, Field, Function, GenericParam, MacroDef, Module, ModuleDef, Static, Struct, + Trait, TypeAlias, Union, Variant, }; pub trait HasAttrs { @@ -62,6 +62,7 @@ impl_has_attrs![ (Function, FunctionId), (Adt, AdtId), (Module, ModuleId), + (GenericParam, GenericParamId), ]; macro_rules! impl_has_attrs_adt { @@ -99,6 +100,12 @@ fn resolve_doc_path( AttrDefId::TraitId(it) => it.resolver(db.upcast()), AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()), AttrDefId::ImplId(it) => it.resolver(db.upcast()), + AttrDefId::GenericParamId(it) => match it { + GenericParamId::TypeParamId(it) => it.parent, + GenericParamId::LifetimeParamId(it) => it.parent, + GenericParamId::ConstParamId(it) => it.parent, + } + .resolver(db.upcast()), AttrDefId::MacroDefId(_) => return None, }; let path = ast::Path::parse(link).ok()?; diff --git a/crates/hir/src/from_id.rs b/crates/hir/src/from_id.rs index 2422887e3..3e47a5e9d 100644 --- a/crates/hir/src/from_id.rs +++ b/crates/hir/src/from_id.rs @@ -6,13 +6,13 @@ use hir_def::{ expr::{LabelId, PatId}, item_scope::ItemInNs, - AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, ModuleDefId, - VariantId, + AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, GenericParamId, + ModuleDefId, VariantId, }; use crate::{ - Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, MacroDef, ModuleDef, Variant, - VariantDef, + code_model::GenericParam, Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, + MacroDef, ModuleDef, Variant, VariantDef, }; macro_rules! from_id { @@ -68,6 +68,26 @@ impl From for AdtId { } } +impl From for GenericParam { + fn from(id: GenericParamId) -> Self { + match id { + GenericParamId::TypeParamId(it) => GenericParam::TypeParam(it.into()), + GenericParamId::LifetimeParamId(it) => GenericParam::LifetimeParam(it.into()), + GenericParamId::ConstParamId(it) => GenericParam::ConstParam(it.into()), + } + } +} + +impl From for GenericParamId { + fn from(id: GenericParam) -> Self { + match id { + GenericParam::TypeParam(it) => GenericParamId::TypeParamId(it.id), + GenericParam::LifetimeParam(it) => GenericParamId::LifetimeParamId(it.id), + GenericParam::ConstParam(it) => GenericParamId::ConstParamId(it.id), + } + } +} + impl From for Variant { fn from(id: EnumVariantId) -> Self { Variant { parent: id.parent.into(), id: id.local_id } diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 8ac27e2dd..769945c47 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -35,8 +35,9 @@ pub use crate::{ code_model::{ Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, ConstParam, Crate, CrateDependency, DefWithBody, Enum, Field, FieldSource, Function, - GenericDef, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, - ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, Variant, VariantDef, + GenericDef, GenericParam, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef, + Module, ModuleDef, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, + Variant, VariantDef, }, has_source::HasSource, semantics::{PathResolution, Semantics, SemanticsScope}, diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 042e119b1..86ae2f0fa 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -21,7 +21,7 @@ use crate::{ nameres::ModuleSource, path::{ModPath, PathKind}, src::HasChildSource, - AdtId, AttrDefId, Lookup, + AdtId, AttrDefId, GenericParamId, Lookup, }; /// Holds documentation @@ -235,6 +235,25 @@ impl Attrs { AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db), AttrDefId::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db), AttrDefId::TypeAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db), + AttrDefId::GenericParamId(it) => match it { + GenericParamId::TypeParamId(it) => { + let src = it.parent.child_source(db); + RawAttrs::from_attrs_owner( + db, + src.with_value( + src.value[it.local_id].as_ref().either(|it| it as _, |it| it as _), + ), + ) + } + GenericParamId::LifetimeParamId(it) => { + let src = it.parent.child_source(db); + RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id])) + } + GenericParamId::ConstParamId(it) => { + let src = it.parent.child_source(db); + RawAttrs::from_attrs_owner(db, src.with_value(&src.value[it.local_id])) + } + }, }; raw_attrs.filter(db, def.krate(db)) diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 25f460504..211cb2faf 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -261,6 +261,15 @@ pub enum AdtId { } impl_from!(StructId, UnionId, EnumId for AdtId); +/// A generic param +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum GenericParamId { + TypeParamId(TypeParamId), + LifetimeParamId(LifetimeParamId), + ConstParamId(ConstParamId), +} +impl_from!(TypeParamId, LifetimeParamId, ConstParamId for GenericParamId); + /// The defs which can be visible in the module. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum ModuleDefId { @@ -357,6 +366,7 @@ pub enum AttrDefId { TypeAliasId(TypeAliasId), MacroDefId(MacroDefId), ImplId(ImplId), + GenericParamId(GenericParamId), } impl_from!( @@ -370,7 +380,8 @@ impl_from!( TraitId, TypeAliasId, MacroDefId, - ImplId + ImplId, + GenericParamId for AttrDefId ); @@ -495,6 +506,15 @@ impl AttrDefId { AttrDefId::TraitId(it) => it.lookup(db).container.module(db).krate, AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate, AttrDefId::ImplId(it) => it.lookup(db).container.module(db).krate, + AttrDefId::GenericParamId(it) => { + match it { + GenericParamId::TypeParamId(it) => it.parent, + GenericParamId::LifetimeParamId(it) => it.parent, + GenericParamId::ConstParamId(it) => it.parent, + } + .module(db) + .krate + } // FIXME: `MacroDefId` should store the defining module, then this can implement // `HasModule` AttrDefId::MacroDefId(it) => it.krate, -- cgit v1.2.3 From 68336fdb61e080e1bfb008fd7742ee22ccc51829 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 2 Jan 2021 12:11:46 +0100 Subject: Implement HasAttrs for Type-, Const- and LifetimeParam --- crates/hir/src/attrs.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs index a027f881e..99fb65bac 100644 --- a/crates/hir/src/attrs.rs +++ b/crates/hir/src/attrs.rs @@ -10,8 +10,8 @@ use hir_ty::db::HirDatabase; use syntax::ast; use crate::{ - Adt, Const, Enum, Field, Function, GenericParam, MacroDef, Module, ModuleDef, Static, Struct, - Trait, TypeAlias, Union, Variant, + Adt, Const, ConstParam, Enum, Field, Function, GenericParam, LifetimeParam, MacroDef, Module, + ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant, }; pub trait HasAttrs { @@ -65,23 +65,24 @@ impl_has_attrs![ (GenericParam, GenericParamId), ]; -macro_rules! impl_has_attrs_adt { - ($($adt:ident),*) => {$( - impl HasAttrs for $adt { +macro_rules! impl_has_attrs_enum { + ($($variant:ident),* for $enum:ident) => {$( + impl HasAttrs for $variant { fn attrs(self, db: &dyn HirDatabase) -> Attrs { - Adt::$adt(self).attrs(db) + $enum::$variant(self).attrs(db) } fn docs(self, db: &dyn HirDatabase) -> Option { - Adt::$adt(self).docs(db) + $enum::$variant(self).docs(db) } fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option) -> Option { - Adt::$adt(self).resolve_doc_path(db, link, ns) + $enum::$variant(self).resolve_doc_path(db, link, ns) } } )*}; } -impl_has_attrs_adt![Struct, Union, Enum]; +impl_has_attrs_enum![Struct, Union, Enum for Adt]; +impl_has_attrs_enum![TypeParam, ConstParam, LifetimeParam for GenericParam]; fn resolve_doc_path( db: &dyn HirDatabase, -- cgit v1.2.3