From 090b2f0e50b48cd22ac48399bd3c4d17c3e92765 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 28 Jan 2021 19:33:00 +0100 Subject: Fix incorrect `FileId` and remove broken shortcut Apparently we were using the crate's root file instead of the file containing the block. --- crates/hir_def/src/nameres.rs | 12 +++--------- crates/hir_def/src/nameres/collector.rs | 13 ++++++------- 2 files changed, 9 insertions(+), 16 deletions(-) (limited to 'crates/hir_def/src') diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 005b36e02..6169b3bbc 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -199,16 +199,10 @@ impl DefMap { pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Arc { 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 @@ impl DefMap { 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) } diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 761b29c86..ae98fadac 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -48,7 +48,7 @@ const FIXED_POINT_LIMIT: usize = 8192; pub(super) fn collect_defs( db: &dyn DefDatabase, mut def_map: DefMap, - block: Option>, + block: Option>, ) -> DefMap { let crate_graph = db.crate_graph(); @@ -261,11 +261,10 @@ impl DefCollector<'_> { } } - fn seed_with_inner(&mut self, block: FileAstId) { - 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) { + 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 @@ impl DefCollector<'_> { 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)); } } -- cgit v1.2.3