aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/body
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/body')
-rw-r--r--crates/hir_def/src/body/lower.rs9
-rw-r--r--crates/hir_def/src/body/tests.rs4
2 files changed, 8 insertions, 5 deletions
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<'_> {
700 let ast_id = self.expander.ast_id(&block); 700 let ast_id = self.expander.ast_id(&block);
701 let block_loc = BlockLoc { ast_id, module: self.expander.module }; 701 let block_loc = BlockLoc { ast_id, module: self.expander.module };
702 let block_id = self.db.intern_block(block_loc); 702 let block_id = self.db.intern_block(block_loc);
703 let def_map = self.db.block_def_map(block_id); 703 let opt_def_map = self.db.block_def_map(block_id);
704 let root = def_map.module_id(def_map.root()); 704 let has_def_map = opt_def_map.is_some();
705 let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone());
706 let module =
707 if has_def_map { def_map.module_id(def_map.root()) } else { self.expander.module };
705 let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); 708 let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
706 let prev_module = mem::replace(&mut self.expander.module, root); 709 let prev_module = mem::replace(&mut self.expander.module, module);
707 710
708 self.collect_stmts_items(block.statements()); 711 self.collect_stmts_items(block.statements());
709 let statements = 712 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<DefMap> {
43 let mut block = 43 let mut block =
44 block_at_pos(&db, &def_map, position).expect("couldn't find enclosing function or block"); 44 block_at_pos(&db, &def_map, position).expect("couldn't find enclosing function or block");
45 loop { 45 loop {
46 let def_map = db.block_def_map(block); 46 let def_map = db.block_def_map(block).unwrap_or_else(|| def_map.clone());
47 let new_block = block_at_pos(&db, &def_map, position); 47 let new_block = block_at_pos(&db, &def_map, position);
48 match new_block { 48 match new_block {
49 Some(new_block) => { 49 Some(new_block) => {
@@ -58,6 +58,7 @@ fn block_def_map_at(ra_fixture: &str) -> Arc<DefMap> {
58} 58}
59 59
60fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) -> Option<BlockId> { 60fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) -> Option<BlockId> {
61 // Find the smallest (innermost) function containing the cursor.
61 let mut size = None; 62 let mut size = None;
62 let mut fn_def = None; 63 let mut fn_def = None;
63 for (_, module) in def_map.modules() { 64 for (_, module) in def_map.modules() {
@@ -73,7 +74,6 @@ fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition)
73 let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root); 74 let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root);
74 let range = ast.syntax().text_range(); 75 let range = ast.syntax().text_range();
75 76
76 // Find the smallest (innermost) function containing the cursor.
77 if !range.contains(position.offset) { 77 if !range.contains(position.offset) {
78 continue; 78 continue;
79 } 79 }