From 0f4463e45e4afc9537ed4ce7d8acc5b2d9563d56 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 14 Sep 2021 14:41:38 +0200 Subject: [PATCH] fix source_to_def trying to use attribute macro calls as containers --- crates/hir/src/semantics.rs | 1 - crates/hir/src/semantics/source_to_def.rs | 17 +++++++--- crates/hir_expand/src/lib.rs | 31 +++++++++++++++++++ .../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 e4b9f479e86..1aa0d61fe61 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 + '_> + '_ { - // 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 1f80fd3404e..723c7a1727b 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) -> Option { 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 { - 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 { - 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 { - 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 b666cf8dda2..8bb56e0700d 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 { } }) } + + /// 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> + '_ { + 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 80533d1faaa..448b30aa849 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 } #[proc_macros::identity] -pub mod ops { +pub mod ops { #[lang = "fn_once"] pub trait FnOnce<Args> {}