mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Merge #7128
7128: Implement HasAttrs for GenericParam r=matklad a=Veykril Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
3bf4cec799
5 changed files with 86 additions and 18 deletions
|
@ -3,15 +3,15 @@ use hir_def::{
|
||||||
attr::{Attrs, Documentation},
|
attr::{Attrs, Documentation},
|
||||||
path::ModPath,
|
path::ModPath,
|
||||||
resolver::HasResolver,
|
resolver::HasResolver,
|
||||||
AttrDefId, ModuleDefId,
|
AttrDefId, GenericParamId, ModuleDefId,
|
||||||
};
|
};
|
||||||
use hir_expand::hygiene::Hygiene;
|
use hir_expand::hygiene::Hygiene;
|
||||||
use hir_ty::db::HirDatabase;
|
use hir_ty::db::HirDatabase;
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Adt, Const, Enum, Field, Function, MacroDef, Module, ModuleDef, Static, Struct, Trait,
|
Adt, Const, ConstParam, Enum, Field, Function, GenericParam, LifetimeParam, MacroDef, Module,
|
||||||
TypeAlias, Union, Variant,
|
ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait HasAttrs {
|
pub trait HasAttrs {
|
||||||
|
@ -62,25 +62,27 @@ impl_has_attrs![
|
||||||
(Function, FunctionId),
|
(Function, FunctionId),
|
||||||
(Adt, AdtId),
|
(Adt, AdtId),
|
||||||
(Module, ModuleId),
|
(Module, ModuleId),
|
||||||
|
(GenericParam, GenericParamId),
|
||||||
];
|
];
|
||||||
|
|
||||||
macro_rules! impl_has_attrs_adt {
|
macro_rules! impl_has_attrs_enum {
|
||||||
($($adt:ident),*) => {$(
|
($($variant:ident),* for $enum:ident) => {$(
|
||||||
impl HasAttrs for $adt {
|
impl HasAttrs for $variant {
|
||||||
fn attrs(self, db: &dyn HirDatabase) -> Attrs {
|
fn attrs(self, db: &dyn HirDatabase) -> Attrs {
|
||||||
Adt::$adt(self).attrs(db)
|
$enum::$variant(self).attrs(db)
|
||||||
}
|
}
|
||||||
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
|
||||||
Adt::$adt(self).docs(db)
|
$enum::$variant(self).docs(db)
|
||||||
}
|
}
|
||||||
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
|
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
|
||||||
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(
|
fn resolve_doc_path(
|
||||||
db: &dyn HirDatabase,
|
db: &dyn HirDatabase,
|
||||||
|
@ -99,6 +101,12 @@ fn resolve_doc_path(
|
||||||
AttrDefId::TraitId(it) => it.resolver(db.upcast()),
|
AttrDefId::TraitId(it) => it.resolver(db.upcast()),
|
||||||
AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()),
|
AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()),
|
||||||
AttrDefId::ImplId(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,
|
AttrDefId::MacroDefId(_) => return None,
|
||||||
};
|
};
|
||||||
let path = ast::Path::parse(link).ok()?;
|
let path = ast::Path::parse(link).ok()?;
|
||||||
|
|
|
@ -6,13 +6,13 @@
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
expr::{LabelId, PatId},
|
expr::{LabelId, PatId},
|
||||||
item_scope::ItemInNs,
|
item_scope::ItemInNs,
|
||||||
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, ModuleDefId,
|
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, GenericParamId,
|
||||||
VariantId,
|
ModuleDefId, VariantId,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, MacroDef, ModuleDef, Variant,
|
code_model::GenericParam, Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local,
|
||||||
VariantDef,
|
MacroDef, ModuleDef, Variant, VariantDef,
|
||||||
};
|
};
|
||||||
|
|
||||||
macro_rules! from_id {
|
macro_rules! from_id {
|
||||||
|
@ -68,6 +68,26 @@ impl From<Adt> for AdtId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<GenericParamId> 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<GenericParam> 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<EnumVariantId> for Variant {
|
impl From<EnumVariantId> for Variant {
|
||||||
fn from(id: EnumVariantId) -> Self {
|
fn from(id: EnumVariantId) -> Self {
|
||||||
Variant { parent: id.parent.into(), id: id.local_id }
|
Variant { parent: id.parent.into(), id: id.local_id }
|
||||||
|
|
|
@ -35,8 +35,9 @@ pub use crate::{
|
||||||
code_model::{
|
code_model::{
|
||||||
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
|
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
|
||||||
ConstParam, Crate, CrateDependency, DefWithBody, Enum, Field, FieldSource, Function,
|
ConstParam, Crate, CrateDependency, DefWithBody, Enum, Field, FieldSource, Function,
|
||||||
GenericDef, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef,
|
GenericDef, GenericParam, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef,
|
||||||
ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, Variant, VariantDef,
|
Module, ModuleDef, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union,
|
||||||
|
Variant, VariantDef,
|
||||||
},
|
},
|
||||||
has_source::HasSource,
|
has_source::HasSource,
|
||||||
semantics::{PathResolution, Semantics, SemanticsScope},
|
semantics::{PathResolution, Semantics, SemanticsScope},
|
||||||
|
|
|
@ -21,7 +21,7 @@ use crate::{
|
||||||
nameres::ModuleSource,
|
nameres::ModuleSource,
|
||||||
path::{ModPath, PathKind},
|
path::{ModPath, PathKind},
|
||||||
src::HasChildSource,
|
src::HasChildSource,
|
||||||
AdtId, AttrDefId, Lookup,
|
AdtId, AttrDefId, GenericParamId, Lookup,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Holds documentation
|
/// Holds documentation
|
||||||
|
@ -235,6 +235,25 @@ impl Attrs {
|
||||||
AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
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::FunctionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
|
||||||
AttrDefId::TypeAliasId(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))
|
raw_attrs.filter(db, def.krate(db))
|
||||||
|
|
|
@ -261,6 +261,15 @@ pub enum AdtId {
|
||||||
}
|
}
|
||||||
impl_from!(StructId, UnionId, EnumId for 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.
|
/// The defs which can be visible in the module.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub enum ModuleDefId {
|
pub enum ModuleDefId {
|
||||||
|
@ -357,6 +366,7 @@ pub enum AttrDefId {
|
||||||
TypeAliasId(TypeAliasId),
|
TypeAliasId(TypeAliasId),
|
||||||
MacroDefId(MacroDefId),
|
MacroDefId(MacroDefId),
|
||||||
ImplId(ImplId),
|
ImplId(ImplId),
|
||||||
|
GenericParamId(GenericParamId),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_from!(
|
impl_from!(
|
||||||
|
@ -370,7 +380,8 @@ impl_from!(
|
||||||
TraitId,
|
TraitId,
|
||||||
TypeAliasId,
|
TypeAliasId,
|
||||||
MacroDefId,
|
MacroDefId,
|
||||||
ImplId
|
ImplId,
|
||||||
|
GenericParamId
|
||||||
for AttrDefId
|
for AttrDefId
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -495,6 +506,15 @@ impl AttrDefId {
|
||||||
AttrDefId::TraitId(it) => it.lookup(db).container.module(db).krate,
|
AttrDefId::TraitId(it) => it.lookup(db).container.module(db).krate,
|
||||||
AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
|
AttrDefId::TypeAliasId(it) => it.lookup(db).module(db).krate,
|
||||||
AttrDefId::ImplId(it) => it.lookup(db).container.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
|
// FIXME: `MacroDefId` should store the defining module, then this can implement
|
||||||
// `HasModule`
|
// `HasModule`
|
||||||
AttrDefId::MacroDefId(it) => it.krate,
|
AttrDefId::MacroDefId(it) => it.krate,
|
||||||
|
|
Loading…
Reference in a new issue