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