From da57f5dc17303cfd5ba318d1735c7f325f6b7130 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 1 Feb 2021 13:32:43 +0100 Subject: Shortcut `block_def_map` if there's no inner items This previously didn't work, but apparently only because of the wonky test setup --- crates/hir_def/src/body/lower.rs | 9 ++++++--- crates/hir_def/src/body/tests.rs | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'crates/hir_def/src/body') diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index bc61730a7..540c6c9ad 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -700,10 +700,13 @@ impl ExprCollector<'_> { let ast_id = self.expander.ast_id(&block); let block_loc = BlockLoc { ast_id, module: self.expander.module }; let block_id = self.db.intern_block(block_loc); - let def_map = self.db.block_def_map(block_id); - let root = def_map.module_id(def_map.root()); + let opt_def_map = self.db.block_def_map(block_id); + let has_def_map = opt_def_map.is_some(); + let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone()); + let module = + if has_def_map { def_map.module_id(def_map.root()) } else { self.expander.module }; let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); - let prev_module = mem::replace(&mut self.expander.module, root); + let prev_module = mem::replace(&mut self.expander.module, module); self.collect_stmts_items(block.statements()); let statements = diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index da60072ce..404603360 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs @@ -43,7 +43,7 @@ fn block_def_map_at(ra_fixture: &str) -> Arc { let mut block = block_at_pos(&db, &def_map, position).expect("couldn't find enclosing function or block"); loop { - let def_map = db.block_def_map(block); + let def_map = db.block_def_map(block).unwrap_or_else(|| def_map.clone()); let new_block = block_at_pos(&db, &def_map, position); match new_block { Some(new_block) => { @@ -58,6 +58,7 @@ fn block_def_map_at(ra_fixture: &str) -> Arc { } fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) -> Option { + // Find the smallest (innermost) function containing the cursor. let mut size = None; let mut fn_def = None; for (_, module) in def_map.modules() { @@ -73,7 +74,6 @@ fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root); let range = ast.syntax().text_range(); - // Find the smallest (innermost) function containing the cursor. if !range.contains(position.offset) { continue; } -- cgit v1.2.3