diff options
Diffstat (limited to 'crates/hir_def/src/body')
-rw-r--r-- | crates/hir_def/src/body/lower.rs | 42 | ||||
-rw-r--r-- | crates/hir_def/src/body/tests.rs | 115 | ||||
-rw-r--r-- | crates/hir_def/src/body/tests/block.rs | 230 |
3 files changed, 373 insertions, 14 deletions
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 209965fca..28b11cdde 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, sync::Arc}; | 4 | use std::{any::type_name, mem, 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, 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 | ||
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 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 = | ||
702 | BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) }; | ||
703 | let block_id = self.db.intern_block(block_loc); | ||
704 | let opt_def_map = self.db.block_def_map(block_id); | ||
705 | let has_def_map = opt_def_map.is_some(); | ||
706 | let def_map = opt_def_map.unwrap_or_else(|| self.expander.def_map.clone()); | ||
707 | let module = if has_def_map { def_map.root() } else { self.expander.module }; | ||
708 | let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); | ||
709 | let prev_local_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_local_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>) { |
@@ -794,7 +812,7 @@ impl ExprCollector<'_> { | |||
794 | } | 812 | } |
795 | Either::Right(e) => { | 813 | Either::Right(e) => { |
796 | let mac = MacroDefId { | 814 | let mac = MacroDefId { |
797 | krate: self.expander.module.krate, | 815 | krate: self.expander.def_map.krate(), |
798 | ast_id: Some(self.expander.ast_id(&e)), | 816 | ast_id: Some(self.expander.ast_id(&e)), |
799 | kind: MacroDefKind::Declarative, | 817 | kind: MacroDefKind::Declarative, |
800 | local_inner: false, | 818 | local_inner: false, |
@@ -832,9 +850,9 @@ 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, |
838 | &name.clone().into(), | 856 | &name.clone().into(), |
839 | BuiltinShadowMode::Other, | 857 | BuiltinShadowMode::Other, |
840 | ); | 858 | ); |
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index 2e5d0a01e..a92134ba7 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,114 @@ 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) -> String { | ||
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.dump(&db); | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | |||
60 | fn 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 | |||
140 | fn check_at(ra_fixture: &str, expect: Expect) { | ||
141 | let actual = block_def_map_at(ra_fixture); | ||
142 | expect.assert_eq(&actual); | ||
143 | } | ||
144 | |||
34 | #[test] | 145 | #[test] |
35 | fn your_stack_belongs_to_me() { | 146 | fn your_stack_belongs_to_me() { |
36 | mark::check!(your_stack_belongs_to_me); | 147 | 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 new file mode 100644 index 000000000..062560a70 --- /dev/null +++ b/crates/hir_def/src/body/tests/block.rs | |||
@@ -0,0 +1,230 @@ | |||
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 | |||
18 | crate | ||
19 | inner: t | ||
20 | outer: v | ||
21 | "#]], | ||
22 | ); | ||
23 | } | ||
24 | |||
25 | #[test] | ||
26 | fn use_from_crate() { | ||
27 | check_at( | ||
28 | r#" | ||
29 | struct Struct; | ||
30 | fn outer() { | ||
31 | use Struct; | ||
32 | use crate::Struct as CrateStruct; | ||
33 | use self::Struct as SelfStruct; | ||
34 | $0 | ||
35 | } | ||
36 | "#, | ||
37 | expect![[r#" | ||
38 | block scope | ||
39 | CrateStruct: t v | ||
40 | SelfStruct: t v | ||
41 | Struct: t v | ||
42 | |||
43 | crate | ||
44 | Struct: t v | ||
45 | outer: v | ||
46 | "#]], | ||
47 | ); | ||
48 | } | ||
49 | |||
50 | #[test] | ||
51 | fn merge_namespaces() { | ||
52 | check_at( | ||
53 | r#" | ||
54 | struct name {} | ||
55 | fn outer() { | ||
56 | fn name() {} | ||
57 | |||
58 | use name as imported; // should import both `name`s | ||
59 | |||
60 | $0 | ||
61 | } | ||
62 | "#, | ||
63 | expect![[r#" | ||
64 | block scope | ||
65 | imported: t v | ||
66 | name: v | ||
67 | |||
68 | crate | ||
69 | name: t | ||
70 | outer: v | ||
71 | "#]], | ||
72 | ); | ||
73 | } | ||
74 | |||
75 | #[test] | ||
76 | fn nested_blocks() { | ||
77 | check_at( | ||
78 | r#" | ||
79 | fn outer() { | ||
80 | struct inner1 {} | ||
81 | fn inner() { | ||
82 | use inner1; | ||
83 | use outer; | ||
84 | fn inner2() {} | ||
85 | $0 | ||
86 | } | ||
87 | } | ||
88 | "#, | ||
89 | expect![[r#" | ||
90 | block scope | ||
91 | inner1: t | ||
92 | inner2: v | ||
93 | outer: v | ||
94 | |||
95 | block scope | ||
96 | inner: v | ||
97 | inner1: t | ||
98 | |||
99 | crate | ||
100 | outer: v | ||
101 | "#]], | ||
102 | ); | ||
103 | } | ||
104 | |||
105 | #[test] | ||
106 | fn super_imports() { | ||
107 | check_at( | ||
108 | r#" | ||
109 | mod module { | ||
110 | fn f() { | ||
111 | use super::Struct; | ||
112 | $0 | ||
113 | } | ||
114 | } | ||
115 | |||
116 | struct Struct {} | ||
117 | "#, | ||
118 | expect![[r#" | ||
119 | block scope | ||
120 | Struct: t | ||
121 | |||
122 | crate | ||
123 | Struct: t | ||
124 | module: t | ||
125 | |||
126 | crate::module | ||
127 | f: v | ||
128 | "#]], | ||
129 | ); | ||
130 | } | ||
131 | |||
132 | #[test] | ||
133 | fn legacy_macro_items() { | ||
134 | // Checks that legacy-scoped `macro_rules!` from parent namespaces are resolved and expanded | ||
135 | // correctly. | ||
136 | check_at( | ||
137 | r#" | ||
138 | macro_rules! hit { | ||
139 | () => { | ||
140 | struct Hit {} | ||
141 | } | ||
142 | } | ||
143 | |||
144 | fn f() { | ||
145 | hit!(); | ||
146 | $0 | ||
147 | } | ||
148 | "#, | ||
149 | expect![[r#" | ||
150 | block scope | ||
151 | Hit: t | ||
152 | |||
153 | crate | ||
154 | f: v | ||
155 | "#]], | ||
156 | ); | ||
157 | } | ||
158 | |||
159 | #[test] | ||
160 | fn macro_resolve() { | ||
161 | check_at( | ||
162 | r#" | ||
163 | //- /lib.rs crate:lib deps:core | ||
164 | use core::mark; | ||
165 | |||
166 | fn f() { | ||
167 | fn nested() { | ||
168 | mark::hit!(Hit); | ||
169 | $0 | ||
170 | } | ||
171 | } | ||
172 | //- /core.rs crate:core | ||
173 | pub mod mark { | ||
174 | #[macro_export] | ||
175 | macro_rules! _hit { | ||
176 | ($name:ident) => { | ||
177 | struct $name {} | ||
178 | } | ||
179 | } | ||
180 | |||
181 | pub use crate::_hit as hit; | ||
182 | } | ||
183 | "#, | ||
184 | expect![[r#" | ||
185 | block scope | ||
186 | Hit: t | ||
187 | |||
188 | block scope | ||
189 | nested: v | ||
190 | |||
191 | crate | ||
192 | f: v | ||
193 | mark: t | ||
194 | "#]], | ||
195 | ); | ||
196 | } | ||
197 | |||
198 | #[test] | ||
199 | fn macro_resolve_legacy() { | ||
200 | check_at( | ||
201 | r#" | ||
202 | //- /lib.rs | ||
203 | mod module; | ||
204 | |||
205 | //- /module.rs | ||
206 | macro_rules! m { | ||
207 | () => { | ||
208 | struct Def {} | ||
209 | }; | ||
210 | } | ||
211 | |||
212 | fn f() { | ||
213 | { | ||
214 | m!(); | ||
215 | $0 | ||
216 | } | ||
217 | } | ||
218 | "#, | ||
219 | expect![[r#" | ||
220 | block scope | ||
221 | Def: t | ||
222 | |||
223 | crate | ||
224 | module: t | ||
225 | |||
226 | crate::module | ||
227 | f: v | ||
228 | "#]], | ||
229 | ) | ||
230 | } | ||