]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/macro_expansion_tests/mbe.rs
Merge #11218
[rust.git] / crates / hir_def / src / macro_expansion_tests / mbe.rs
1 //! Tests specific to declarative macros, aka macros by example. This covers
2 //! both stable `macro_rules!` macros as well as unstable `macro` macros.
3
4 mod tt_conversion;
5 mod matching;
6 mod meta_syntax;
7 mod regression;
8
9 use expect_test::expect;
10
11 use crate::macro_expansion_tests::check;
12
13 #[test]
14 fn token_mapping_smoke_test() {
15     check(
16         r#"
17 // +tokenids
18 macro_rules! f {
19     ( struct $ident:ident ) => {
20         struct $ident {
21             map: ::std::collections::HashSet<()>,
22         }
23     };
24 }
25
26 // +tokenids
27 f!(struct MyTraitMap2);
28 "#,
29         expect![[r##"
30 // call ids will be shifted by Shift(30)
31 // +tokenids
32 macro_rules! f {#0
33     (#1 struct#2 $#3ident#4:#5ident#6 )#1 =#7>#8 {#9
34         struct#10 $#11ident#12 {#13
35             map#14:#15 :#16:#17std#18:#19:#20collections#21:#22:#23HashSet#24<#25(#26)#26>#27,#28
36         }#13
37     }#9;#29
38 }#0
39
40 // // +tokenids
41 // f!(struct#1 MyTraitMap2#2);
42 struct#10 MyTraitMap2#32 {#13
43     map#14:#15 ::std#18::collections#21::HashSet#24<#25(#26)#26>#27,#28
44 }#13
45 "##]],
46     );
47 }
48
49 #[test]
50 fn mbe_smoke_test() {
51     check(
52         r#"
53 macro_rules! impl_froms {
54     ($e:ident: $($v:ident),*) => {
55         $(
56             impl From<$v> for $e {
57                 fn from(it: $v) -> $e { $e::$v(it) }
58             }
59         )*
60     }
61 }
62 impl_froms!(TokenTree: Leaf, Subtree);
63 "#,
64         expect![[r#"
65 macro_rules! impl_froms {
66     ($e:ident: $($v:ident),*) => {
67         $(
68             impl From<$v> for $e {
69                 fn from(it: $v) -> $e { $e::$v(it) }
70             }
71         )*
72     }
73 }
74 impl From<Leaf> for TokenTree {
75     fn from(it: Leaf) -> TokenTree {
76         TokenTree::Leaf(it)
77     }
78 }
79 impl From<Subtree> for TokenTree {
80     fn from(it: Subtree) -> TokenTree {
81         TokenTree::Subtree(it)
82     }
83 }
84 "#]],
85     );
86 }
87
88 #[test]
89 fn wrong_nesting_level() {
90     check(
91         r#"
92 macro_rules! m {
93     ($($i:ident);*) => ($i)
94 }
95 m!{a}
96 "#,
97         expect![[r#"
98 macro_rules! m {
99     ($($i:ident);*) => ($i)
100 }
101 /* error: expected simple binding, found nested binding `i` */
102 "#]],
103     );
104 }
105
106 #[test]
107 fn match_by_first_token_literally() {
108     check(
109         r#"
110 macro_rules! m {
111     ($i:ident) => ( mod $i {} );
112     (= $i:ident) => ( fn $i() {} );
113     (+ $i:ident) => ( struct $i; )
114 }
115 m! { foo }
116 m! { = bar }
117 m! { + Baz }
118 "#,
119         expect![[r#"
120 macro_rules! m {
121     ($i:ident) => ( mod $i {} );
122     (= $i:ident) => ( fn $i() {} );
123     (+ $i:ident) => ( struct $i; )
124 }
125 mod foo {}
126 fn bar() {}
127 struct Baz;
128 "#]],
129     );
130 }
131
132 #[test]
133 fn match_by_last_token_literally() {
134     check(
135         r#"
136 macro_rules! m {
137     ($i:ident) => ( mod $i {} );
138     ($i:ident =) => ( fn $i() {} );
139     ($i:ident +) => ( struct $i; )
140 }
141 m! { foo }
142 m! { bar = }
143 m! { Baz + }
144 "#,
145         expect![[r#"
146 macro_rules! m {
147     ($i:ident) => ( mod $i {} );
148     ($i:ident =) => ( fn $i() {} );
149     ($i:ident +) => ( struct $i; )
150 }
151 mod foo {}
152 fn bar() {}
153 struct Baz;
154 "#]],
155     );
156 }
157
158 #[test]
159 fn match_by_ident() {
160     check(
161         r#"
162 macro_rules! m {
163     ($i:ident) => ( mod $i {} );
164     (spam $i:ident) => ( fn $i() {} );
165     (eggs $i:ident) => ( struct $i; )
166 }
167 m! { foo }
168 m! { spam bar }
169 m! { eggs Baz }
170 "#,
171         expect![[r#"
172 macro_rules! m {
173     ($i:ident) => ( mod $i {} );
174     (spam $i:ident) => ( fn $i() {} );
175     (eggs $i:ident) => ( struct $i; )
176 }
177 mod foo {}
178 fn bar() {}
179 struct Baz;
180 "#]],
181     );
182 }
183
184 #[test]
185 fn match_by_separator_token() {
186     check(
187         r#"
188 macro_rules! m {
189     ($($i:ident),*) => ($(mod $i {} )*);
190     ($($i:ident)#*) => ($(fn $i() {} )*);
191     ($i:ident ,# $ j:ident) => ( struct $i; struct $ j; )
192 }
193
194 m! { foo, bar }
195
196 m! { foo# bar }
197
198 m! { Foo,# Bar }
199 "#,
200         expect![[r##"
201 macro_rules! m {
202     ($($i:ident),*) => ($(mod $i {} )*);
203     ($($i:ident)#*) => ($(fn $i() {} )*);
204     ($i:ident ,# $ j:ident) => ( struct $i; struct $ j; )
205 }
206
207 mod foo {}
208 mod bar {}
209
210 fn foo() {}
211 fn bar() {}
212
213 struct Foo;
214 struct Bar;
215 "##]],
216     );
217 }
218
219 #[test]
220 fn test_match_group_pattern_with_multiple_defs() {
221     check(
222         r#"
223 macro_rules! m {
224     ($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
225 }
226 m! { foo, bar }
227 "#,
228         expect![[r#"
229 macro_rules! m {
230     ($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
231 }
232 impl Bar {
233     fn foo() {}
234     fn bar() {}
235 }
236 "#]],
237     );
238 }
239
240 #[test]
241 fn test_match_group_pattern_with_multiple_statement() {
242     check(
243         r#"
244 macro_rules! m {
245     ($($i:ident),*) => ( fn baz() { $($i ();)* } );
246 }
247 m! { foo, bar }
248 "#,
249         expect![[r#"
250 macro_rules! m {
251     ($($i:ident),*) => ( fn baz() { $($i ();)* } );
252 }
253 fn baz() {
254     foo();
255     bar();
256 }
257 "#]],
258     )
259 }
260
261 #[test]
262 fn test_match_group_pattern_with_multiple_statement_without_semi() {
263     check(
264         r#"
265 macro_rules! m {
266     ($($i:ident),*) => ( fn baz() { $($i() );* } );
267 }
268 m! { foo, bar }
269 "#,
270         expect![[r#"
271 macro_rules! m {
272     ($($i:ident),*) => ( fn baz() { $($i() );* } );
273 }
274 fn baz() {
275     foo();
276     bar()
277 }
278 "#]],
279     )
280 }
281
282 #[test]
283 fn test_match_group_empty_fixed_token() {
284     check(
285         r#"
286 macro_rules! m {
287     ($($i:ident)* #abc) => ( fn baz() { $($i ();)* } );
288 }
289 m!{#abc}
290 "#,
291         expect![[r##"
292 macro_rules! m {
293     ($($i:ident)* #abc) => ( fn baz() { $($i ();)* } );
294 }
295 fn baz() {}
296 "##]],
297     )
298 }
299
300 #[test]
301 fn test_match_group_in_subtree() {
302     check(
303         r#"
304 macro_rules! m {
305     (fn $name:ident { $($i:ident)* } ) => ( fn $name() { $($i ();)* } );
306 }
307 m! { fn baz { a b } }
308 "#,
309         expect![[r#"
310 macro_rules! m {
311     (fn $name:ident { $($i:ident)* } ) => ( fn $name() { $($i ();)* } );
312 }
313 fn baz() {
314     a();
315     b();
316 }
317 "#]],
318     )
319 }
320
321 #[test]
322 fn test_expr_order() {
323     check(
324         r#"
325 macro_rules! m {
326     ($ i:expr) => { fn bar() { $ i * 3; } }
327 }
328 // +tree
329 m! { 1 + 2 }
330 "#,
331         expect![[r#"
332 macro_rules! m {
333     ($ i:expr) => { fn bar() { $ i * 3; } }
334 }
335 fn bar() {
336     (1+2)*3;
337 }
338 // MACRO_ITEMS@0..17
339 //   FN@0..17
340 //     FN_KW@0..2 "fn"
341 //     NAME@2..5
342 //       IDENT@2..5 "bar"
343 //     PARAM_LIST@5..7
344 //       L_PAREN@5..6 "("
345 //       R_PAREN@6..7 ")"
346 //     BLOCK_EXPR@7..17
347 //       STMT_LIST@7..17
348 //         L_CURLY@7..8 "{"
349 //         EXPR_STMT@8..16
350 //           BIN_EXPR@8..15
351 //             PAREN_EXPR@8..13
352 //               L_PAREN@8..9 "("
353 //               BIN_EXPR@9..12
354 //                 LITERAL@9..10
355 //                   INT_NUMBER@9..10 "1"
356 //                 PLUS@10..11 "+"
357 //                 LITERAL@11..12
358 //                   INT_NUMBER@11..12 "2"
359 //               R_PAREN@12..13 ")"
360 //             STAR@13..14 "*"
361 //             LITERAL@14..15
362 //               INT_NUMBER@14..15 "3"
363 //           SEMICOLON@15..16 ";"
364 //         R_CURLY@16..17 "}"
365
366 "#]],
367     )
368 }
369
370 #[test]
371 fn test_match_group_with_multichar_sep() {
372     check(
373         r#"
374 macro_rules! m {
375     (fn $name:ident { $($i:literal)* }) => ( fn $name() -> bool { $($i)&&* } );
376 }
377 m! (fn baz { true false } );
378 "#,
379         expect![[r#"
380 macro_rules! m {
381     (fn $name:ident { $($i:literal)* }) => ( fn $name() -> bool { $($i)&&* } );
382 }
383 fn baz() -> bool {
384     true && false
385 }
386 "#]],
387     );
388
389     check(
390         r#"
391 macro_rules! m {
392     (fn $name:ident { $($i:literal)&&* }) => ( fn $name() -> bool { $($i)&&* } );
393 }
394 m! (fn baz { true && false } );
395 "#,
396         expect![[r#"
397 macro_rules! m {
398     (fn $name:ident { $($i:literal)&&* }) => ( fn $name() -> bool { $($i)&&* } );
399 }
400 fn baz() -> bool {
401     true && false
402 }
403 "#]],
404     );
405 }
406
407 #[test]
408 fn test_match_group_zero_match() {
409     check(
410         r#"
411 macro_rules! m { ( $($i:ident)* ) => (); }
412 m!();
413 "#,
414         expect![[r#"
415 macro_rules! m { ( $($i:ident)* ) => (); }
416
417 "#]],
418     );
419 }
420
421 #[test]
422 fn test_match_group_in_group() {
423     check(
424         r#"
425 macro_rules! m {
426     [ $( ( $($i:ident)* ) )* ] => [ ok![$( ( $($i)* ) )*]; ]
427 }
428 m! ( (a b) );
429 "#,
430         expect![[r#"
431 macro_rules! m {
432     [ $( ( $($i:ident)* ) )* ] => [ ok![$( ( $($i)* ) )*]; ]
433 }
434 ok![(a b)];
435 "#]],
436     )
437 }
438
439 #[test]
440 fn test_expand_to_item_list() {
441     check(
442         r#"
443 macro_rules! structs {
444     ($($i:ident),*) => { $(struct $i { field: u32 } )* }
445 }
446
447 // +tree
448 structs!(Foo, Bar);
449             "#,
450         expect![[r#"
451 macro_rules! structs {
452     ($($i:ident),*) => { $(struct $i { field: u32 } )* }
453 }
454
455 struct Foo {
456     field: u32
457 }
458 struct Bar {
459     field: u32
460 }
461 // MACRO_ITEMS@0..40
462 //   STRUCT@0..20
463 //     STRUCT_KW@0..6 "struct"
464 //     NAME@6..9
465 //       IDENT@6..9 "Foo"
466 //     RECORD_FIELD_LIST@9..20
467 //       L_CURLY@9..10 "{"
468 //       RECORD_FIELD@10..19
469 //         NAME@10..15
470 //           IDENT@10..15 "field"
471 //         COLON@15..16 ":"
472 //         PATH_TYPE@16..19
473 //           PATH@16..19
474 //             PATH_SEGMENT@16..19
475 //               NAME_REF@16..19
476 //                 IDENT@16..19 "u32"
477 //       R_CURLY@19..20 "}"
478 //   STRUCT@20..40
479 //     STRUCT_KW@20..26 "struct"
480 //     NAME@26..29
481 //       IDENT@26..29 "Bar"
482 //     RECORD_FIELD_LIST@29..40
483 //       L_CURLY@29..30 "{"
484 //       RECORD_FIELD@30..39
485 //         NAME@30..35
486 //           IDENT@30..35 "field"
487 //         COLON@35..36 ":"
488 //         PATH_TYPE@36..39
489 //           PATH@36..39
490 //             PATH_SEGMENT@36..39
491 //               NAME_REF@36..39
492 //                 IDENT@36..39 "u32"
493 //       R_CURLY@39..40 "}"
494
495             "#]],
496     );
497 }
498
499 #[test]
500 fn test_two_idents() {
501     check(
502         r#"
503 macro_rules! m {
504     ($i:ident, $j:ident) => { fn foo() { let a = $i; let b = $j; } }
505 }
506 m! { foo, bar }
507 "#,
508         expect![[r#"
509 macro_rules! m {
510     ($i:ident, $j:ident) => { fn foo() { let a = $i; let b = $j; } }
511 }
512 fn foo() {
513     let a = foo;
514     let b = bar;
515 }
516 "#]],
517     );
518 }
519
520 #[test]
521 fn test_tt_to_stmts() {
522     check(
523         r#"
524 macro_rules! m {
525     () => {
526         let a = 0;
527         a = 10 + 1;
528         a
529     }
530 }
531
532 fn f() -> i32 {
533     // +tree
534     m!{}
535 }
536 "#,
537         expect![[r#"
538 macro_rules! m {
539     () => {
540         let a = 0;
541         a = 10 + 1;
542         a
543     }
544 }
545
546 fn f() -> i32 {
547     let a = 0;
548     a = 10+1;
549     a
550 // MACRO_STMTS@0..15
551 //   LET_STMT@0..7
552 //     LET_KW@0..3 "let"
553 //     IDENT_PAT@3..4
554 //       NAME@3..4
555 //         IDENT@3..4 "a"
556 //     EQ@4..5 "="
557 //     LITERAL@5..6
558 //       INT_NUMBER@5..6 "0"
559 //     SEMICOLON@6..7 ";"
560 //   EXPR_STMT@7..14
561 //     BIN_EXPR@7..13
562 //       PATH_EXPR@7..8
563 //         PATH@7..8
564 //           PATH_SEGMENT@7..8
565 //             NAME_REF@7..8
566 //               IDENT@7..8 "a"
567 //       EQ@8..9 "="
568 //       BIN_EXPR@9..13
569 //         LITERAL@9..11
570 //           INT_NUMBER@9..11 "10"
571 //         PLUS@11..12 "+"
572 //         LITERAL@12..13
573 //           INT_NUMBER@12..13 "1"
574 //     SEMICOLON@13..14 ";"
575 //   PATH_EXPR@14..15
576 //     PATH@14..15
577 //       PATH_SEGMENT@14..15
578 //         NAME_REF@14..15
579 //           IDENT@14..15 "a"
580
581 }
582 "#]],
583     );
584 }
585
586 #[test]
587 fn test_match_literal() {
588     check(
589         r#"
590 macro_rules! m {
591     ('(') => { fn l_paren() {} }
592 }
593 m!['('];
594 "#,
595         expect![[r#"
596 macro_rules! m {
597     ('(') => { fn l_paren() {} }
598 }
599 fn l_paren() {}
600 "#]],
601     );
602 }
603
604 #[test]
605 fn test_parse_macro_def_simple() {
606     cov_mark::check!(parse_macro_def_simple);
607     check(
608         r#"
609 macro m($id:ident) { fn $id() {} }
610 m!(bar);
611 "#,
612         expect![[r#"
613 macro m($id:ident) { fn $id() {} }
614 fn bar() {}
615 "#]],
616     );
617 }
618
619 #[test]
620 fn test_parse_macro_def_rules() {
621     cov_mark::check!(parse_macro_def_rules);
622
623     check(
624         r#"
625 macro m {
626     ($id:ident) => { fn $id() {} }
627 }
628 m!(bar);
629 "#,
630         expect![[r#"
631 macro m {
632     ($id:ident) => { fn $id() {} }
633 }
634 fn bar() {}
635 "#]],
636     );
637 }
638
639 #[test]
640 fn test_macro_2_0_panic_2015() {
641     check(
642         r#"
643 macro panic_2015 {
644     () => (),
645     (bar) => (),
646 }
647 panic_2015!(bar);
648 "#,
649         expect![[r#"
650 macro panic_2015 {
651     () => (),
652     (bar) => (),
653 }
654
655 "#]],
656     );
657 }
658
659 #[test]
660 fn test_path() {
661     check(
662         r#"
663 macro_rules! m {
664     ($p:path) => { fn foo() { let a = $p; } }
665 }
666
667 m! { foo }
668
669 m! { bar::<u8>::baz::<u8> }
670 "#,
671         expect![[r#"
672 macro_rules! m {
673     ($p:path) => { fn foo() { let a = $p; } }
674 }
675
676 fn foo() {
677     let a = foo;
678 }
679
680 fn foo() {
681     let a = bar::<u8>::baz::<u8> ;
682 }
683 "#]],
684     );
685 }
686
687 #[test]
688 fn test_two_paths() {
689     check(
690         r#"
691 macro_rules! m {
692     ($i:path, $j:path) => { fn foo() { let a = $ i; let b = $j; } }
693 }
694 m! { foo, bar }
695 "#,
696         expect![[r#"
697 macro_rules! m {
698     ($i:path, $j:path) => { fn foo() { let a = $ i; let b = $j; } }
699 }
700 fn foo() {
701     let a = foo;
702     let b = bar;
703 }
704 "#]],
705     );
706 }
707
708 #[test]
709 fn test_path_with_path() {
710     check(
711         r#"
712 macro_rules! m {
713     ($p:path) => { fn foo() { let a = $p::bar; } }
714 }
715 m! { foo }
716 "#,
717         expect![[r#"
718 macro_rules! m {
719     ($p:path) => { fn foo() { let a = $p::bar; } }
720 }
721 fn foo() {
722     let a = foo::bar;
723 }
724 "#]],
725     );
726 }
727
728 #[test]
729 fn test_expr() {
730     check(
731         r#"
732 macro_rules! m {
733     ($e:expr) => { fn bar() { $e; } }
734 }
735
736 m! { 2 + 2 * baz(3).quux() }
737 "#,
738         expect![[r#"
739 macro_rules! m {
740     ($e:expr) => { fn bar() { $e; } }
741 }
742
743 fn bar() {
744     (2+2*baz(3).quux());
745 }
746 "#]],
747     )
748 }
749
750 #[test]
751 fn test_last_expr() {
752     check(
753         r#"
754 macro_rules! vec {
755     ($($item:expr),*) => {{
756             let mut v = Vec::new();
757             $( v.push($item); )*
758             v
759     }};
760 }
761
762 fn f() {
763     vec![1,2,3];
764 }
765 "#,
766         expect![[r#"
767 macro_rules! vec {
768     ($($item:expr),*) => {{
769             let mut v = Vec::new();
770             $( v.push($item); )*
771             v
772     }};
773 }
774
775 fn f() {
776      {
777         let mut v = Vec::new();
778         v.push(1);
779         v.push(2);
780         v.push(3);
781         v
782     };
783 }
784 "#]],
785     );
786 }
787
788 #[test]
789 fn test_expr_with_attr() {
790     check(
791         r#"
792 macro_rules! m { ($a:expr) => { ok!(); } }
793 m!(#[allow(a)]());
794 "#,
795         expect![[r#"
796 macro_rules! m { ($a:expr) => { ok!(); } }
797 ok!();
798 "#]],
799     )
800 }
801
802 #[test]
803 fn test_ty() {
804     check(
805         r#"
806 macro_rules! m {
807     ($t:ty) => ( fn bar() -> $t {} )
808 }
809 m! { Baz<u8> }
810 "#,
811         expect![[r#"
812 macro_rules! m {
813     ($t:ty) => ( fn bar() -> $t {} )
814 }
815 fn bar() -> Baz<u8> {}
816 "#]],
817     )
818 }
819
820 #[test]
821 fn test_ty_with_complex_type() {
822     check(
823         r#"
824 macro_rules! m {
825     ($t:ty) => ( fn bar() -> $ t {} )
826 }
827
828 m! { &'a Baz<u8> }
829
830 m! { extern "Rust" fn() -> Ret }
831 "#,
832         expect![[r#"
833 macro_rules! m {
834     ($t:ty) => ( fn bar() -> $ t {} )
835 }
836
837 fn bar() -> & 'a Baz<u8> {}
838
839 fn bar() -> extern "Rust"fn() -> Ret {}
840 "#]],
841     );
842 }
843
844 #[test]
845 fn test_pat_() {
846     check(
847         r#"
848 macro_rules! m {
849     ($p:pat) => { fn foo() { let $p; } }
850 }
851 m! { (a, b) }
852 "#,
853         expect![[r#"
854 macro_rules! m {
855     ($p:pat) => { fn foo() { let $p; } }
856 }
857 fn foo() {
858     let (a, b);
859 }
860 "#]],
861     );
862 }
863
864 #[test]
865 fn test_stmt() {
866     check(
867         r#"
868 macro_rules! m {
869     ($s:stmt) => ( fn bar() { $s; } )
870 }
871 m! { 2 }
872 m! { let a = 0 }
873 "#,
874         expect![[r#"
875 macro_rules! m {
876     ($s:stmt) => ( fn bar() { $s; } )
877 }
878 fn bar() {
879     2;
880 }
881 fn bar() {
882     let a = 0;
883 }
884 "#]],
885     )
886 }
887
888 #[test]
889 fn test_single_item() {
890     check(
891         r#"
892 macro_rules! m { ($i:item) => ( $i ) }
893 m! { mod c {} }
894 "#,
895         expect![[r#"
896 macro_rules! m { ($i:item) => ( $i ) }
897 mod c {}
898 "#]],
899     )
900 }
901
902 #[test]
903 fn test_all_items() {
904     check(
905         r#"
906 macro_rules! m { ($($i:item)*) => ($($i )*) }
907 m! {
908     extern crate a;
909     mod b;
910     mod c {}
911     use d;
912     const E: i32 = 0;
913     static F: i32 = 0;
914     impl G {}
915     struct H;
916     enum I { Foo }
917     trait J {}
918     fn h() {}
919     extern {}
920     type T = u8;
921 }
922 "#,
923         expect![[r#"
924 macro_rules! m { ($($i:item)*) => ($($i )*) }
925 extern crate a;
926 mod b;
927 mod c {}
928 use d;
929 const E: i32 = 0;
930 static F: i32 = 0;
931 impl G {}
932 struct H;
933 enum I {
934     Foo
935 }
936 trait J {}
937 fn h() {}
938 extern {}
939 type T = u8;
940 "#]],
941     );
942 }
943
944 #[test]
945 fn test_block() {
946     check(
947         r#"
948 macro_rules! m { ($b:block) => { fn foo() $b } }
949 m! { { 1; } }
950 "#,
951         expect![[r#"
952 macro_rules! m { ($b:block) => { fn foo() $b } }
953 fn foo() {
954     1;
955 }
956 "#]],
957     );
958 }
959
960 #[test]
961 fn test_meta() {
962     check(
963         r#"
964 macro_rules! m {
965     ($m:meta) => ( #[$m] fn bar() {} )
966 }
967 m! { cfg(target_os = "windows") }
968 m! { hello::world }
969 "#,
970         expect![[r##"
971 macro_rules! m {
972     ($m:meta) => ( #[$m] fn bar() {} )
973 }
974 #[cfg(target_os = "windows")] fn bar() {}
975 #[hello::world] fn bar() {}
976 "##]],
977     );
978 }
979
980 #[test]
981 fn test_meta_doc_comments() {
982     cov_mark::check!(test_meta_doc_comments);
983     check(
984         r#"
985 macro_rules! m {
986     ($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
987 }
988 m! {
989     /// Single Line Doc 1
990     /**
991         MultiLines Doc
992     */
993 }
994 "#,
995         expect![[r##"
996 macro_rules! m {
997     ($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
998 }
999 #[doc = " Single Line Doc 1"]
1000 #[doc = "\n        MultiLines Doc\n    "] fn bar() {}
1001 "##]],
1002     );
1003 }
1004
1005 #[test]
1006 fn test_meta_extended_key_value_attributes() {
1007     check(
1008         r#"
1009 macro_rules! m {
1010     (#[$m:meta]) => ( #[$m] fn bar() {} )
1011 }
1012 m! { #[doc = concat!("The `", "bla", "` lang item.")] }
1013 "#,
1014         expect![[r##"
1015 macro_rules! m {
1016     (#[$m:meta]) => ( #[$m] fn bar() {} )
1017 }
1018 #[doc = concat!("The `", "bla", "` lang item.")] fn bar() {}
1019 "##]],
1020     );
1021 }
1022
1023 #[test]
1024 fn test_meta_doc_comments_non_latin() {
1025     check(
1026         r#"
1027 macro_rules! m {
1028     ($(#[$ m:meta])+) => ( $(#[$m])+ fn bar() {} )
1029 }
1030 m! {
1031     /// 錦瑟無端五十弦,一弦一柱思華年。
1032     /**
1033         莊生曉夢迷蝴蝶,望帝春心託杜鵑。
1034     */
1035 }
1036 "#,
1037         expect![[r##"
1038 macro_rules! m {
1039     ($(#[$ m:meta])+) => ( $(#[$m])+ fn bar() {} )
1040 }
1041 #[doc = " 錦瑟無端五十弦,一弦一柱思華年。"]
1042 #[doc = "\n        莊生曉夢迷蝴蝶,望帝春心託杜鵑。\n    "] fn bar() {}
1043 "##]],
1044     );
1045 }
1046
1047 #[test]
1048 fn test_meta_doc_comments_escaped_characters() {
1049     check(
1050         r#"
1051 macro_rules! m {
1052     ($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
1053 }
1054 m! {
1055     /// \ " '
1056 }
1057 "#,
1058         expect![[r##"
1059 macro_rules! m {
1060     ($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
1061 }
1062 #[doc = " \\ \" \'"] fn bar() {}
1063 "##]],
1064     );
1065 }
1066
1067 #[test]
1068 fn test_tt_block() {
1069     check(
1070         r#"
1071 macro_rules! m { ($tt:tt) => { fn foo() $tt } }
1072 m! { { 1; } }
1073 "#,
1074         expect![[r#"
1075 macro_rules! m { ($tt:tt) => { fn foo() $tt } }
1076 fn foo() {
1077     1;
1078 }
1079 "#]],
1080     );
1081 }
1082
1083 #[test]
1084 fn test_tt_group() {
1085     check(
1086         r#"
1087 macro_rules! m { ($($tt:tt)*) => { $($tt)* } }
1088 m! { fn foo() {} }"
1089 "#,
1090         expect![[r#"
1091 macro_rules! m { ($($tt:tt)*) => { $($tt)* } }
1092 fn foo() {}"
1093 "#]],
1094     );
1095 }
1096
1097 #[test]
1098 fn test_tt_composite() {
1099     check(
1100         r#"
1101 macro_rules! m { ($tt:tt) => { ok!(); } }
1102 m! { => }
1103 m! { = > }
1104 "#,
1105         expect![[r#"
1106 macro_rules! m { ($tt:tt) => { ok!(); } }
1107 ok!();
1108 /* error: leftover tokens */ok!();
1109 "#]],
1110     );
1111 }
1112
1113 #[test]
1114 fn test_tt_composite2() {
1115     check(
1116         r#"
1117 macro_rules! m { ($($tt:tt)*) => { abs!(=> $($tt)*); } }
1118 m! {#}
1119 "#,
1120         expect![[r##"
1121 macro_rules! m { ($($tt:tt)*) => { abs!(=> $($tt)*); } }
1122 abs!( = > #);
1123 "##]],
1124     );
1125 }
1126
1127 #[test]
1128 fn test_tt_with_composite_without_space() {
1129     // Test macro input without any spaces
1130     // See https://github.com/rust-analyzer/rust-analyzer/issues/6692
1131     check(
1132         r#"
1133 macro_rules! m { ($ op:tt, $j:path) => ( ok!(); ) }
1134 m!(==,Foo::Bool)
1135 "#,
1136         expect![[r#"
1137 macro_rules! m { ($ op:tt, $j:path) => ( ok!(); ) }
1138 ok!();
1139 "#]],
1140     );
1141 }
1142
1143 #[test]
1144 fn test_underscore() {
1145     check(
1146         r#"
1147 macro_rules! m { ($_:tt) => { ok!(); } }
1148 m! { => }
1149 "#,
1150         expect![[r#"
1151 macro_rules! m { ($_:tt) => { ok!(); } }
1152 ok!();
1153 "#]],
1154     );
1155 }
1156
1157 #[test]
1158 fn test_underscore_not_greedily() {
1159     check(
1160         r#"
1161 // `_` overlaps with `$a:ident` but rustc matches it under the `_` token.
1162 macro_rules! m1 {
1163     ($($a:ident)* _) => { ok!(); }
1164 }
1165 m1![a b c d _];
1166
1167 // `_ => ou` overlaps with `$a:expr => $b:ident` but rustc matches it under `_ => $c:expr`.
1168 macro_rules! m2 {
1169     ($($a:expr => $b:ident)* _ => $c:expr) => { ok!(); }
1170 }
1171 m2![a => b c => d _ => ou]
1172 "#,
1173         expect![[r#"
1174 // `_` overlaps with `$a:ident` but rustc matches it under the `_` token.
1175 macro_rules! m1 {
1176     ($($a:ident)* _) => { ok!(); }
1177 }
1178 ok!();
1179
1180 // `_ => ou` overlaps with `$a:expr => $b:ident` but rustc matches it under `_ => $c:expr`.
1181 macro_rules! m2 {
1182     ($($a:expr => $b:ident)* _ => $c:expr) => { ok!(); }
1183 }
1184 ok!();
1185 "#]],
1186     );
1187 }
1188
1189 #[test]
1190 fn test_underscore_flavors() {
1191     check(
1192         r#"
1193 macro_rules! m1 { ($a:ty) => { ok!(); } }
1194 m1![_];
1195
1196 macro_rules! m2 { ($a:lifetime) => { ok!(); } }
1197 m2!['_];
1198 "#,
1199         expect![[r#"
1200 macro_rules! m1 { ($a:ty) => { ok!(); } }
1201 ok!();
1202
1203 macro_rules! m2 { ($a:lifetime) => { ok!(); } }
1204 ok!();
1205 "#]],
1206     );
1207 }
1208
1209 #[test]
1210 fn test_vertical_bar_with_pat() {
1211     check(
1212         r#"
1213 macro_rules! m { (|$pat:pat| ) => { ok!(); } }
1214 m! { |x| }
1215  "#,
1216         expect![[r#"
1217 macro_rules! m { (|$pat:pat| ) => { ok!(); } }
1218 ok!();
1219  "#]],
1220     );
1221 }
1222
1223 #[test]
1224 fn test_dollar_crate_lhs_is_not_meta() {
1225     check(
1226         r#"
1227 macro_rules! m {
1228     ($crate) => { err!(); };
1229     () => { ok!(); };
1230 }
1231 m!{}
1232 "#,
1233         expect![[r#"
1234 macro_rules! m {
1235     ($crate) => { err!(); };
1236     () => { ok!(); };
1237 }
1238 ok!();
1239 "#]],
1240     );
1241 }
1242
1243 #[test]
1244 fn test_lifetime() {
1245     check(
1246         r#"
1247 macro_rules! m {
1248     ($lt:lifetime) => { struct Ref<$lt>{ s: &$ lt str } }
1249 }
1250 m! {'a}
1251 "#,
1252         expect![[r#"
1253 macro_rules! m {
1254     ($lt:lifetime) => { struct Ref<$lt>{ s: &$ lt str } }
1255 }
1256 struct Ref<'a> {
1257     s: &'a str
1258 }
1259 "#]],
1260     );
1261 }
1262
1263 #[test]
1264 fn test_literal() {
1265     check(
1266         r#"
1267 macro_rules! m {
1268     ($type:ty, $lit:literal) => { const VALUE: $type = $ lit; };
1269 }
1270 m!(u8, 0);
1271 "#,
1272         expect![[r#"
1273 macro_rules! m {
1274     ($type:ty, $lit:literal) => { const VALUE: $type = $ lit; };
1275 }
1276 const VALUE: u8 = 0;
1277 "#]],
1278     );
1279
1280     check(
1281         r#"
1282 macro_rules! m {
1283     ($type:ty, $lit:literal) => { const VALUE: $ type = $ lit; };
1284 }
1285 m!(i32, -1);
1286 "#,
1287         expect![[r#"
1288 macro_rules! m {
1289     ($type:ty, $lit:literal) => { const VALUE: $ type = $ lit; };
1290 }
1291 const VALUE: i32 = -1;
1292 "#]],
1293     );
1294 }
1295
1296 #[test]
1297 fn test_boolean_is_ident() {
1298     check(
1299         r#"
1300 macro_rules! m {
1301     ($lit0:literal, $lit1:literal) => { const VALUE: (bool, bool) = ($lit0, $lit1); };
1302 }
1303 m!(true, false);
1304 "#,
1305         expect![[r#"
1306 macro_rules! m {
1307     ($lit0:literal, $lit1:literal) => { const VALUE: (bool, bool) = ($lit0, $lit1); };
1308 }
1309 const VALUE: (bool, bool) = (true , false );
1310 "#]],
1311     );
1312 }
1313
1314 #[test]
1315 fn test_vis() {
1316     check(
1317         r#"
1318 macro_rules! m {
1319     ($vis:vis $name:ident) => { $vis fn $name() {} }
1320 }
1321 m!(pub foo);
1322 m!(foo);
1323 "#,
1324         expect![[r#"
1325 macro_rules! m {
1326     ($vis:vis $name:ident) => { $vis fn $name() {} }
1327 }
1328 pub fn foo() {}
1329 fn foo() {}
1330 "#]],
1331     );
1332 }
1333
1334 #[test]
1335 fn test_inner_macro_rules() {
1336     check(
1337         r#"
1338 macro_rules! m {
1339     ($a:ident, $b:ident, $c:tt) => {
1340         macro_rules! inner {
1341             ($bi:ident) => { fn $bi() -> u8 { $c } }
1342         }
1343
1344         inner!($a);
1345         fn $b() -> u8 { $c }
1346     }
1347 }
1348 m!(x, y, 1);
1349 "#,
1350         expect![[r#"
1351 macro_rules! m {
1352     ($a:ident, $b:ident, $c:tt) => {
1353         macro_rules! inner {
1354             ($bi:ident) => { fn $bi() -> u8 { $c } }
1355         }
1356
1357         inner!($a);
1358         fn $b() -> u8 { $c }
1359     }
1360 }
1361 macro_rules !inner {
1362     ($bi: ident) = > {
1363         fn $bi()-> u8 {
1364             1
1365         }
1366     }
1367 }
1368 inner!(x);
1369 fn y() -> u8 {
1370     1
1371 }
1372 "#]],
1373     );
1374 }
1375
1376 #[test]
1377 fn test_expr_after_path_colons() {
1378     check(
1379         r#"
1380 macro_rules! m {
1381     ($k:expr) => { fn f() { K::$k; } }
1382 }
1383 // +tree +errors
1384 m!(C("0"));
1385 "#,
1386         expect![[r#"
1387 macro_rules! m {
1388     ($k:expr) => { fn f() { K::$k; } }
1389 }
1390 /* parse error: expected identifier */
1391 /* parse error: expected SEMICOLON */
1392 /* parse error: expected SEMICOLON */
1393 /* parse error: expected expression */
1394 fn f() {
1395     K::(C("0"));
1396 }
1397 // MACRO_ITEMS@0..19
1398 //   FN@0..19
1399 //     FN_KW@0..2 "fn"
1400 //     NAME@2..3
1401 //       IDENT@2..3 "f"
1402 //     PARAM_LIST@3..5
1403 //       L_PAREN@3..4 "("
1404 //       R_PAREN@4..5 ")"
1405 //     BLOCK_EXPR@5..19
1406 //       STMT_LIST@5..19
1407 //         L_CURLY@5..6 "{"
1408 //         EXPR_STMT@6..10
1409 //           PATH_EXPR@6..10
1410 //             PATH@6..10
1411 //               PATH@6..7
1412 //                 PATH_SEGMENT@6..7
1413 //                   NAME_REF@6..7
1414 //                     IDENT@6..7 "K"
1415 //               COLON2@7..9 "::"
1416 //               ERROR@9..10
1417 //                 L_PAREN@9..10 "("
1418 //         EXPR_STMT@10..16
1419 //           CALL_EXPR@10..16
1420 //             PATH_EXPR@10..11
1421 //               PATH@10..11
1422 //                 PATH_SEGMENT@10..11
1423 //                   NAME_REF@10..11
1424 //                     IDENT@10..11 "C"
1425 //             ARG_LIST@11..16
1426 //               L_PAREN@11..12 "("
1427 //               LITERAL@12..15
1428 //                 STRING@12..15 "\"0\""
1429 //               R_PAREN@15..16 ")"
1430 //         ERROR@16..17
1431 //           R_PAREN@16..17 ")"
1432 //         SEMICOLON@17..18 ";"
1433 //         R_CURLY@18..19 "}"
1434
1435 "#]],
1436     );
1437 }
1438
1439 #[test]
1440 fn test_match_is_not_greedy() {
1441     check(
1442         r#"
1443 macro_rules! foo {
1444     ($($i:ident $(,)*),*) => {};
1445 }
1446 foo!(a,b);
1447 "#,
1448         expect![[r#"
1449 macro_rules! foo {
1450     ($($i:ident $(,)*),*) => {};
1451 }
1452
1453 "#]],
1454     );
1455 }
1456
1457 #[test]
1458 fn expr_interpolation() {
1459     check(
1460         r#"
1461 macro_rules! m { ($expr:expr) => { map($expr) } }
1462 fn f() {
1463     let _ = m!(x + foo);
1464 }
1465 "#,
1466         expect![[r#"
1467 macro_rules! m { ($expr:expr) => { map($expr) } }
1468 fn f() {
1469     let _ = map((x+foo));
1470 }
1471 "#]],
1472     )
1473 }
1474
1475 #[test]
1476 fn mbe_are_not_attributes() {
1477     check(
1478         r#"
1479 macro_rules! error {
1480     () => {struct Bar}
1481 }
1482
1483 #[error]
1484 struct Foo;
1485 "#,
1486         expect![[r##"
1487 macro_rules! error {
1488     () => {struct Bar}
1489 }
1490
1491 #[error]
1492 struct Foo;
1493 "##]],
1494     )
1495 }