aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres/tests/macros.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/nameres/tests/macros.rs')
-rw-r--r--crates/ra_hir/src/nameres/tests/macros.rs635
1 files changed, 0 insertions, 635 deletions
diff --git a/crates/ra_hir/src/nameres/tests/macros.rs b/crates/ra_hir/src/nameres/tests/macros.rs
deleted file mode 100644
index 4f52ad2c5..000000000
--- a/crates/ra_hir/src/nameres/tests/macros.rs
+++ /dev/null
@@ -1,635 +0,0 @@
1use super::*;
2
3#[test]
4fn macro_rules_are_globally_visible() {
5 let map = def_map(
6 "
7 //- /lib.rs
8 macro_rules! structs {
9 ($($i:ident),*) => {
10 $(struct $i { field: u32 } )*
11 }
12 }
13 structs!(Foo);
14 mod nested;
15
16 //- /nested.rs
17 structs!(Bar, Baz);
18 ",
19 );
20 assert_snapshot!(map, @r###"
21 ⋮crate
22 ⋮Foo: t v
23 ⋮nested: t
24
25 ⋮crate::nested
26 ⋮Bar: t v
27 ⋮Baz: t v
28 "###);
29}
30
31#[test]
32fn macro_rules_can_define_modules() {
33 let map = def_map(
34 "
35 //- /lib.rs
36 macro_rules! m {
37 ($name:ident) => { mod $name; }
38 }
39 m!(n1);
40
41 mod m {
42 m!(n3)
43 }
44
45 //- /n1.rs
46 m!(n2)
47 //- /n1/n2.rs
48 struct X;
49 //- /m/n3.rs
50 struct Y;
51 ",
52 );
53 assert_snapshot!(map, @r###"
54 crate
55 m: t
56 n1: t
57
58 crate::m
59 n3: t
60
61 crate::m::n3
62 Y: t v
63
64 crate::n1
65 n2: t
66
67 crate::n1::n2
68 X: t v
69 "###);
70}
71
72#[test]
73fn macro_rules_from_other_crates_are_visible() {
74 let map = def_map_with_crate_graph(
75 "
76 //- /main.rs
77 foo::structs!(Foo, Bar)
78 mod bar;
79
80 //- /bar.rs
81 use crate::*;
82
83 //- /lib.rs
84 #[macro_export]
85 macro_rules! structs {
86 ($($i:ident),*) => {
87 $(struct $i { field: u32 } )*
88 }
89 }
90 ",
91 crate_graph! {
92 "main": ("/main.rs", ["foo"]),
93 "foo": ("/lib.rs", []),
94 },
95 );
96 assert_snapshot!(map, @r###"
97 ⋮crate
98 ⋮Bar: t v
99 ⋮Foo: t v
100 ⋮bar: t
101
102 ⋮crate::bar
103 ⋮Bar: t v
104 ⋮Foo: t v
105 ⋮bar: t
106 "###);
107}
108
109#[test]
110fn macro_rules_export_with_local_inner_macros_are_visible() {
111 let map = def_map_with_crate_graph(
112 "
113 //- /main.rs
114 foo::structs!(Foo, Bar)
115 mod bar;
116
117 //- /bar.rs
118 use crate::*;
119
120 //- /lib.rs
121 #[macro_export(local_inner_macros)]
122 macro_rules! structs {
123 ($($i:ident),*) => {
124 $(struct $i { field: u32 } )*
125 }
126 }
127 ",
128 crate_graph! {
129 "main": ("/main.rs", ["foo"]),
130 "foo": ("/lib.rs", []),
131 },
132 );
133 assert_snapshot!(map, @r###"
134 ⋮crate
135 ⋮Bar: t v
136 ⋮Foo: t v
137 ⋮bar: t
138
139 ⋮crate::bar
140 ⋮Bar: t v
141 ⋮Foo: t v
142 ⋮bar: t
143 "###);
144}
145
146#[test]
147fn unexpanded_macro_should_expand_by_fixedpoint_loop() {
148 let map = def_map_with_crate_graph(
149 "
150 //- /main.rs
151 macro_rules! baz {
152 () => {
153 use foo::bar;
154 }
155 }
156
157 foo!();
158 bar!();
159 baz!();
160
161 //- /lib.rs
162 #[macro_export]
163 macro_rules! foo {
164 () => {
165 struct Foo { field: u32 }
166 }
167 }
168 #[macro_export]
169 macro_rules! bar {
170 () => {
171 use foo::foo;
172 }
173 }
174 ",
175 crate_graph! {
176 "main": ("/main.rs", ["foo"]),
177 "foo": ("/lib.rs", []),
178 },
179 );
180 assert_snapshot!(map, @r###"
181 ⋮crate
182 ⋮Foo: t v
183 ⋮bar: m
184 ⋮foo: m
185 "###);
186}
187
188#[test]
189fn macro_rules_from_other_crates_are_visible_with_macro_use() {
190 covers!(macro_rules_from_other_crates_are_visible_with_macro_use);
191 let map = def_map_with_crate_graph(
192 "
193 //- /main.rs
194 structs!(Foo);
195 structs_priv!(Bar);
196 structs_not_exported!(MacroNotResolved1);
197 crate::structs!(MacroNotResolved2);
198
199 mod bar;
200
201 #[macro_use]
202 extern crate foo;
203
204 //- /bar.rs
205 structs!(Baz);
206 crate::structs!(MacroNotResolved3);
207
208 //- /lib.rs
209 #[macro_export]
210 macro_rules! structs {
211 ($i:ident) => { struct $i; }
212 }
213
214 macro_rules! structs_not_exported {
215 ($i:ident) => { struct $i; }
216 }
217
218 mod priv_mod {
219 #[macro_export]
220 macro_rules! structs_priv {
221 ($i:ident) => { struct $i; }
222 }
223 }
224 ",
225 crate_graph! {
226 "main": ("/main.rs", ["foo"]),
227 "foo": ("/lib.rs", []),
228 },
229 );
230 assert_snapshot!(map, @r###"
231 ⋮crate
232 ⋮Bar: t v
233 ⋮Foo: t v
234 ⋮bar: t
235 ⋮foo: t
236
237 ⋮crate::bar
238 ⋮Baz: t v
239 "###);
240}
241
242#[test]
243fn prelude_is_macro_use() {
244 covers!(prelude_is_macro_use);
245 let map = def_map_with_crate_graph(
246 "
247 //- /main.rs
248 structs!(Foo);
249 structs_priv!(Bar);
250 structs_outside!(Out);
251 crate::structs!(MacroNotResolved2);
252
253 mod bar;
254
255 //- /bar.rs
256 structs!(Baz);
257 crate::structs!(MacroNotResolved3);
258
259 //- /lib.rs
260 #[prelude_import]
261 use self::prelude::*;
262
263 mod prelude {
264 #[macro_export]
265 macro_rules! structs {
266 ($i:ident) => { struct $i; }
267 }
268
269 mod priv_mod {
270 #[macro_export]
271 macro_rules! structs_priv {
272 ($i:ident) => { struct $i; }
273 }
274 }
275 }
276
277 #[macro_export]
278 macro_rules! structs_outside {
279 ($i:ident) => { struct $i; }
280 }
281 ",
282 crate_graph! {
283 "main": ("/main.rs", ["foo"]),
284 "foo": ("/lib.rs", []),
285 },
286 );
287 assert_snapshot!(map, @r###"
288 ⋮crate
289 ⋮Bar: t v
290 ⋮Foo: t v
291 ⋮Out: t v
292 ⋮bar: t
293
294 ⋮crate::bar
295 ⋮Baz: t v
296 "###);
297}
298
299#[test]
300fn prelude_cycle() {
301 let map = def_map(
302 "
303 //- /lib.rs
304 #[prelude_import]
305 use self::prelude::*;
306
307 declare_mod!();
308
309 mod prelude {
310 macro_rules! declare_mod {
311 () => (mod foo {})
312 }
313 }
314 ",
315 );
316 assert_snapshot!(map, @r###"
317 ⋮crate
318 ⋮prelude: t
319
320 ⋮crate::prelude
321 "###);
322}
323
324#[test]
325fn plain_macros_are_legacy_textual_scoped() {
326 let map = def_map(
327 r#"
328 //- /main.rs
329 mod m1;
330 bar!(NotFoundNotMacroUse);
331
332 mod m2 {
333 foo!(NotFoundBeforeInside2);
334 }
335
336 macro_rules! foo {
337 ($x:ident) => { struct $x; }
338 }
339 foo!(Ok);
340
341 mod m3;
342 foo!(OkShadowStop);
343 bar!(NotFoundMacroUseStop);
344
345 #[macro_use]
346 mod m5 {
347 #[macro_use]
348 mod m6 {
349 macro_rules! foo {
350 ($x:ident) => { fn $x() {} }
351 }
352 }
353 }
354 foo!(ok_double_macro_use_shadow);
355
356 baz!(NotFoundBefore);
357 #[macro_use]
358 mod m7 {
359 macro_rules! baz {
360 ($x:ident) => { struct $x; }
361 }
362 }
363 baz!(OkAfter);
364
365 //- /m1.rs
366 foo!(NotFoundBeforeInside1);
367 macro_rules! bar {
368 ($x:ident) => { struct $x; }
369 }
370
371 //- /m3/mod.rs
372 foo!(OkAfterInside);
373 macro_rules! foo {
374 ($x:ident) => { fn $x() {} }
375 }
376 foo!(ok_shadow);
377
378 #[macro_use]
379 mod m4;
380 bar!(OkMacroUse);
381
382 //- /m3/m4.rs
383 foo!(ok_shadow_deep);
384 macro_rules! bar {
385 ($x:ident) => { struct $x; }
386 }
387 "#,
388 );
389 assert_snapshot!(map, @r###"
390 ⋮crate
391 ⋮Ok: t v
392 ⋮OkAfter: t v
393 ⋮OkShadowStop: t v
394 ⋮m1: t
395 ⋮m2: t
396 ⋮m3: t
397 ⋮m5: t
398 ⋮m7: t
399 ⋮ok_double_macro_use_shadow: v
400
401 ⋮crate::m7
402
403 ⋮crate::m1
404
405 ⋮crate::m5
406 ⋮m6: t
407
408 ⋮crate::m5::m6
409
410 ⋮crate::m2
411
412 ⋮crate::m3
413 ⋮OkAfterInside: t v
414 ⋮OkMacroUse: t v
415 ⋮m4: t
416 ⋮ok_shadow: v
417
418 ⋮crate::m3::m4
419 ⋮ok_shadow_deep: v
420 "###);
421}
422
423#[test]
424fn type_value_macro_live_in_different_scopes() {
425 let map = def_map(
426 "
427 //- /main.rs
428 #[macro_export]
429 macro_rules! foo {
430 ($x:ident) => { type $x = (); }
431 }
432
433 foo!(foo);
434 use foo as bar;
435
436 use self::foo as baz;
437 fn baz() {}
438 ",
439 );
440 assert_snapshot!(map, @r###"
441 ⋮crate
442 ⋮bar: t m
443 ⋮baz: t v m
444 ⋮foo: t m
445 "###);
446}
447
448#[test]
449fn macro_use_can_be_aliased() {
450 let map = def_map_with_crate_graph(
451 "
452 //- /main.rs
453 #[macro_use]
454 extern crate foo;
455
456 foo!(Direct);
457 bar!(Alias);
458
459 //- /lib.rs
460 use crate::foo as bar;
461
462 mod m {
463 #[macro_export]
464 macro_rules! foo {
465 ($x:ident) => { struct $x; }
466 }
467 }
468 ",
469 crate_graph! {
470 "main": ("/main.rs", ["foo"]),
471 "foo": ("/lib.rs", []),
472 },
473 );
474 assert_snapshot!(map, @r###"
475 ⋮crate
476 ⋮Alias: t v
477 ⋮Direct: t v
478 ⋮foo: t
479 "###);
480}
481
482#[test]
483fn path_qualified_macros() {
484 let map = def_map(
485 "
486 //- /main.rs
487 macro_rules! foo {
488 ($x:ident) => { struct $x; }
489 }
490
491 crate::foo!(NotResolved);
492
493 crate::bar!(OkCrate);
494 bar!(OkPlain);
495 alias1!(NotHere);
496 m::alias1!(OkAliasPlain);
497 m::alias2!(OkAliasSuper);
498 m::alias3!(OkAliasCrate);
499 not_found!(NotFound);
500
501 mod m {
502 #[macro_export]
503 macro_rules! bar {
504 ($x:ident) => { struct $x; }
505 }
506
507 pub use bar as alias1;
508 pub use super::bar as alias2;
509 pub use crate::bar as alias3;
510 pub use self::bar as not_found;
511 }
512 ",
513 );
514 assert_snapshot!(map, @r###"
515 ⋮crate
516 ⋮OkAliasCrate: t v
517 ⋮OkAliasPlain: t v
518 ⋮OkAliasSuper: t v
519 ⋮OkCrate: t v
520 ⋮OkPlain: t v
521 ⋮bar: m
522 ⋮m: t
523
524 ⋮crate::m
525 ⋮alias1: m
526 ⋮alias2: m
527 ⋮alias3: m
528 ⋮not_found: _
529 "###);
530}
531
532#[test]
533fn macro_dollar_crate_is_correct_in_item() {
534 covers!(macro_dollar_crate_self);
535 covers!(macro_dollar_crate_other);
536 let map = def_map_with_crate_graph(
537 "
538 //- /main.rs
539 #[macro_use]
540 extern crate foo;
541
542 #[macro_use]
543 mod m {
544 macro_rules! current {
545 () => {
546 use $crate::Foo as FooSelf;
547 }
548 }
549 }
550
551 struct Foo;
552
553 current!();
554 not_current1!();
555 foo::not_current2!();
556
557 //- /lib.rs
558 mod m {
559 #[macro_export]
560 macro_rules! not_current1 {
561 () => {
562 use $crate::Bar;
563 }
564 }
565 }
566
567 #[macro_export]
568 macro_rules! not_current2 {
569 () => {
570 use $crate::Baz;
571 }
572 }
573
574 struct Bar;
575 struct Baz;
576 ",
577 crate_graph! {
578 "main": ("/main.rs", ["foo"]),
579 "foo": ("/lib.rs", []),
580 },
581 );
582 assert_snapshot!(map, @r###"
583 ⋮crate
584 ⋮Bar: t v
585 ⋮Baz: t v
586 ⋮Foo: t v
587 ⋮FooSelf: t v
588 ⋮foo: t
589 ⋮m: t
590
591 ⋮crate::m
592 "###);
593}
594
595#[test]
596fn macro_dollar_crate_is_correct_in_indirect_deps() {
597 covers!(macro_dollar_crate_other);
598 // From std
599 let map = def_map_with_crate_graph(
600 r#"
601 //- /main.rs
602 foo!();
603
604 //- /std.rs
605 #[prelude_import]
606 use self::prelude::*;
607
608 pub use core::foo;
609
610 mod prelude {}
611
612 #[macro_use]
613 mod std_macros;
614
615 //- /core.rs
616 #[macro_export]
617 macro_rules! foo {
618 () => {
619 use $crate::bar;
620 }
621 }
622
623 pub struct bar;
624 "#,
625 crate_graph! {
626 "main": ("/main.rs", ["std"]),
627 "std": ("/std.rs", ["core"]),
628 "core": ("/core.rs", []),
629 },
630 );
631 assert_snapshot!(map, @r###"
632 ⋮crate
633 ⋮bar: t v
634 "###);
635}