From c05a1a6e37156b956380d57049a72cfe6f21095d Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 18 Mar 2021 16:11:18 +0100 Subject: Store an `AstId` for procedural macros --- crates/hir/src/has_source.rs | 15 +++++++++++---- crates/hir/src/lib.rs | 7 +++++-- 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 5b22ab58b..d57fad9ed 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -6,7 +6,7 @@ use hir_def::{ src::{HasChildSource, HasSource as _}, Lookup, VariantId, }; -use hir_expand::InFile; +use hir_expand::{InFile, MacroDefKind}; use syntax::ast; use crate::{ @@ -111,10 +111,17 @@ impl HasSource for TypeAlias { } } impl HasSource for MacroDef { - type Ast = ast::Macro; + type Ast = Either; fn source(self, db: &dyn HirDatabase) -> Option> { - let ast_id = self.id.ast_id()?; - Some(InFile { file_id: ast_id.file_id, value: ast_id.to_node(db.upcast()) }) + Some(match &self.id.kind { + MacroDefKind::Declarative(id) + | MacroDefKind::BuiltIn(_, id) + | MacroDefKind::BuiltInDerive(_, id) + | MacroDefKind::BuiltInEager(_, id) => { + id.with_value(Either::Left(id.to_node(db.upcast()))) + } + MacroDefKind::ProcMacro(_, id) => id.map(|_| Either::Right(id.to_node(db.upcast()))), + }) } } impl HasSource for Impl { diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index b860cbf3c..95cfde61c 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1144,12 +1144,15 @@ impl MacroDef { /// XXX: this parses the file pub fn name(self, db: &dyn HirDatabase) -> Option { - self.source(db)?.value.name().map(|it| it.as_name()) + match self.source(db)?.value { + Either::Left(it) => it.name().map(|it| it.as_name()), + Either::Right(it) => it.name().map(|it| it.as_name()), + } } /// Indicate it is a proc-macro pub fn is_proc_macro(&self) -> bool { - matches!(self.id.kind, MacroDefKind::ProcMacro(_)) + matches!(self.id.kind, MacroDefKind::ProcMacro(..)) } /// Indicate it is a derive macro -- cgit v1.2.3