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