diff options
author | Jonas Schievink <[email protected]> | 2021-02-02 10:46:58 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-02-02 10:46:58 +0000 |
commit | 7202ce6c963f047b8890ee50acc5aaf5d65f175d (patch) | |
tree | 4424b450134591652648d4709715655975fe3ba7 /crates/hir_def/src | |
parent | 3c1fcfcd1b79430c1ea97eea4ce0c89c9c793cdb (diff) |
Revert "Use block_def_map in body lowering"
Diffstat (limited to 'crates/hir_def/src')
-rw-r--r-- | crates/hir_def/src/body.rs | 19 | ||||
-rw-r--r-- | crates/hir_def/src/body/lower.rs | 35 | ||||
-rw-r--r-- | crates/hir_def/src/body/tests.rs | 116 | ||||
-rw-r--r-- | crates/hir_def/src/data.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/expr.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/item_tree.rs | 6 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 6 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests.rs | 68 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/block.rs (renamed from crates/hir_def/src/body/tests/block.rs) | 1 |
9 files changed, 95 insertions, 160 deletions
diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 41abd8f83..b9ecf22fa 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 | ||
47 | pub(crate) struct Expander { | 47 | pub(crate) struct Expander { |
48 | cfg_expander: CfgExpander, | 48 | cfg_expander: CfgExpander, |
49 | def_map: Arc<DefMap>, | 49 | crate_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 | def_map: crate_def_map, | 94 | crate_def_map, |
95 | current_file_id, | 95 | current_file_id, |
96 | ast_id_map, | 96 | ast_id_map, |
97 | module, | 97 | module, |
@@ -102,6 +102,7 @@ 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>, | ||
105 | macro_call: ast::MacroCall, | 106 | macro_call: ast::MacroCall, |
106 | ) -> ExpandResult<Option<(Mark, T)>> { | 107 | ) -> ExpandResult<Option<(Mark, T)>> { |
107 | if self.recursion_limit + 1 > EXPANSION_RECURSION_LIMIT { | 108 | if self.recursion_limit + 1 > EXPANSION_RECURSION_LIMIT { |
@@ -111,12 +112,18 @@ impl Expander { | |||
111 | 112 | ||
112 | let macro_call = InFile::new(self.current_file_id, ¯o_call); | 113 | let macro_call = InFile::new(self.current_file_id, ¯o_call); |
113 | 114 | ||
114 | let resolver = | 115 | let resolver = |path: ModPath| -> Option<MacroDefId> { |
115 | |path: ModPath| -> Option<MacroDefId> { self.resolve_path_as_macro(db, &path) }; | 116 | if let Some(local_scope) = local_scope { |
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 | }; | ||
116 | 123 | ||
117 | let mut err = None; | 124 | let mut err = None; |
118 | let call_id = | 125 | let call_id = |
119 | macro_call.as_call_id_with_errors(db, self.def_map.krate(), resolver, &mut |e| { | 126 | macro_call.as_call_id_with_errors(db, self.crate_def_map.krate(), resolver, &mut |e| { |
120 | err.get_or_insert(e); | 127 | err.get_or_insert(e); |
121 | }); | 128 | }); |
122 | let call_id = match call_id { | 129 | let call_id = match call_id { |
@@ -197,7 +204,7 @@ impl Expander { | |||
197 | } | 204 | } |
198 | 205 | ||
199 | fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> { | 206 | fn resolve_path_as_macro(&self, db: &dyn DefDatabase, path: &ModPath) -> Option<MacroDefId> { |
200 | self.def_map | 207 | self.crate_def_map |
201 | .resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other) | 208 | .resolve_path(db, self.module.local_id, path, BuiltinShadowMode::Other) |
202 | .0 | 209 | .0 |
203 | .take_macros() | 210 | .take_macros() |
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index bc61730a7..209965fca 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 | ||
4 | use std::{any::type_name, mem, sync::Arc}; | 4 | use std::{any::type_name, sync::Arc}; |
5 | 5 | ||
6 | use either::Either; | 6 | use either::Either; |
7 | use hir_expand::{ | 7 | use 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, BlockLoc, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, | 39 | AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, |
40 | ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, | 40 | StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, |
41 | }; | 41 | }; |
42 | 42 | ||
43 | use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; | 43 | use 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 unit(&mut self) -> ExprId { | 155 | fn empty_block(&mut self) -> ExprId { |
156 | self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() }) | 156 | self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None, label: None }) |
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.unit()), | 225 | expr: else_branch.unwrap_or_else(|| self.empty_block()), |
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, e); | 564 | let res = self.expander.enter_expand(self.db, Some(&self.body.item_scope), e); |
565 | 565 | ||
566 | match &res.err { | 566 | match &res.err { |
567 | Some(ExpandError::UnresolvedProcMacro) => { | 567 | Some(ExpandError::UnresolvedProcMacro) => { |
@@ -697,27 +697,12 @@ 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 ast_id = self.expander.ast_id(&block); | 700 | let syntax_node_ptr = AstPtr::new(&block.clone().into()); |
701 | let block_loc = BlockLoc { ast_id, module: self.expander.module }; | ||
702 | let block_id = self.db.intern_block(block_loc); | ||
703 | let def_map = self.db.block_def_map(block_id); | ||
704 | let root = def_map.module_id(def_map.root()); | ||
705 | let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); | ||
706 | let prev_module = mem::replace(&mut self.expander.module, root); | ||
707 | |||
708 | self.collect_stmts_items(block.statements()); | 701 | self.collect_stmts_items(block.statements()); |
709 | let statements = | 702 | let statements = |
710 | block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect(); | 703 | block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect(); |
711 | let tail = block.tail_expr().map(|e| self.collect_expr(e)); | 704 | let tail = block.tail_expr().map(|e| self.collect_expr(e)); |
712 | let syntax_node_ptr = AstPtr::new(&block.clone().into()); | 705 | self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr) |
713 | let expr_id = self.alloc_expr( | ||
714 | Expr::Block { id: block_id, statements, tail, label: None }, | ||
715 | syntax_node_ptr, | ||
716 | ); | ||
717 | |||
718 | self.expander.def_map = prev_def_map; | ||
719 | self.expander.module = prev_module; | ||
720 | expr_id | ||
721 | } | 706 | } |
722 | 707 | ||
723 | fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) { | 708 | fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) { |
@@ -847,7 +832,7 @@ impl ExprCollector<'_> { | |||
847 | if annotation == BindingAnnotation::Unannotated && subpat.is_none() { | 832 | if annotation == BindingAnnotation::Unannotated && subpat.is_none() { |
848 | // This could also be a single-segment path pattern. To | 833 | // This could also be a single-segment path pattern. To |
849 | // decide that, we need to try resolving the name. | 834 | // decide that, we need to try resolving the name. |
850 | let (resolved, _) = self.expander.def_map.resolve_path( | 835 | let (resolved, _) = self.expander.crate_def_map.resolve_path( |
851 | self.db, | 836 | self.db, |
852 | self.expander.module.local_id, | 837 | self.expander.module.local_id, |
853 | &name.clone().into(), | 838 | &name.clone().into(), |
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index da60072ce..2e5d0a01e 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -1,10 +1,7 @@ | |||
1 | mod block; | 1 | use base_db::{fixture::WithFixture, SourceDatabase}; |
2 | |||
3 | use base_db::{fixture::WithFixture, FilePosition, SourceDatabase}; | ||
4 | use expect_test::Expect; | ||
5 | use test_utils::mark; | 2 | use test_utils::mark; |
6 | 3 | ||
7 | use crate::{test_db::TestDB, BlockId, ModuleDefId}; | 4 | use crate::{test_db::TestDB, ModuleDefId}; |
8 | 5 | ||
9 | use super::*; | 6 | use super::*; |
10 | 7 | ||
@@ -34,115 +31,6 @@ fn check_diagnostics(ra_fixture: &str) { | |||
34 | db.check_diagnostics(); | 31 | db.check_diagnostics(); |
35 | } | 32 | } |
36 | 33 | ||
37 | fn 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); | ||
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 | |||
60 | fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) -> Option<BlockId> { | ||
61 | let mut size = None; | ||
62 | let mut fn_def = None; | ||
63 | for (_, module) in def_map.modules() { | ||
64 | let file_id = module.definition_source(db).file_id; | ||
65 | if file_id != position.file_id.into() { | ||
66 | continue; | ||
67 | } | ||
68 | let root = db.parse_or_expand(file_id).unwrap(); | ||
69 | let ast_map = db.ast_id_map(file_id); | ||
70 | let item_tree = db.item_tree(file_id); | ||
71 | for decl in module.scope.declarations() { | ||
72 | if let ModuleDefId::FunctionId(it) = decl { | ||
73 | 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 | |||
76 | // Find the smallest (innermost) function containing the cursor. | ||
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 | |||
140 | fn 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 | |||
146 | #[test] | 34 | #[test] |
147 | fn your_stack_belongs_to_me() { | 35 | fn your_stack_belongs_to_me() { |
148 | mark::check!(your_stack_belongs_to_me); | 36 | mark::check!(your_stack_belongs_to_me); |
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index c2b0dc007..e7b7724f7 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, call).value { | 265 | if let Some((mark, mac)) = expander.enter_expand(db, None, 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/expr.rs b/crates/hir_def/src/expr.rs index 4d72eaeaf..5be838f4a 100644 --- a/crates/hir_def/src/expr.rs +++ b/crates/hir_def/src/expr.rs | |||
@@ -20,7 +20,6 @@ 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, | ||
24 | }; | 23 | }; |
25 | 24 | ||
26 | pub type ExprId = Idx<Expr>; | 25 | pub type ExprId = Idx<Expr>; |
@@ -57,7 +56,6 @@ pub enum Expr { | |||
57 | else_branch: Option<ExprId>, | 56 | else_branch: Option<ExprId>, |
58 | }, | 57 | }, |
59 | Block { | 58 | Block { |
60 | id: BlockId, | ||
61 | statements: Vec<Statement>, | 59 | statements: Vec<Statement>, |
62 | tail: Option<ExprId>, | 60 | tail: Option<ExprId>, |
63 | label: Option<LabelId>, | 61 | label: Option<LabelId>, |
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 4bde67649..42d9f0947 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}; | |||
24 | use profile::Count; | 24 | use profile::Count; |
25 | use rustc_hash::FxHashMap; | 25 | use rustc_hash::FxHashMap; |
26 | use smallvec::SmallVec; | 26 | use smallvec::SmallVec; |
27 | use syntax::{ast, match_ast, SyntaxKind}; | 27 | use syntax::{ast, match_ast}; |
28 | use test_utils::mark; | 28 | use test_utils::mark; |
29 | 29 | ||
30 | use crate::{ | 30 | use crate::{ |
@@ -80,10 +80,6 @@ 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 | } | ||
87 | node | 83 | node |
88 | } else { | 84 | } else { |
89 | return Default::default(); | 85 | return Default::default(); |
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 9839761d1..6169b3bbc 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -201,10 +201,8 @@ impl DefMap { | |||
201 | let block: BlockLoc = db.lookup_intern_block(block_id); | 201 | let block: BlockLoc = db.lookup_intern_block(block_id); |
202 | let parent = block.module.def_map(db); | 202 | let parent = block.module.def_map(db); |
203 | 203 | ||
204 | let item_tree = db.item_tree(block.ast_id.file_id); | 204 | // FIXME: It would be good to just return the parent map when the block has no items, but |
205 | if item_tree.inner_items_of_block(block.ast_id.value).is_empty() { | 205 | // we rely on `def_map.block` in a few places, which is `Some` for the inner `DefMap`. |
206 | return parent.clone(); | ||
207 | } | ||
208 | 206 | ||
209 | let block_info = | 207 | let block_info = |
210 | BlockInfo { block: block_id, parent, parent_module: block.module.local_id }; | 208 | BlockInfo { block: block_id, parent, parent_module: block.module.local_id }; |
diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs index 723481c36..b36d0b59b 100644 --- a/crates/hir_def/src/nameres/tests.rs +++ b/crates/hir_def/src/nameres/tests.rs | |||
@@ -4,14 +4,16 @@ mod macros; | |||
4 | mod mod_resolution; | 4 | mod mod_resolution; |
5 | mod diagnostics; | 5 | mod diagnostics; |
6 | mod primitives; | 6 | mod primitives; |
7 | mod block; | ||
7 | 8 | ||
8 | use std::sync::Arc; | 9 | use std::sync::Arc; |
9 | 10 | ||
10 | use base_db::{fixture::WithFixture, SourceDatabase}; | 11 | use base_db::{fixture::WithFixture, FilePosition, SourceDatabase}; |
11 | use expect_test::{expect, Expect}; | 12 | use expect_test::{expect, Expect}; |
13 | use syntax::AstNode; | ||
12 | use test_utils::mark; | 14 | use test_utils::mark; |
13 | 15 | ||
14 | use crate::{db::DefDatabase, nameres::*, test_db::TestDB}; | 16 | use crate::{db::DefDatabase, nameres::*, test_db::TestDB, Lookup}; |
15 | 17 | ||
16 | fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { | 18 | fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { |
17 | let db = TestDB::with_files(ra_fixture); | 19 | let db = TestDB::with_files(ra_fixture); |
@@ -19,12 +21,74 @@ fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { | |||
19 | db.crate_def_map(krate) | 21 | db.crate_def_map(krate) |
20 | } | 22 | } |
21 | 23 | ||
24 | fn 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 | |||
40 | fn 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 | |||
22 | fn check(ra_fixture: &str, expect: Expect) { | 80 | fn check(ra_fixture: &str, expect: Expect) { |
23 | let def_map = compute_crate_def_map(ra_fixture); | 81 | let def_map = compute_crate_def_map(ra_fixture); |
24 | let actual = def_map.dump(); | 82 | let actual = def_map.dump(); |
25 | expect.assert_eq(&actual); | 83 | expect.assert_eq(&actual); |
26 | } | 84 | } |
27 | 85 | ||
86 | fn 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 | |||
28 | #[test] | 92 | #[test] |
29 | fn crate_def_map_smoke_test() { | 93 | fn crate_def_map_smoke_test() { |
30 | check( | 94 | check( |
diff --git a/crates/hir_def/src/body/tests/block.rs b/crates/hir_def/src/nameres/tests/block.rs index 6b1ed2555..6cc659513 100644 --- a/crates/hir_def/src/body/tests/block.rs +++ b/crates/hir_def/src/nameres/tests/block.rs | |||
@@ -1,5 +1,4 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | use expect_test::expect; | ||
3 | 2 | ||
4 | #[test] | 3 | #[test] |
5 | fn inner_item_smoke() { | 4 | fn inner_item_smoke() { |