Skip to content

Commit 0f4463e

Browse files
committed
fix source_to_def trying to use attribute macro calls as containers
1 parent d1e4891 commit 0f4463e

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

crates/hir/src/semantics.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,6 @@ impl<'db> SemanticsImpl<'db> {
569569
node: &SyntaxNode,
570570
offset: TextSize,
571571
) -> impl Iterator<Item = impl Iterator<Item = SyntaxNode> + '_> + '_ {
572-
// Handle macro token cases
573572
node.token_at_offset(offset)
574573
.map(move |token| self.descend_into_macros(token))
575574
.map(|descendants| {

crates/hir/src/semantics/source_to_def.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ impl SourceToDefCtx<'_, '_> {
131131

132132
pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
133133
let _p = profile::span("module_to_def");
134-
let parent_declaration =
135-
src.syntax().cloned().ancestors_with_macros(self.db.upcast()).skip(1).find_map(|it| {
134+
let parent_declaration = src
135+
.syntax()
136+
.cloned()
137+
.ancestors_with_macros_skip_attr_item(self.db.upcast())
138+
.skip(1)
139+
.find_map(|it| {
136140
let m = ast::Module::cast(it.value.clone())?;
137141
Some(it.with_value(m))
138142
});
@@ -306,7 +310,8 @@ impl SourceToDefCtx<'_, '_> {
306310
}
307311

308312
pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
309-
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
313+
for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
314+
{
310315
if let Some(res) = self.container_to_def(container) {
311316
return Some(res);
312317
}
@@ -370,7 +375,8 @@ impl SourceToDefCtx<'_, '_> {
370375
}
371376

372377
fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
373-
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
378+
for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
379+
{
374380
let res: GenericDefId = match_ast! {
375381
match (container.value) {
376382
ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
@@ -388,7 +394,8 @@ impl SourceToDefCtx<'_, '_> {
388394
}
389395

390396
fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
391-
for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
397+
for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
398+
{
392399
let res: DefWithBodyId = match_ast! {
393400
match (container.value) {
394401
ast::Const(it) => self.const_to_def(container.with_value(it))?.into(),

crates/hir_expand/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ impl HirFileId {
186186
}
187187
}
188188

189+
/// Return whether this file is an include macro
190+
pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool {
191+
match self.0 {
192+
HirFileIdRepr::MacroFile(macro_file) => {
193+
let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
194+
matches!(loc.kind, MacroCallKind::Attr { .. })
195+
}
196+
_ => false,
197+
}
198+
}
199+
189200
pub fn is_macro(self) -> bool {
190201
matches!(self.0, HirFileIdRepr::MacroFile(_))
191202
}
@@ -534,6 +545,26 @@ impl InFile<SyntaxNode> {
534545
}
535546
})
536547
}
548+
549+
/// Skips the attributed item that caused the macro invocation we are climbing up
550+
pub fn ancestors_with_macros_skip_attr_item(
551+
self,
552+
db: &dyn db::AstDatabase,
553+
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
554+
iter::successors(Some(self), move |node| match node.value.parent() {
555+
Some(parent) => Some(node.with_value(parent)),
556+
None => {
557+
let parent_node = node.file_id.call_node(db)?;
558+
if node.file_id.is_attr_macro(db) {
559+
// macro call was an attributed item, skip it
560+
// FIXME: does this fail if this is a direct expansion of another macro?
561+
parent_node.map(|node| node.parent()).transpose()
562+
} else {
563+
Some(parent_node)
564+
}
565+
}
566+
})
567+
}
537568
}
538569

539570
impl<'a> InFile<&'a SyntaxNode> {

crates/ide/src/syntax_highlighting/test_data/highlighting.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<span class="brace">}</span>
5353

5454
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="module attribute">proc_macros</span><span class="operator attribute">::</span><span class="builtin_attr attribute">identity</span><span class="attribute attribute">]</span>
55-
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">ops</span> <span class="brace">{</span>
55+
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration public">ops</span> <span class="brace">{</span>
5656
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="builtin_attr attribute">lang</span> <span class="operator attribute">=</span> <span class="string_literal attribute">"fn_once"</span><span class="attribute attribute">]</span>
5757
<span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">FnOnce</span><span class="angle">&lt;</span><span class="type_param declaration">Args</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="brace">}</span>
5858

0 commit comments

Comments
 (0)