fix source_to_def trying to use attribute macro calls as containers

This commit is contained in:
Lukas Wirth 2021-09-14 14:41:38 +02:00
parent d1e489185f
commit 0f4463e45e
4 changed files with 44 additions and 7 deletions

View file

@ -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| {

View file

@ -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(),

View file

@ -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> {

View file

@ -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">&lt;</span><span class="type_param declaration">Args</span><span class="angle">&gt;</span> <span class="brace">{</span><span class="brace">}</span>