diff options
Diffstat (limited to 'crates/hir_def/src/body')
-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/body/tests/block.rs | 187 |
3 files changed, 12 insertions, 326 deletions
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/body/tests/block.rs b/crates/hir_def/src/body/tests/block.rs deleted file mode 100644 index 6b1ed2555..000000000 --- a/crates/hir_def/src/body/tests/block.rs +++ /dev/null | |||
@@ -1,187 +0,0 @@ | |||
1 | use super::*; | ||
2 | use expect_test::expect; | ||
3 | |||
4 | #[test] | ||
5 | fn inner_item_smoke() { | ||
6 | check_at( | ||
7 | r#" | ||
8 | struct inner {} | ||
9 | fn outer() { | ||
10 | $0 | ||
11 | fn inner() {} | ||
12 | } | ||
13 | "#, | ||
14 | expect![[r#" | ||
15 | block scope | ||
16 | inner: v | ||
17 | crate | ||
18 | inner: t | ||
19 | outer: v | ||
20 | "#]], | ||
21 | ); | ||
22 | } | ||
23 | |||
24 | #[test] | ||
25 | fn use_from_crate() { | ||
26 | check_at( | ||
27 | r#" | ||
28 | struct Struct; | ||
29 | fn outer() { | ||
30 | use Struct; | ||
31 | use crate::Struct as CrateStruct; | ||
32 | use self::Struct as SelfStruct; | ||
33 | $0 | ||
34 | } | ||
35 | "#, | ||
36 | expect![[r#" | ||
37 | block scope | ||
38 | CrateStruct: t v | ||
39 | SelfStruct: t v | ||
40 | Struct: t v | ||
41 | crate | ||
42 | Struct: t v | ||
43 | outer: v | ||
44 | "#]], | ||
45 | ); | ||
46 | } | ||
47 | |||
48 | #[test] | ||
49 | fn merge_namespaces() { | ||
50 | check_at( | ||
51 | r#" | ||
52 | struct name {} | ||
53 | fn outer() { | ||
54 | fn name() {} | ||
55 | |||
56 | use name as imported; // should import both `name`s | ||
57 | |||
58 | $0 | ||
59 | } | ||
60 | "#, | ||
61 | expect![[r#" | ||
62 | block scope | ||
63 | imported: t v | ||
64 | name: v | ||
65 | crate | ||
66 | name: t | ||
67 | outer: v | ||
68 | "#]], | ||
69 | ); | ||
70 | } | ||
71 | |||
72 | #[test] | ||
73 | fn nested_blocks() { | ||
74 | check_at( | ||
75 | r#" | ||
76 | fn outer() { | ||
77 | struct inner1 {} | ||
78 | fn inner() { | ||
79 | use inner1; | ||
80 | use outer; | ||
81 | fn inner2() {} | ||
82 | $0 | ||
83 | } | ||
84 | } | ||
85 | "#, | ||
86 | expect![[r#" | ||
87 | block scope | ||
88 | inner1: t | ||
89 | inner2: v | ||
90 | outer: v | ||
91 | block scope | ||
92 | inner: v | ||
93 | inner1: t | ||
94 | crate | ||
95 | outer: v | ||
96 | "#]], | ||
97 | ); | ||
98 | } | ||
99 | |||
100 | #[test] | ||
101 | fn super_imports() { | ||
102 | check_at( | ||
103 | r#" | ||
104 | mod module { | ||
105 | fn f() { | ||
106 | use super::Struct; | ||
107 | $0 | ||
108 | } | ||
109 | } | ||
110 | |||
111 | struct Struct {} | ||
112 | "#, | ||
113 | expect![[r#" | ||
114 | block scope | ||
115 | Struct: t | ||
116 | crate | ||
117 | Struct: t | ||
118 | module: t | ||
119 | |||
120 | crate::module | ||
121 | f: v | ||
122 | "#]], | ||
123 | ); | ||
124 | } | ||
125 | |||
126 | #[test] | ||
127 | fn legacy_macro_items() { | ||
128 | // Checks that legacy-scoped `macro_rules!` from parent namespaces are resolved and expanded | ||
129 | // correctly. | ||
130 | check_at( | ||
131 | r#" | ||
132 | macro_rules! hit { | ||
133 | () => { | ||
134 | struct Hit {} | ||
135 | } | ||
136 | } | ||
137 | |||
138 | fn f() { | ||
139 | hit!(); | ||
140 | $0 | ||
141 | } | ||
142 | "#, | ||
143 | expect![[r#" | ||
144 | block scope | ||
145 | Hit: t | ||
146 | crate | ||
147 | f: v | ||
148 | "#]], | ||
149 | ); | ||
150 | } | ||
151 | |||
152 | #[test] | ||
153 | fn macro_resolve() { | ||
154 | check_at( | ||
155 | r#" | ||
156 | //- /lib.rs crate:lib deps:core | ||
157 | use core::mark; | ||
158 | |||
159 | fn f() { | ||
160 | fn nested() { | ||
161 | mark::hit!(Hit); | ||
162 | $0 | ||
163 | } | ||
164 | } | ||
165 | //- /core.rs crate:core | ||
166 | pub mod mark { | ||
167 | #[macro_export] | ||
168 | macro_rules! _hit { | ||
169 | ($name:ident) => { | ||
170 | struct $name {} | ||
171 | } | ||
172 | } | ||
173 | |||
174 | pub use crate::_hit as hit; | ||
175 | } | ||
176 | "#, | ||
177 | expect![[r#" | ||
178 | block scope | ||
179 | Hit: t | ||
180 | block scope | ||
181 | nested: v | ||
182 | crate | ||
183 | f: v | ||
184 | mark: t | ||
185 | "#]], | ||
186 | ); | ||
187 | } | ||