]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_def/src/item_tree.rs
Use block_def_map in body lowering
[rust.git] / crates / hir_def / src / item_tree.rs
index 1e5c9466042ae481cfe892f129efbee8f579e179..4bde676490ec2a613a3cc548dddf0e08b6f790bc 100644 (file)
@@ -24,7 +24,7 @@
 use profile::Count;
 use rustc_hash::FxHashMap;
 use smallvec::SmallVec;
-use syntax::{ast, match_ast};
+use syntax::{ast, match_ast, SyntaxKind};
 use test_utils::mark;
 
 use crate::{
@@ -80,6 +80,10 @@ impl ItemTree {
     pub(crate) fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
         let _p = profile::span("item_tree_query").detail(|| format!("{:?}", file_id));
         let syntax = if let Some(node) = db.parse_or_expand(file_id) {
+            if node.kind() == SyntaxKind::ERROR {
+                // FIXME: not 100% sure why these crop up, but return an empty tree to avoid a panic
+                return Default::default();
+            }
             node
         } else {
             return Default::default();
@@ -98,15 +102,17 @@ pub(crate) fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<I
                     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);
                 },
             }
         };