aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-02-03 14:41:13 +0000
committerGitHub <[email protected]>2021-02-03 14:41:13 +0000
commitfd84df9e1bb231f7aa4bcf760e0aff0a6bd10e9f (patch)
tree57fbfe241dcd571b9376b23260299160e0312246
parent93ecef53a370703a67f87b90c4640d3e8bf73934 (diff)
parent63744fe128193464eb0ce63fbe6c30c4f98b6135 (diff)
Merge #7541
7541: Use block_def_map in body lowering (third time's the charm) r=jonas-schievink a=jonas-schievink After https://github.com/rust-analyzer/rust-analyzer/pull/7380 and https://github.com/rust-analyzer/rust-analyzer/pull/7506 both had to be reverted, this should have finally resolved all remaining bugs. Most importantly, the optimization to skip `block_def_map` computation when the block contains no inner items was fixed (which fortunately was simpler than expected). I've ran `analysis-stats` on libstd locally, which works fine, and also ran this PR locally for a short while without issues. Note that this *still* has no (or almost no) user-facing impact, because the rest of r-a still relies on some local item support hacks. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
-rw-r--r--crates/hir_def/src/body.rs19
-rw-r--r--crates/hir_def/src/body/lower.rs38
-rw-r--r--crates/hir_def/src/body/tests.rs116
-rw-r--r--crates/hir_def/src/body/tests/block.rs (renamed from crates/hir_def/src/nameres/tests/block.rs)34
-rw-r--r--crates/hir_def/src/data.rs2
-rw-r--r--crates/hir_def/src/db.rs2
-rw-r--r--crates/hir_def/src/expr.rs2
-rw-r--r--crates/hir_def/src/item_tree.rs6
-rw-r--r--crates/hir_def/src/lib.rs9
-rw-r--r--crates/hir_def/src/nameres.rs13
-rw-r--r--crates/hir_def/src/nameres/tests.rs68
-rw-r--r--crates/hir_ty/src/infer/expr.rs2
12 files changed, 211 insertions, 100 deletions
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs
index b9ecf22fa..41abd8f83 100644
--- a/crates/hir_def/src/body.rs
+++ b/crates/hir_def/src/body.rs
@@ -46,7 +46,7 @@ pub(crate) struct CfgExpander {
46 46
47pub(crate) struct Expander { 47pub(crate) struct Expander {
48 cfg_expander: CfgExpander, 48 cfg_expander: CfgExpander,
49 crate_def_map: Arc<DefMap>, 49 def_map: Arc<DefMap>,
50 current_file_id: HirFileId, 50 current_file_id: HirFileId,
51 ast_id_map: Arc<AstIdMap>, 51 ast_id_map: Arc<AstIdMap>,
52 module: ModuleId, 52 module: ModuleId,
@@ -91,7 +91,7 @@ impl Expander {
91 let ast_id_map = db.ast_id_map(current_file_id); 91 let ast_id_map = db.ast_id_map(current_file_id);
92 Expander { 92 Expander {
93 cfg_expander, 93 cfg_expander,
94 crate_def_map, 94 def_map: crate_def_map,
95 current_file_id, 95 current_file_id,
96 ast_id_map, 96 ast_id_map,
97 module, 97 module,
@@ -102,7 +102,6 @@ impl Expander {
102 pub(crate) fn enter_expand<T: ast::AstNode>( 102 pub(crate) fn enter_expand<T: ast::AstNode>(
103 &mut self, 103 &mut self,
104 db: &dyn DefDatabase, 104 db: &dyn DefDatabase,
105 local_scope: Option<&ItemScope>,
106 macro_call: ast::MacroCall, 105 macro_call: ast::MacroCall,
107 ) -> ExpandResult<Option<(Mark, T)>> { 106 ) -> ExpandResult<Option<(Mark, T)>> {
108 if self.recursion_limit + 1 > EXPANSION_RECURSION_LIMIT { 107 if self.recursion_limit + 1 > EXPANSION_RECURSION_LIMIT {
@@ -112,18 +111,12 @@ impl Expander {
112 111
113 let macro_call = InFile::new(self.current_file_id, &macro_call); 112 let macro_call = InFile::new(self.current_file_id, &macro_call);
114 113
115 let resolver = |path: ModPath| -> Option<MacroDefId> { 114 let resolver =
116 if let Some(local_scope) = local_scope { 115 |path: ModPath| -> Option<MacroDefId> { self.resolve_path_as_macro(db, &path) };
117 if let Some(def) = path.as_ident().and_then(|n| local_scope.get_legacy_macro(n)) {
118 return Some(def);
119 }
120 }
121 self.resolve_path_as_macro(db, &path)
122 };
123 116
124 let mut err = None; 117 let mut err = None;
125 let call_id = 118 let call_id =
126 macro_call.as_call_id_with_errors(db, self.crate_def_map.krate(), resolver, &mut |e| { 119 macro_call.as_call_id_with_errors(db, self.def_map.krate(), resolver, &mut |e| {
127 err.get_or_insert(e); 120 err.get_or_insert(e);
128 }); 121 });
129 let call_id = match call_id { 122 let call_id = match call_id {
@@ -204,7 +197,7 @@ impl Expander {
204 } 197 }
205 198
206 fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> { 199 fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> {
207 self.crate_def_map 200 self.def_map
208 .resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other) 201 .resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other)
209 .0 202 .0
210 .take_macros() 203 .take_macros()
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs
index 209965fca..540c6c9ad 100644
--- a/crates/hir_def/src/body/lower.rs
+++ b/crates/hir_def/src/body/lower.rs
@@ -1,7 +1,7 @@
1//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` 1//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
2//! representation. 2//! representation.
3 3
4use std::{any::type_name, sync::Arc}; 4use std::{any::type_name, mem, sync::Arc};
5 5
6use either::Either; 6use either::Either;
7use hir_expand::{ 7use hir_expand::{
@@ -36,8 +36,8 @@ use crate::{
36 item_tree::{ItemTree, ItemTreeId, ItemTreeNode}, 36 item_tree::{ItemTree, ItemTreeId, ItemTreeNode},
37 path::{GenericArgs, Path}, 37 path::{GenericArgs, Path},
38 type_ref::{Mutability, Rawness, TypeRef}, 38 type_ref::{Mutability, Rawness, TypeRef},
39 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, 39 AdtId, BlockLoc, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern,
40 StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, 40 ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
41}; 41};
42 42
43use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; 43use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
@@ -152,8 +152,8 @@ impl ExprCollector<'_> {
152 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { 152 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
153 self.make_expr(expr, Err(SyntheticSyntax)) 153 self.make_expr(expr, Err(SyntheticSyntax))
154 } 154 }
155 fn empty_block(&mut self) -> ExprId { 155 fn unit(&mut self) -> ExprId {
156 self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None, label: None }) 156 self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() })
157 } 157 }
158 fn missing_expr(&mut self) -> ExprId { 158 fn missing_expr(&mut self) -> ExprId {
159 self.alloc_expr_desugared(Expr::Missing) 159 self.alloc_expr_desugared(Expr::Missing)
@@ -222,7 +222,7 @@ impl ExprCollector<'_> {
222 MatchArm { pat, expr: then_branch, guard: None }, 222 MatchArm { pat, expr: then_branch, guard: None },
223 MatchArm { 223 MatchArm {
224 pat: placeholder_pat, 224 pat: placeholder_pat,
225 expr: else_branch.unwrap_or_else(|| self.empty_block()), 225 expr: else_branch.unwrap_or_else(|| self.unit()),
226 guard: None, 226 guard: None,
227 }, 227 },
228 ]; 228 ];
@@ -561,7 +561,7 @@ impl ExprCollector<'_> {
561 let outer_file = self.expander.current_file_id; 561 let outer_file = self.expander.current_file_id;
562 562
563 let macro_call = self.expander.to_source(AstPtr::new(&e)); 563 let macro_call = self.expander.to_source(AstPtr::new(&e));
564 let res = self.expander.enter_expand(self.db, Some(&self.body.item_scope), e); 564 let res = self.expander.enter_expand(self.db, e);
565 565
566 match &res.err { 566 match &res.err {
567 Some(ExpandError::UnresolvedProcMacro) => { 567 Some(ExpandError::UnresolvedProcMacro) => {
@@ -697,12 +697,30 @@ impl ExprCollector<'_> {
697 } 697 }
698 698
699 fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId { 699 fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
700 let syntax_node_ptr = AstPtr::new(&block.clone().into()); 700 let ast_id = self.expander.ast_id(&block);
701 let block_loc = BlockLoc { ast_id, module: self.expander.module };
702 let block_id = self.db.intern_block(block_loc);
703 let opt_def_map = self.db.block_def_map(block_id);
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 };
708 let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
709 let prev_module = mem::replace(&mut self.expander.module, module);
710
701 self.collect_stmts_items(block.statements()); 711 self.collect_stmts_items(block.statements());
702 let statements = 712 let statements =
703 block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect(); 713 block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
704 let tail = block.tail_expr().map(|e| self.collect_expr(e)); 714 let tail = block.tail_expr().map(|e| self.collect_expr(e));
705 self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr) 715 let syntax_node_ptr = AstPtr::new(&block.clone().into());
716 let expr_id = self.alloc_expr(
717 Expr::Block { id: block_id, statements, tail, label: None },
718 syntax_node_ptr,
719 );
720
721 self.expander.def_map = prev_def_map;
722 self.expander.module = prev_module;
723 expr_id
706 } 724 }
707 725
708 fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) { 726 fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) {
@@ -832,7 +850,7 @@ impl ExprCollector<'_> {
832 if annotation == BindingAnnotation::Unannotated && subpat.is_none() { 850 if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
833 // This could also be a single-segment path pattern. To 851 // This could also be a single-segment path pattern. To
834 // decide that, we need to try resolving the name. 852 // decide that, we need to try resolving the name.
835 let (resolved, _) = self.expander.crate_def_map.resolve_path( 853 let (resolved, _) = self.expander.def_map.resolve_path(
836 self.db, 854 self.db,
837 self.expander.module.local_id, 855 self.expander.module.local_id,
838 &name.clone().into(), 856 &name.clone().into(),
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs
index 2e5d0a01e..404603360 100644
--- a/crates/hir_def/src/body/tests.rs
+++ b/crates/hir_def/src/body/tests.rs
@@ -1,7 +1,10 @@
1use base_db::{fixture::WithFixture, SourceDatabase}; 1mod block;
2
3use base_db::{fixture::WithFixture, FilePosition, SourceDatabase};
4use expect_test::Expect;
2use test_utils::mark; 5use test_utils::mark;
3 6
4use crate::{test_db::TestDB, ModuleDefId}; 7use crate::{test_db::TestDB, BlockId, ModuleDefId};
5 8
6use super::*; 9use super::*;
7 10
@@ -31,6 +34,115 @@ fn check_diagnostics(ra_fixture: &str) {
31 db.check_diagnostics(); 34 db.check_diagnostics();
32} 35}
33 36
37fn block_def_map_at(ra_fixture: &str) -> Arc<DefMap> {
38 let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
39
40 let krate = db.crate_graph().iter().next().unwrap();
41 let def_map = db.crate_def_map(krate);
42
43 let mut block =
44 block_at_pos(&db, &def_map, position).expect("couldn't find enclosing function or block");
45 loop {
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);
48 match new_block {
49 Some(new_block) => {
50 assert_ne!(block, new_block);
51 block = new_block;
52 }
53 None => {
54 return def_map;
55 }
56 }
57 }
58}
59
60fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) -> Option<BlockId> {
61 // Find the smallest (innermost) function containing the cursor.
62 let mut size = None;
63 let mut fn_def = None;
64 for (_, module) in def_map.modules() {
65 let file_id = module.definition_source(db).file_id;
66 if file_id != position.file_id.into() {
67 continue;
68 }
69 let root = db.parse_or_expand(file_id).unwrap();
70 let ast_map = db.ast_id_map(file_id);
71 let item_tree = db.item_tree(file_id);
72 for decl in module.scope.declarations() {
73 if let ModuleDefId::FunctionId(it) = decl {
74 let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root);
75 let range = ast.syntax().text_range();
76
77 if !range.contains(position.offset) {
78 continue;
79 }
80
81 let new_size = match size {
82 None => range.len(),
83 Some(size) => {
84 if range.len() < size {
85 range.len()
86 } else {
87 size
88 }
89 }
90 };
91 if size != Some(new_size) {
92 size = Some(new_size);
93 fn_def = Some(it);
94 }
95 }
96 }
97 }
98
99 let (body, source_map) = db.body_with_source_map(fn_def?.into());
100
101 // Now find the smallest encompassing block expression in the function body.
102 let mut size = None;
103 let mut block_id = None;
104 for (expr_id, expr) in body.exprs.iter() {
105 if let Expr::Block { id, .. } = expr {
106 if let Ok(ast) = source_map.expr_syntax(expr_id) {
107 if ast.file_id != position.file_id.into() {
108 continue;
109 }
110
111 let root = db.parse_or_expand(ast.file_id).unwrap();
112 let ast = ast.value.to_node(&root);
113 let range = ast.syntax().text_range();
114
115 if !range.contains(position.offset) {
116 continue;
117 }
118
119 let new_size = match size {
120 None => range.len(),
121 Some(size) => {
122 if range.len() < size {
123 range.len()
124 } else {
125 size
126 }
127 }
128 };
129 if size != Some(new_size) {
130 size = Some(new_size);
131 block_id = Some(*id);
132 }
133 }
134 }
135 }
136
137 Some(block_id.expect("can't find block containing cursor"))
138}
139
140fn check_at(ra_fixture: &str, expect: Expect) {
141 let def_map = block_def_map_at(ra_fixture);
142 let actual = def_map.dump();
143 expect.assert_eq(&actual);
144}
145
34#[test] 146#[test]
35fn your_stack_belongs_to_me() { 147fn your_stack_belongs_to_me() {
36 mark::check!(your_stack_belongs_to_me); 148 mark::check!(your_stack_belongs_to_me);
diff --git a/crates/hir_def/src/nameres/tests/block.rs b/crates/hir_def/src/body/tests/block.rs
index 6cc659513..d5f3ac4c5 100644
--- a/crates/hir_def/src/nameres/tests/block.rs
+++ b/crates/hir_def/src/body/tests/block.rs
@@ -1,4 +1,5 @@
1use super::*; 1use super::*;
2use expect_test::expect;
2 3
3#[test] 4#[test]
4fn inner_item_smoke() { 5fn inner_item_smoke() {
@@ -184,3 +185,36 @@ pub mod mark {
184 "#]], 185 "#]],
185 ); 186 );
186} 187}
188
189#[test]
190fn macro_resolve_legacy() {
191 check_at(
192 r#"
193//- /lib.rs
194mod module;
195
196//- /module.rs
197macro_rules! m {
198 () => {
199 struct Def {}
200 };
201}
202
203fn f() {
204 {
205 m!();
206 $0
207 }
208}
209 "#,
210 expect![[r#"
211 block scope
212 Def: t
213 crate
214 module: t
215
216 crate::module
217 f: v
218 "#]],
219 )
220}
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs
index e7b7724f7..c2b0dc007 100644
--- a/crates/hir_def/src/data.rs
+++ b/crates/hir_def/src/data.rs
@@ -262,7 +262,7 @@ fn collect_items(
262 let root = db.parse_or_expand(file_id).unwrap(); 262 let root = db.parse_or_expand(file_id).unwrap();
263 let call = ast_id_map.get(call.ast_id).to_node(&root); 263 let call = ast_id_map.get(call.ast_id).to_node(&root);
264 264
265 if let Some((mark, mac)) = expander.enter_expand(db, None, call).value { 265 if let Some((mark, mac)) = expander.enter_expand(db, call).value {
266 let src: InFile<ast::MacroItems> = expander.to_source(mac); 266 let src: InFile<ast::MacroItems> = expander.to_source(mac);
267 let item_tree = db.item_tree(src.file_id); 267 let item_tree = db.item_tree(src.file_id);
268 let iter = 268 let iter =
diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs
index aef7e1f6c..7fe6f6346 100644
--- a/crates/hir_def/src/db.rs
+++ b/crates/hir_def/src/db.rs
@@ -59,7 +59,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
59 fn crate_def_map_query(&self, krate: CrateId) -> Arc<DefMap>; 59 fn crate_def_map_query(&self, krate: CrateId) -> Arc<DefMap>;
60 60
61 #[salsa::invoke(DefMap::block_def_map_query)] 61 #[salsa::invoke(DefMap::block_def_map_query)]
62 fn block_def_map(&self, block: BlockId) -> Arc<DefMap>; 62 fn block_def_map(&self, block: BlockId) -> Option<Arc<DefMap>>;
63 63
64 #[salsa::invoke(StructData::struct_data_query)] 64 #[salsa::invoke(StructData::struct_data_query)]
65 fn struct_data(&self, id: StructId) -> Arc<StructData>; 65 fn struct_data(&self, id: StructId) -> Arc<StructData>;
diff --git a/crates/hir_def/src/expr.rs b/crates/hir_def/src/expr.rs
index 5be838f4a..4d72eaeaf 100644
--- a/crates/hir_def/src/expr.rs
+++ b/crates/hir_def/src/expr.rs
@@ -20,6 +20,7 @@ use crate::{
20 builtin_type::{BuiltinFloat, BuiltinInt}, 20 builtin_type::{BuiltinFloat, BuiltinInt},
21 path::{GenericArgs, Path}, 21 path::{GenericArgs, Path},
22 type_ref::{Mutability, Rawness, TypeRef}, 22 type_ref::{Mutability, Rawness, TypeRef},
23 BlockId,
23}; 24};
24 25
25pub type ExprId = Idx<Expr>; 26pub type ExprId = Idx<Expr>;
@@ -56,6 +57,7 @@ pub enum Expr {
56 else_branch: Option<ExprId>, 57 else_branch: Option<ExprId>,
57 }, 58 },
58 Block { 59 Block {
60 id: BlockId,
59 statements: Vec<Statement>, 61 statements: Vec<Statement>,
60 tail: Option<ExprId>, 62 tail: Option<ExprId>,
61 label: Option<LabelId>, 63 label: Option<LabelId>,
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index 42d9f0947..4bde67649 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -24,7 +24,7 @@ use la_arena::{Arena, Idx, RawIdx};
24use profile::Count; 24use profile::Count;
25use rustc_hash::FxHashMap; 25use rustc_hash::FxHashMap;
26use smallvec::SmallVec; 26use smallvec::SmallVec;
27use syntax::{ast, match_ast}; 27use syntax::{ast, match_ast, SyntaxKind};
28use test_utils::mark; 28use test_utils::mark;
29 29
30use crate::{ 30use crate::{
@@ -80,6 +80,10 @@ impl ItemTree {
80 pub(crate) fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> { 80 pub(crate) fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
81 let _p = profile::span("item_tree_query").detail(|| format!("{:?}", file_id)); 81 let _p = profile::span("item_tree_query").detail(|| format!("{:?}", file_id));
82 let syntax = if let Some(node) = db.parse_or_expand(file_id) { 82 let syntax = if let Some(node) = db.parse_or_expand(file_id) {
83 if node.kind() == SyntaxKind::ERROR {
84 // FIXME: not 100% sure why these crop up, but return an empty tree to avoid a panic
85 return Default::default();
86 }
83 node 87 node
84 } else { 88 } else {
85 return Default::default(); 89 return Default::default();
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs
index 42b50b5b7..5dd3705b0 100644
--- a/crates/hir_def/src/lib.rs
+++ b/crates/hir_def/src/lib.rs
@@ -81,7 +81,13 @@ pub struct ModuleId {
81impl ModuleId { 81impl ModuleId {
82 pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> { 82 pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
83 match self.block { 83 match self.block {
84 Some(block) => db.block_def_map(block), 84 Some(block) => {
85 db.block_def_map(block).unwrap_or_else(|| {
86 // NOTE: This should be unreachable - all `ModuleId`s come from their `DefMap`s,
87 // so the `DefMap` here must exist.
88 panic!("no `block_def_map` for `ModuleId` {:?}", self);
89 })
90 }
85 None => db.crate_def_map(self.krate), 91 None => db.crate_def_map(self.krate),
86 } 92 }
87 } 93 }
@@ -239,6 +245,7 @@ pub struct BlockId(salsa::InternId);
239#[derive(Debug, Hash, PartialEq, Eq, Clone)] 245#[derive(Debug, Hash, PartialEq, Eq, Clone)]
240pub struct BlockLoc { 246pub struct BlockLoc {
241 ast_id: AstId<ast::BlockExpr>, 247 ast_id: AstId<ast::BlockExpr>,
248 /// The containing module.
242 module: ModuleId, 249 module: ModuleId,
243} 250}
244impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block); 251impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs
index 0a15fc470..ece5958f4 100644
--- a/crates/hir_def/src/nameres.rs
+++ b/crates/hir_def/src/nameres.rs
@@ -197,12 +197,17 @@ impl DefMap {
197 Arc::new(def_map) 197 Arc::new(def_map)
198 } 198 }
199 199
200 pub(crate) fn block_def_map_query(db: &dyn DefDatabase, block_id: BlockId) -> Arc<DefMap> { 200 pub(crate) fn block_def_map_query(
201 db: &dyn DefDatabase,
202 block_id: BlockId,
203 ) -> Option<Arc<DefMap>> {
201 let block: BlockLoc = db.lookup_intern_block(block_id); 204 let block: BlockLoc = db.lookup_intern_block(block_id);
202 let parent = block.module.def_map(db); 205 let parent = block.module.def_map(db);
203 206
204 // FIXME: It would be good to just return the parent map when the block has no items, but 207 let item_tree = db.item_tree(block.ast_id.file_id);
205 // we rely on `def_map.block` in a few places, which is `Some` for the inner `DefMap`. 208 if item_tree.inner_items_of_block(block.ast_id.value).is_empty() {
209 return None;
210 }
206 211
207 let block_info = 212 let block_info =
208 BlockInfo { block: block_id, parent, parent_module: block.module.local_id }; 213 BlockInfo { block: block_id, parent, parent_module: block.module.local_id };
@@ -211,7 +216,7 @@ impl DefMap {
211 def_map.block = Some(block_info); 216 def_map.block = Some(block_info);
212 217
213 let def_map = collector::collect_defs(db, def_map, Some(block.ast_id)); 218 let def_map = collector::collect_defs(db, def_map, Some(block.ast_id));
214 Arc::new(def_map) 219 Some(Arc::new(def_map))
215 } 220 }
216 221
217 fn empty(krate: CrateId, edition: Edition) -> DefMap { 222 fn empty(krate: CrateId, edition: Edition) -> DefMap {
diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs
index b36d0b59b..723481c36 100644
--- a/crates/hir_def/src/nameres/tests.rs
+++ b/crates/hir_def/src/nameres/tests.rs
@@ -4,16 +4,14 @@ mod macros;
4mod mod_resolution; 4mod mod_resolution;
5mod diagnostics; 5mod diagnostics;
6mod primitives; 6mod primitives;
7mod block;
8 7
9use std::sync::Arc; 8use std::sync::Arc;
10 9
11use base_db::{fixture::WithFixture, FilePosition, SourceDatabase}; 10use base_db::{fixture::WithFixture, SourceDatabase};
12use expect_test::{expect, Expect}; 11use expect_test::{expect, Expect};
13use syntax::AstNode;
14use test_utils::mark; 12use test_utils::mark;
15 13
16use crate::{db::DefDatabase, nameres::*, test_db::TestDB, Lookup}; 14use crate::{db::DefDatabase, nameres::*, test_db::TestDB};
17 15
18fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { 16fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> {
19 let db = TestDB::with_files(ra_fixture); 17 let db = TestDB::with_files(ra_fixture);
@@ -21,74 +19,12 @@ fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> {
21 db.crate_def_map(krate) 19 db.crate_def_map(krate)
22} 20}
23 21
24fn compute_block_def_map(ra_fixture: &str) -> Arc<DefMap> {
25 let (db, position) = TestDB::with_position(ra_fixture);
26
27 // FIXME: perhaps we should make this use body lowering tests instead?
28
29 let module = db.module_for_file(position.file_id);
30 let mut def_map = db.crate_def_map(module.krate);
31 while let Some(new_def_map) = descend_def_map_at_position(&db, position, def_map.clone()) {
32 def_map = new_def_map;
33 }
34
35 // FIXME: select the right module, not the root
36
37 def_map
38}
39
40fn descend_def_map_at_position(
41 db: &dyn DefDatabase,
42 position: FilePosition,
43 def_map: Arc<DefMap>,
44) -> Option<Arc<DefMap>> {
45 for (local_id, module_data) in def_map.modules() {
46 let mod_def = module_data.origin.definition_source(db);
47 let ast_map = db.ast_id_map(mod_def.file_id);
48 let item_tree = db.item_tree(mod_def.file_id);
49 let root = db.parse_or_expand(mod_def.file_id).unwrap();
50 for item in module_data.scope.declarations() {
51 match item {
52 ModuleDefId::FunctionId(it) => {
53 // Technically blocks can be inside any type (due to arrays and const generics),
54 // and also in const/static initializers. For tests we only really care about
55 // functions though.
56
57 let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root);
58
59 if ast.syntax().text_range().contains(position.offset) {
60 // Cursor inside function, descend into its body's DefMap.
61 // Note that we don't handle block *expressions* inside function bodies.
62 let ast_map = db.ast_id_map(position.file_id.into());
63 let ast_id = ast_map.ast_id(&ast.body().unwrap());
64 let block = BlockLoc {
65 ast_id: InFile::new(position.file_id.into(), ast_id),
66 module: def_map.module_id(local_id),
67 };
68 let block_id = db.intern_block(block);
69 return Some(db.block_def_map(block_id));
70 }
71 }
72 _ => continue,
73 }
74 }
75 }
76
77 None
78}
79
80fn check(ra_fixture: &str, expect: Expect) { 22fn check(ra_fixture: &str, expect: Expect) {
81 let def_map = compute_crate_def_map(ra_fixture); 23 let def_map = compute_crate_def_map(ra_fixture);
82 let actual = def_map.dump(); 24 let actual = def_map.dump();
83 expect.assert_eq(&actual); 25 expect.assert_eq(&actual);
84} 26}
85 27
86fn check_at(ra_fixture: &str, expect: Expect) {
87 let def_map = compute_block_def_map(ra_fixture);
88 let actual = def_map.dump();
89 expect.assert_eq(&actual);
90}
91
92#[test] 28#[test]
93fn crate_def_map_smoke_test() { 29fn crate_def_map_smoke_test() {
94 check( 30 check(
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index d7351d212..12f1591c8 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -137,7 +137,7 @@ impl<'a> InferenceContext<'a> {
137 137
138 self.coerce_merge_branch(&then_ty, &else_ty) 138 self.coerce_merge_branch(&then_ty, &else_ty)
139 } 139 }
140 Expr::Block { statements, tail, label } => match label { 140 Expr::Block { statements, tail, label, id: _ } => match label {
141 Some(_) => { 141 Some(_) => {
142 let break_ty = self.table.new_type_var(); 142 let break_ty = self.table.new_type_var();
143 self.breakables.push(BreakableContext { 143 self.breakables.push(BreakableContext {