aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/body/tests/block.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/body/tests/block.rs')
-rw-r--r--crates/hir_def/src/body/tests/block.rs290
1 files changed, 290 insertions, 0 deletions
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..8bca72a17
--- /dev/null
+++ b/crates/hir_def/src/body/tests/block.rs
@@ -0,0 +1,290 @@
1use super::*;
2use expect_test::expect;
3
4#[test]
5fn inner_item_smoke() {
6 check_at(
7 r#"
8struct inner {}
9fn 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]
26fn use_from_crate() {
27 check_at(
28 r#"
29struct Struct {}
30fn outer() {
31 fn Struct() {}
32 use Struct as PlainStruct;
33 use crate::Struct as CrateStruct;
34 use self::Struct as SelfStruct;
35 use super::Struct as SuperStruct;
36 $0
37}
38"#,
39 expect![[r#"
40 block scope
41 CrateStruct: t
42 PlainStruct: t v
43 SelfStruct: t
44 Struct: v
45 SuperStruct: _
46
47 crate
48 Struct: t
49 outer: v
50 "#]],
51 );
52}
53
54#[test]
55fn merge_namespaces() {
56 check_at(
57 r#"
58struct name {}
59fn outer() {
60 fn name() {}
61
62 use name as imported; // should import both `name`s
63
64 $0
65}
66"#,
67 expect![[r#"
68 block scope
69 imported: t v
70 name: v
71
72 crate
73 name: t
74 outer: v
75 "#]],
76 );
77}
78
79#[test]
80fn nested_blocks() {
81 check_at(
82 r#"
83fn outer() {
84 struct inner1 {}
85 fn inner() {
86 use inner1;
87 use outer;
88 fn inner2() {}
89 $0
90 }
91}
92"#,
93 expect![[r#"
94 block scope
95 inner1: t
96 inner2: v
97 outer: v
98
99 block scope
100 inner: v
101 inner1: t
102
103 crate
104 outer: v
105 "#]],
106 );
107}
108
109#[test]
110fn super_imports() {
111 check_at(
112 r#"
113mod module {
114 fn f() {
115 use super::Struct;
116 $0
117 }
118}
119
120struct Struct {}
121"#,
122 expect![[r#"
123 block scope
124 Struct: t
125
126 crate
127 Struct: t
128 module: t
129
130 crate::module
131 f: v
132 "#]],
133 );
134}
135
136#[test]
137fn legacy_macro_items() {
138 // Checks that legacy-scoped `macro_rules!` from parent namespaces are resolved and expanded
139 // correctly.
140 check_at(
141 r#"
142macro_rules! hit {
143 () => {
144 struct Hit {}
145 }
146}
147
148fn f() {
149 hit!();
150 $0
151}
152"#,
153 expect![[r#"
154 block scope
155 Hit: t
156
157 crate
158 f: v
159 "#]],
160 );
161}
162
163#[test]
164fn macro_resolve() {
165 check_at(
166 r#"
167//- /lib.rs crate:lib deps:core
168use core::mark;
169
170fn f() {
171 fn nested() {
172 mark::hit!(Hit);
173 $0
174 }
175}
176//- /core.rs crate:core
177pub mod mark {
178 #[macro_export]
179 macro_rules! _hit {
180 ($name:ident) => {
181 struct $name {}
182 }
183 }
184
185 pub use crate::_hit as hit;
186}
187"#,
188 expect![[r#"
189 block scope
190 Hit: t
191
192 block scope
193 nested: v
194
195 crate
196 f: v
197 mark: t
198 "#]],
199 );
200}
201
202#[test]
203fn macro_resolve_legacy() {
204 check_at(
205 r#"
206//- /lib.rs
207mod module;
208
209//- /module.rs
210macro_rules! m {
211 () => {
212 struct Def {}
213 };
214}
215
216fn f() {
217 {
218 m!();
219 $0
220 }
221}
222 "#,
223 expect![[r#"
224 block scope
225 Def: t
226
227 crate
228 module: t
229
230 crate::module
231 f: v
232 "#]],
233 )
234}
235
236#[test]
237fn super_does_not_resolve_to_block_module() {
238 check_at(
239 r#"
240fn main() {
241 struct Struct {}
242 mod module {
243 use super::Struct;
244
245 $0
246 }
247}
248 "#,
249 expect![[r#"
250 block scope
251 Struct: t
252 module: t
253
254 block scope::module
255 Struct: _
256
257 crate
258 main: v
259 "#]],
260 );
261}
262
263#[test]
264fn underscore_import() {
265 // This used to panic, because the default (private) visibility inside block expressions would
266 // point into the containing `DefMap`, which visibilities should never be able to do.
267 mark::check!(adjust_vis_in_block_def_map);
268 check_at(
269 r#"
270mod m {
271 fn main() {
272 use Tr as _;
273 trait Tr {}
274 $0
275 }
276}
277 "#,
278 expect![[r#"
279 block scope
280 _: t
281 Tr: t
282
283 crate
284 m: t
285
286 crate::m
287 main: v
288 "#]],
289 );
290}