From 8d87f9b298f41b8eb1e9fa0481c5092c1c136ef9 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 6 Jun 2021 15:51:05 +0200 Subject: Handle attribute macros in `descend_into_macros` --- crates/hir_def/src/child_by_source.rs | 4 ++++ crates/hir_def/src/item_scope.rs | 17 +++++++++++++++-- crates/hir_def/src/keys.rs | 3 ++- crates/hir_def/src/nameres/collector.rs | 5 +++++ 4 files changed, 26 insertions(+), 3 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/child_by_source.rs b/crates/hir_def/src/child_by_source.rs index f2e809ca9..f22383e22 100644 --- a/crates/hir_def/src/child_by_source.rs +++ b/crates/hir_def/src/child_by_source.rs @@ -85,6 +85,10 @@ impl ChildBySource for ItemScope { res[keys::CONST].insert(src, konst); }); self.impls().for_each(|imp| add_impl(db, res, imp)); + self.attr_macro_invocs().for_each(|(ast_id, call_id)| { + let item = ast_id.with_value(ast_id.to_node(db.upcast())); + res[keys::ATTR_MACRO].insert(item, call_id); + }); fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) { match item { diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs index 9014468ea..0f74f050d 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs @@ -4,11 +4,11 @@ use std::collections::hash_map::Entry; use base_db::CrateId; -use hir_expand::name::Name; -use hir_expand::MacroDefKind; +use hir_expand::{name::Name, AstId, MacroCallId, MacroDefKind}; use once_cell::sync::Lazy; use rustc_hash::{FxHashMap, FxHashSet}; use stdx::format_to; +use syntax::ast; use crate::{ db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId, ImplId, @@ -53,6 +53,7 @@ pub struct ItemScope { // FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will // be all resolved to the last one defined if shadowing happens. legacy_macros: FxHashMap, + attr_macros: FxHashMap, MacroCallId>, } pub(crate) static BUILTIN_SCOPE: Lazy> = Lazy::new(|| { @@ -169,6 +170,16 @@ impl ItemScope { self.legacy_macros.insert(name, mac); } + pub(crate) fn add_attr_macro_invoc(&mut self, item: AstId, call: MacroCallId) { + self.attr_macros.insert(item, call); + } + + pub(crate) fn attr_macro_invocs( + &self, + ) -> impl Iterator, MacroCallId)> + '_ { + self.attr_macros.iter().map(|(k, v)| (*k, *v)) + } + pub(crate) fn unnamed_trait_vis(&self, tr: TraitId) -> Option { self.unnamed_trait_imports.get(&tr).copied() } @@ -307,6 +318,7 @@ impl ItemScope { unnamed_consts, unnamed_trait_imports, legacy_macros, + attr_macros, } = self; types.shrink_to_fit(); values.shrink_to_fit(); @@ -317,6 +329,7 @@ impl ItemScope { unnamed_consts.shrink_to_fit(); unnamed_trait_imports.shrink_to_fit(); legacy_macros.shrink_to_fit(); + attr_macros.shrink_to_fit(); } } diff --git a/crates/hir_def/src/keys.rs b/crates/hir_def/src/keys.rs index 89b3ed868..688cd9fcf 100644 --- a/crates/hir_def/src/keys.rs +++ b/crates/hir_def/src/keys.rs @@ -2,7 +2,7 @@ use std::marker::PhantomData; -use hir_expand::{InFile, MacroDefId}; +use hir_expand::{InFile, MacroCallId, MacroDefId}; use rustc_hash::FxHashMap; use syntax::{ast, AstNode, AstPtr}; @@ -32,6 +32,7 @@ pub const LIFETIME_PARAM: Key = Key::new(); pub const CONST_PARAM: Key = Key::new(); pub const MACRO: Key = Key::new(); +pub const ATTR_MACRO: Key = Key::new(); /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are /// equal if they point to exactly the same object. diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index d0b1db5d1..d019ba3a9 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -1112,6 +1112,11 @@ impl DefCollector<'_> { return false; } } + + self.def_map.modules[directive.module_id] + .scope + .add_attr_macro_invoc(ast_id.ast_id, call_id); + resolved.push((directive.module_id, call_id, directive.depth)); res = ReachedFixedPoint::No; return false; -- cgit v1.2.3