Unnamed repository; edit this file 'description' to name the repository.
fix source_to_def trying to use attribute macro calls as containers
| -rw-r--r-- | crates/hir/src/semantics.rs | 1 | ||||
| -rw-r--r-- | crates/hir/src/semantics/source_to_def.rs | 17 | ||||
| -rw-r--r-- | crates/hir_expand/src/lib.rs | 31 | ||||
| -rw-r--r-- | crates/ide/src/syntax_highlighting/test_data/highlighting.html | 2 |
4 files changed, 44 insertions, 7 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index e4b9f479e8..1aa0d61fe6 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -569,7 +569,6 @@ impl<'db> SemanticsImpl<'db> { node: &SyntaxNode, offset: TextSize, ) -> impl Iterator<Item = impl Iterator<Item = SyntaxNode> + '_> + '_ { - // Handle macro token cases node.token_at_offset(offset) .map(move |token| self.descend_into_macros(token)) .map(|descendants| { diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 1f80fd3404..723c7a1727 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -131,8 +131,12 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> { let _p = profile::span("module_to_def"); - let parent_declaration = - src.syntax().cloned().ancestors_with_macros(self.db.upcast()).skip(1).find_map(|it| { + let parent_declaration = src + .syntax() + .cloned() + .ancestors_with_macros_skip_attr_item(self.db.upcast()) + .skip(1) + .find_map(|it| { let m = ast::Module::cast(it.value.clone())?; Some(it.with_value(m)) }); @@ -306,7 +310,8 @@ impl SourceToDefCtx<'_, '_> { } pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> { - for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { + for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1) + { if let Some(res) = self.container_to_def(container) { return Some(res); } @@ -370,7 +375,8 @@ impl SourceToDefCtx<'_, '_> { } fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> { - for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { + for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1) + { let res: GenericDefId = match_ast! { match (container.value) { ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), @@ -388,7 +394,8 @@ impl SourceToDefCtx<'_, '_> { } fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> { - for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { + for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1) + { let res: DefWithBodyId = match_ast! { match (container.value) { ast::Const(it) => self.const_to_def(container.with_value(it))?.into(), diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index b666cf8dda..8bb56e0700 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -186,6 +186,17 @@ impl HirFileId { } } + /// Return whether this file is an include macro + pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool { + match self.0 { + HirFileIdRepr::MacroFile(macro_file) => { + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + matches!(loc.kind, MacroCallKind::Attr { .. }) + } + _ => false, + } + } + pub fn is_macro(self) -> bool { matches!(self.0, HirFileIdRepr::MacroFile(_)) } @@ -534,6 +545,26 @@ impl InFile<SyntaxNode> { } }) } + + /// Skips the attributed item that caused the macro invocation we are climbing up + pub fn ancestors_with_macros_skip_attr_item( + self, + db: &dyn db::AstDatabase, + ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { + iter::successors(Some(self), move |node| match node.value.parent() { + Some(parent) => Some(node.with_value(parent)), + None => { + let parent_node = node.file_id.call_node(db)?; + if node.file_id.is_attr_macro(db) { + // macro call was an attributed item, skip it + // FIXME: does this fail if this is a direct expansion of another macro? + parent_node.map(|node| node.parent()).transpose() + } else { + Some(parent_node) + } + } + }) + } } impl<'a> InFile<&'a SyntaxNode> { diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 80533d1faa..448b30aa84 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html @@ -52,7 +52,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd <span class="brace">}</span> <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> -<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">ops</span> <span class="brace">{</span> +<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration public">ops</span> <span class="brace">{</span> <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> <span class="keyword">pub</span> <span class="keyword">trait</span> <span class="trait declaration public">FnOnce</span><span class="angle"><</span><span class="type_param declaration">Args</span><span class="angle">></span> <span class="brace">{</span><span class="brace">}</span> |