Skip to content

Commit 8d87f9b

Browse files
Handle attribute macros in descend_into_macros
1 parent 13da28c commit 8d87f9b

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
lines changed

crates/hir/src/semantics.rs

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -362,25 +362,57 @@ impl<'db> SemanticsImpl<'db> {
362362

363363
let token = successors(Some(InFile::new(sa.file_id, token)), |token| {
364364
self.db.unwind_if_cancelled();
365-
let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?;
366-
let tt = macro_call.token_tree()?;
367-
if !tt.syntax().text_range().contains_range(token.value.text_range()) {
368-
return None;
369-
}
370-
let file_id = sa.expand(self.db, token.with_value(&macro_call))?;
371-
let token = self
372-
.expansion_info_cache
373-
.borrow_mut()
374-
.entry(file_id)
375-
.or_insert_with(|| file_id.expansion_info(self.db.upcast()))
376-
.as_ref()?
377-
.map_token_down(token.as_ref())?;
378-
379-
if let Some(parent) = token.value.parent() {
380-
self.cache(find_root(&parent), token.file_id);
365+
366+
for node in token.value.ancestors() {
367+
match_ast! {
368+
match node {
369+
ast::MacroCall(macro_call) => {
370+
let tt = macro_call.token_tree()?;
371+
if !tt.syntax().text_range().contains_range(token.value.text_range()) {
372+
return None;
373+
}
374+
let file_id = sa.expand(self.db, token.with_value(&macro_call))?;
375+
let token = self
376+
.expansion_info_cache
377+
.borrow_mut()
378+
.entry(file_id)
379+
.or_insert_with(|| file_id.expansion_info(self.db.upcast()))
380+
.as_ref()?
381+
.map_token_down(token.as_ref())?;
382+
383+
if let Some(parent) = token.value.parent() {
384+
self.cache(find_root(&parent), token.file_id);
385+
}
386+
387+
return Some(token);
388+
},
389+
ast::Item(item) => {
390+
match self.with_ctx(|ctx| ctx.item_to_macro_call(token.with_value(item))) {
391+
Some(call_id) => {
392+
let file_id = call_id.as_file();
393+
let token = self
394+
.expansion_info_cache
395+
.borrow_mut()
396+
.entry(file_id)
397+
.or_insert_with(|| file_id.expansion_info(self.db.upcast()))
398+
.as_ref()?
399+
.map_token_down(token.as_ref())?;
400+
401+
if let Some(parent) = token.value.parent() {
402+
self.cache(find_root(&parent), token.file_id);
403+
}
404+
405+
return Some(token);
406+
}
407+
None => {}
408+
}
409+
},
410+
_ => {}
411+
}
412+
}
381413
}
382414

383-
Some(token)
415+
None
384416
})
385417
.last()
386418
.unwrap();

crates/hir/src/semantics/source_to_def.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir_def::{
1010
ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
1111
UnionId, VariantId,
1212
};
13-
use hir_expand::{name::AsName, AstId, MacroDefKind};
13+
use hir_expand::{name::AsName, AstId, MacroCallId, MacroDefKind};
1414
use rustc_hash::FxHashMap;
1515
use smallvec::SmallVec;
1616
use stdx::impl_from;
@@ -145,16 +145,25 @@ impl SourceToDefCtx<'_, '_> {
145145
Some((container, label_id))
146146
}
147147

148+
pub(super) fn item_to_macro_call(&mut self, src: InFile<ast::Item>) -> Option<MacroCallId> {
149+
let map = self.dyn_map(src.as_ref())?;
150+
map[keys::ATTR_MACRO].get(&src).copied()
151+
}
152+
148153
fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
149154
&mut self,
150155
src: InFile<Ast>,
151156
key: Key<Ast, ID>,
152157
) -> Option<ID> {
153-
let container = self.find_container(src.as_ref().map(|it| it.syntax()))?;
158+
self.dyn_map(src.as_ref())?[key].get(&src).copied()
159+
}
160+
161+
fn dyn_map<Ast: AstNode + 'static>(&mut self, src: InFile<&Ast>) -> Option<&DynMap> {
162+
let container = self.find_container(src.map(|it| it.syntax()))?;
154163
let db = self.db;
155164
let dyn_map =
156165
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
157-
dyn_map[key].get(&src).copied()
166+
Some(dyn_map)
158167
}
159168

160169
pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {

crates/hir_def/src/child_by_source.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ impl ChildBySource for ItemScope {
8585
res[keys::CONST].insert(src, konst);
8686
});
8787
self.impls().for_each(|imp| add_impl(db, res, imp));
88+
self.attr_macro_invocs().for_each(|(ast_id, call_id)| {
89+
let item = ast_id.with_value(ast_id.to_node(db.upcast()));
90+
res[keys::ATTR_MACRO].insert(item, call_id);
91+
});
8892

8993
fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) {
9094
match item {

crates/hir_def/src/item_scope.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
use std::collections::hash_map::Entry;
55

66
use base_db::CrateId;
7-
use hir_expand::name::Name;
8-
use hir_expand::MacroDefKind;
7+
use hir_expand::{name::Name, AstId, MacroCallId, MacroDefKind};
98
use once_cell::sync::Lazy;
109
use rustc_hash::{FxHashMap, FxHashSet};
1110
use stdx::format_to;
11+
use syntax::ast;
1212

1313
use crate::{
1414
db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId, ImplId,
@@ -53,6 +53,7 @@ pub struct ItemScope {
5353
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
5454
// be all resolved to the last one defined if shadowing happens.
5555
legacy_macros: FxHashMap<Name, MacroDefId>,
56+
attr_macros: FxHashMap<AstId<ast::Item>, MacroCallId>,
5657
}
5758

5859
pub(crate) static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
@@ -169,6 +170,16 @@ impl ItemScope {
169170
self.legacy_macros.insert(name, mac);
170171
}
171172

173+
pub(crate) fn add_attr_macro_invoc(&mut self, item: AstId<ast::Item>, call: MacroCallId) {
174+
self.attr_macros.insert(item, call);
175+
}
176+
177+
pub(crate) fn attr_macro_invocs(
178+
&self,
179+
) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
180+
self.attr_macros.iter().map(|(k, v)| (*k, *v))
181+
}
182+
172183
pub(crate) fn unnamed_trait_vis(&self, tr: TraitId) -> Option<Visibility> {
173184
self.unnamed_trait_imports.get(&tr).copied()
174185
}
@@ -307,6 +318,7 @@ impl ItemScope {
307318
unnamed_consts,
308319
unnamed_trait_imports,
309320
legacy_macros,
321+
attr_macros,
310322
} = self;
311323
types.shrink_to_fit();
312324
values.shrink_to_fit();
@@ -317,6 +329,7 @@ impl ItemScope {
317329
unnamed_consts.shrink_to_fit();
318330
unnamed_trait_imports.shrink_to_fit();
319331
legacy_macros.shrink_to_fit();
332+
attr_macros.shrink_to_fit();
320333
}
321334
}
322335

crates/hir_def/src/keys.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::marker::PhantomData;
44

5-
use hir_expand::{InFile, MacroDefId};
5+
use hir_expand::{InFile, MacroCallId, MacroDefId};
66
use rustc_hash::FxHashMap;
77
use syntax::{ast, AstNode, AstPtr};
88

@@ -32,6 +32,7 @@ pub const LIFETIME_PARAM: Key<ast::LifetimeParam, LifetimeParamId> = Key::new();
3232
pub const CONST_PARAM: Key<ast::ConstParam, ConstParamId> = Key::new();
3333

3434
pub const MACRO: Key<ast::MacroCall, MacroDefId> = Key::new();
35+
pub const ATTR_MACRO: Key<ast::Item, MacroCallId> = Key::new();
3536

3637
/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
3738
/// equal if they point to exactly the same object.

crates/hir_def/src/nameres/collector.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,11 @@ impl DefCollector<'_> {
11121112
return false;
11131113
}
11141114
}
1115+
1116+
self.def_map.modules[directive.module_id]
1117+
.scope
1118+
.add_attr_macro_invoc(ast_id.ast_id, call_id);
1119+
11151120
resolved.push((directive.module_id, call_id, directive.depth));
11161121
res = ReachedFixedPoint::No;
11171122
return false;

0 commit comments

Comments
 (0)