7480: ItemTree: Treat items in `MacroStmts` as top-level r=jonas-schievink a=jonas-schievink

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-01-28 17:02:24 +00:00 committed by GitHub
commit 868887ec4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View file

@ -98,15 +98,17 @@ impl ItemTree {
ctx.lower_module_items(&items)
},
ast::MacroStmts(stmts) => {
ctx.lower_inner_items(stmts.syntax())
// The produced statements can include items, which should be added as top-level
// items.
ctx.lower_macro_stmts(stmts)
},
// Macros can expand to expressions. We return an empty item tree in this case, but
// still need to collect inner items.
ast::Expr(e) => {
// Macros can expand to expressions. We return an empty item tree in this case, but
// still need to collect inner items.
ctx.lower_inner_items(e.syntax())
},
_ => {
panic!("cannot create item tree from {:?}", syntax);
panic!("cannot create item tree from {:?} {}", syntax, syntax);
},
}
};

View file

@ -61,6 +61,32 @@ impl Ctx {
self.tree
}
pub(super) fn lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree {
self.tree.top_level = stmts
.statements()
.filter_map(|stmt| match stmt {
ast::Stmt::Item(item) => Some(item),
_ => None,
})
.flat_map(|item| self.lower_mod_item(&item, false))
.flat_map(|items| items.0)
.collect();
// Non-items need to have their inner items collected.
for stmt in stmts.statements() {
match stmt {
ast::Stmt::ExprStmt(_) | ast::Stmt::LetStmt(_) => {
self.collect_inner_items(stmt.syntax())
}
_ => {}
}
}
if let Some(expr) = stmts.expr() {
self.collect_inner_items(expr.syntax());
}
self.tree
}
pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree {
self.collect_inner_items(within);
self.tree