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

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