diff options
author | Jonas Schievink <[email protected]> | 2021-02-01 12:20:35 +0000 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-02-03 13:21:15 +0000 |
commit | 7eff6705cc1c1d4399a7c9da360d344a96df59b6 (patch) | |
tree | 3baf5b1f4fd8f94111194bf91dc2c26de13d0f13 | |
parent | b7be2b1d3cc2fd8ca8e7aa7542aaf9d4f905f9f5 (diff) |
Use body lowering for block_def_map tests
Removes the hacky and buggy custom lowering code
-rw-r--r-- | crates/hir_def/src/body/tests.rs | 116 | ||||
-rw-r--r-- | crates/hir_def/src/body/tests/block.rs (renamed from crates/hir_def/src/nameres/tests/block.rs) | 1 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests.rs | 68 |
3 files changed, 117 insertions, 68 deletions
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index 2e5d0a01e..da60072ce 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -1,7 +1,10 @@ | |||
1 | use base_db::{fixture::WithFixture, SourceDatabase}; | 1 | mod block; |
2 | |||
3 | use base_db::{fixture::WithFixture, FilePosition, SourceDatabase}; | ||
4 | use expect_test::Expect; | ||
2 | use test_utils::mark; | 5 | use test_utils::mark; |
3 | 6 | ||
4 | use crate::{test_db::TestDB, ModuleDefId}; | 7 | use crate::{test_db::TestDB, BlockId, ModuleDefId}; |
5 | 8 | ||
6 | use super::*; | 9 | use 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 | ||
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 | |||
34 | #[test] | 146 | #[test] |
35 | fn your_stack_belongs_to_me() { | 147 | fn 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..6b1ed2555 100644 --- a/crates/hir_def/src/nameres/tests/block.rs +++ b/crates/hir_def/src/body/tests/block.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | use expect_test::expect; | ||
2 | 3 | ||
3 | #[test] | 4 | #[test] |
4 | fn inner_item_smoke() { | 5 | fn inner_item_smoke() { |
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; | |||
4 | mod mod_resolution; | 4 | mod mod_resolution; |
5 | mod diagnostics; | 5 | mod diagnostics; |
6 | mod primitives; | 6 | mod primitives; |
7 | mod block; | ||
8 | 7 | ||
9 | use std::sync::Arc; | 8 | use std::sync::Arc; |
10 | 9 | ||
11 | use base_db::{fixture::WithFixture, FilePosition, SourceDatabase}; | 10 | use base_db::{fixture::WithFixture, SourceDatabase}; |
12 | use expect_test::{expect, Expect}; | 11 | use expect_test::{expect, Expect}; |
13 | use syntax::AstNode; | ||
14 | use test_utils::mark; | 12 | use test_utils::mark; |
15 | 13 | ||
16 | use crate::{db::DefDatabase, nameres::*, test_db::TestDB, Lookup}; | 14 | use crate::{db::DefDatabase, nameres::*, test_db::TestDB}; |
17 | 15 | ||
18 | fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> { | 16 | fn 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 | ||
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 | |||
80 | fn check(ra_fixture: &str, expect: Expect) { | 22 | fn 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 | ||
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 | |||
92 | #[test] | 28 | #[test] |
93 | fn crate_def_map_smoke_test() { | 29 | fn crate_def_map_smoke_test() { |
94 | check( | 30 | check( |