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 +++-- 3 files changed, 37 insertions(+), 9 deletions(-) (limited to 'crates/hir/src') 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}, -- cgit v1.2.3