]> git.lizzy.rs Git - rust.git/commitdiff
Fix incorrect `FileId` and remove broken shortcut
authorJonas Schievink <jonasschievink@gmail.com>
Thu, 28 Jan 2021 18:33:00 +0000 (19:33 +0100)
committerJonas Schievink <jonasschievink@gmail.com>
Thu, 28 Jan 2021 18:33:00 +0000 (19:33 +0100)
Apparently we were using the crate's root file instead of the file
containing the block.

crates/hir_def/src/nameres.rs
crates/hir_def/src/nameres/collector.rs

index 005b36e02887ef244ba9808bac5b2eac08e3775d..6169b3bbcbdd851a000bb8bc51f243dcfb400008 100644 (file)
@@ -199,16 +199,10 @@ pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<D
 
     pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Arc<DefMap> {
         let block: BlockLoc = db.lookup_intern_block(block_id);
-        let item_tree = db.item_tree(block.ast_id.file_id);
-        let block_items = item_tree.inner_items_of_block(block.ast_id.value);
-
         let parent = block.module.def_map(db);
 
-        if block_items.is_empty() {
-            // If there are no inner items, nothing new is brought into scope, so we can just return
-            // the parent DefMap. This keeps DefMap parent chains short.
-            return parent;
-        }
+        // FIXME: It would be good to just return the parent map when the block has no items, but
+        // we rely on `def_map.block` in a few places, which is `Some` for the inner `DefMap`.
 
         let block_info =
             BlockInfo { block: block_id, parent, parent_module: block.module.local_id };
@@ -216,7 +210,7 @@ pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Ar
         let mut def_map = DefMap::empty(block.module.krate, block_info.parent.edition);
         def_map.block = Some(block_info);
 
-        let def_map = collector::collect_defs(db, def_map, Some(block.ast_id.value));
+        let def_map = collector::collect_defs(db, def_map, Some(block.ast_id));
         Arc::new(def_map)
     }
 
index 761b29c868c06728014821ec2be1640400992d82..ae98fadac29785514ffdb72376f97fccb12588d1 100644 (file)
@@ -48,7 +48,7 @@
 pub(super) fn collect_defs(
     db: &dyn DefDatabase,
     mut def_map: DefMap,
-    block: Option<FileAstId<ast::BlockExpr>>,
+    block: Option<AstId<ast::BlockExpr>>,
 ) -> DefMap {
     let crate_graph = db.crate_graph();
 
@@ -261,11 +261,10 @@ fn seed_with_top_level(&mut self) {
         }
     }
 
-    fn seed_with_inner(&mut self, block: FileAstId<ast::BlockExpr>) {
-        let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id;
-        let item_tree = self.db.item_tree(file_id.into());
+    fn seed_with_inner(&mut self, block: AstId<ast::BlockExpr>) {
+        let item_tree = self.db.item_tree(block.file_id);
         let module_id = self.def_map.root;
-        self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
+        self.def_map.modules[module_id].origin = ModuleOrigin::BlockExpr { block };
         if item_tree
             .top_level_attrs(self.db, self.def_map.krate)
             .cfg()
@@ -275,11 +274,11 @@ fn seed_with_inner(&mut self, block: FileAstId<ast::BlockExpr>) {
                 def_collector: &mut *self,
                 macro_depth: 0,
                 module_id,
-                file_id: file_id.into(),
+                file_id: block.file_id,
                 item_tree: &item_tree,
                 mod_dir: ModDir::root(),
             }
-            .collect(item_tree.inner_items_of_block(block));
+            .collect(item_tree.inner_items_of_block(block.value));
         }
     }