]> git.proxmox.com Git - rustc.git/blame - src/tools/rust-analyzer/crates/hir-def/src/body/tests/block.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / hir-def / src / body / tests / block.rs
CommitLineData
064997fb
FG
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 nested_module_scoping() {
138 check_block_scopes_at(
139 r#"
140fn f() {
141 mod module {
142 struct Struct {}
143 fn f() {
144 use self::Struct;
145 $0
146 }
147 }
148}
149 "#,
150 expect![[r#"
151 BlockId(1) in ModuleId { krate: CrateId(0), block: Some(BlockId(0)), local_id: Idx::<ModuleData>(1) }
152 BlockId(0) in ModuleId { krate: CrateId(0), block: None, local_id: Idx::<ModuleData>(0) }
153 crate scope
154 "#]],
155 );
156}
157
158#[test]
159fn legacy_macro_items() {
160 // Checks that legacy-scoped `macro_rules!` from parent namespaces are resolved and expanded
161 // correctly.
162 check_at(
163 r#"
164macro_rules! mark {
165 () => {
166 struct Hit {}
167 }
168}
169
170fn f() {
171 mark!();
172 $0
173}
174"#,
175 expect![[r#"
176 block scope
177 Hit: t
178
179 crate
180 f: v
181 "#]],
182 );
183}
184
185#[test]
186fn macro_resolve() {
187 check_at(
188 r#"
189//- /lib.rs crate:lib deps:core
190use core::cov_mark;
191
192fn f() {
193 fn nested() {
194 cov_mark::mark!(Hit);
195 $0
196 }
197}
198//- /core.rs crate:core
199pub mod cov_mark {
200 #[macro_export]
201 macro_rules! _mark {
202 ($name:ident) => {
203 struct $name {}
204 }
205 }
206
207 pub use crate::_mark as mark;
208}
209"#,
210 expect![[r#"
211 block scope
212 Hit: t
213
214 block scope
215 nested: v
216
217 crate
218 cov_mark: t
219 f: v
220 "#]],
221 );
222}
223
224#[test]
225fn macro_resolve_legacy() {
226 check_at(
227 r#"
228//- /lib.rs
229mod module;
230
231//- /module.rs
232macro_rules! m {
233 () => {
234 struct Def {}
235 };
236}
237
238fn f() {
239 {
240 m!();
241 $0
242 }
243}
244 "#,
245 expect![[r#"
246 block scope
247 Def: t
248
249 crate
250 module: t
251
252 crate::module
253 f: v
254 "#]],
255 )
256}
257
258#[test]
259fn super_does_not_resolve_to_block_module() {
260 check_at(
261 r#"
262fn main() {
263 struct Struct {}
264 mod module {
265 use super::Struct;
266
267 $0
268 }
269}
270 "#,
271 expect![[r#"
272 block scope
273 Struct: t
274 module: t
275
276 block scope::module
277 Struct: _
278
279 crate
280 main: v
281 "#]],
282 );
283}
284
285#[test]
286fn underscore_import() {
287 // This used to panic, because the default (private) visibility inside block expressions would
288 // point into the containing `DefMap`, which visibilities should never be able to do.
289 cov_mark::check!(adjust_vis_in_block_def_map);
290 check_at(
291 r#"
292mod m {
293 fn main() {
294 use Tr as _;
295 trait Tr {}
296 $0
297 }
298}
299 "#,
300 expect![[r#"
301 block scope
302 _: t
303 Tr: t
304
305 crate
306 m: t
307
308 crate::m
309 main: v
310 "#]],
311 );
312}
313
314#[test]
315fn nested_macro_item_decl() {
316 cov_mark::check!(macro_call_in_macro_stmts_is_added_to_item_tree);
317 check_at(
318 r#"
319macro_rules! inner_declare {
320 ($ident:ident) => {
321 static $ident: u32 = 0;
322 };
323}
324macro_rules! declare {
325 ($ident:ident) => {
326 inner_declare!($ident);
327 };
328}
329
330fn foo() {
331 declare!(bar);
332 bar;
333 $0
334}
335 "#,
336 expect![[r#"
337 block scope
338 bar: v
339
340 crate
341 foo: v
342 "#]],
343 )
344}
345
346#[test]
347fn is_visible_from_same_def_map() {
348 // Regression test for https://github.com/rust-lang/rust-analyzer/issues/9481
349 cov_mark::check!(is_visible_from_same_block_def_map);
350 check_at(
351 r#"
352fn outer() {
353 mod tests {
354 use super::*;
355 }
356 use crate::name;
357 $0
358}
359 "#,
360 expect![[r#"
361 block scope
362 name: _
363 tests: t
364
365 block scope::tests
366 name: _
367 outer: v
368
369 crate
370 outer: v
371 "#]],
372 );
373}
374
375#[test]
376fn stmt_macro_expansion_with_trailing_expr() {
377 cov_mark::check!(macro_stmt_with_trailing_macro_expr);
378 check_at(
379 r#"
380macro_rules! mac {
381 () => { mac!($) };
382 ($x:tt) => { fn inner() {} };
383}
384fn foo() {
385 mac!();
386 $0
387}
388 "#,
389 expect![[r#"
390 block scope
391 inner: v
392
393 crate
394 foo: v
395 "#]],
396 )
397}
353b0b11
FG
398
399#[test]
400fn trailing_expr_macro_expands_stmts() {
401 check_at(
402 r#"
403macro_rules! foo {
404 () => { const FOO: u32 = 0;const BAR: u32 = 0; };
405}
406fn f() {$0
407 foo!{}
408};
409 "#,
410 expect![[r#"
411 block scope
412 BAR: v
413 FOO: v
414
415 crate
416 f: v
417 "#]],
418 )
419}