1 //! Tests specific to declarative macros, aka macros by example. This covers
2 //! both stable `macro_rules!` macros as well as unstable `macro` macros.
9 use expect_test::expect;
11 use crate::macro_expansion_tests::check;
14 fn token_mapping_smoke_test() {
19 ( struct $ident:ident ) => {
21 map: ::std::collections::HashSet<()>,
27 f!(struct MyTraitMap2);
30 // call ids will be shifted by Shift(30)
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
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
53 macro_rules! impl_froms {
54 ($e:ident: $($v:ident),*) => {
56 impl From<$v> for $e {
57 fn from(it: $v) -> $e { $e::$v(it) }
62 impl_froms!(TokenTree: Leaf, Subtree);
65 macro_rules! impl_froms {
66 ($e:ident: $($v:ident),*) => {
68 impl From<$v> for $e {
69 fn from(it: $v) -> $e { $e::$v(it) }
74 impl From<Leaf> for TokenTree {
75 fn from(it: Leaf) -> TokenTree {
79 impl From<Subtree> for TokenTree {
80 fn from(it: Subtree) -> TokenTree {
81 TokenTree::Subtree(it)
89 fn wrong_nesting_level() {
93 ($($i:ident);*) => ($i)
99 ($($i:ident);*) => ($i)
101 /* error: expected simple binding, found nested binding `i` */
107 fn match_by_first_token_literally() {
111 ($i:ident) => ( mod $i {} );
112 (= $i:ident) => ( fn $i() {} );
113 (+ $i:ident) => ( struct $i; )
121 ($i:ident) => ( mod $i {} );
122 (= $i:ident) => ( fn $i() {} );
123 (+ $i:ident) => ( struct $i; )
133 fn match_by_last_token_literally() {
137 ($i:ident) => ( mod $i {} );
138 ($i:ident =) => ( fn $i() {} );
139 ($i:ident +) => ( struct $i; )
147 ($i:ident) => ( mod $i {} );
148 ($i:ident =) => ( fn $i() {} );
149 ($i:ident +) => ( struct $i; )
159 fn match_by_ident() {
163 ($i:ident) => ( mod $i {} );
164 (spam $i:ident) => ( fn $i() {} );
165 (eggs $i:ident) => ( struct $i; )
173 ($i:ident) => ( mod $i {} );
174 (spam $i:ident) => ( fn $i() {} );
175 (eggs $i:ident) => ( struct $i; )
185 fn match_by_separator_token() {
189 ($($i:ident),*) => ($(mod $i {} )*);
190 ($($i:ident)#*) => ($(fn $i() {} )*);
191 ($i:ident ,# $ j:ident) => ( struct $i; struct $ j; )
202 ($($i:ident),*) => ($(mod $i {} )*);
203 ($($i:ident)#*) => ($(fn $i() {} )*);
204 ($i:ident ,# $ j:ident) => ( struct $i; struct $ j; )
220 fn test_match_group_pattern_with_multiple_defs() {
224 ($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
230 ($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
241 fn test_match_group_pattern_with_multiple_statement() {
245 ($($i:ident),*) => ( fn baz() { $($i ();)* } );
251 ($($i:ident),*) => ( fn baz() { $($i ();)* } );
262 fn test_match_group_pattern_with_multiple_statement_without_semi() {
266 ($($i:ident),*) => ( fn baz() { $($i() );* } );
272 ($($i:ident),*) => ( fn baz() { $($i() );* } );
283 fn test_match_group_empty_fixed_token() {
287 ($($i:ident)* #abc) => ( fn baz() { $($i ();)* } );
293 ($($i:ident)* #abc) => ( fn baz() { $($i ();)* } );
301 fn test_match_group_in_subtree() {
305 (fn $name:ident { $($i:ident)* } ) => ( fn $name() { $($i ();)* } );
307 m! { fn baz { a b } }
311 (fn $name:ident { $($i:ident)* } ) => ( fn $name() { $($i ();)* } );
322 fn test_expr_order() {
326 ($ i:expr) => { fn bar() { $ i * 3; } }
333 ($ i:expr) => { fn bar() { $ i * 3; } }
355 // INT_NUMBER@9..10 "1"
358 // INT_NUMBER@11..12 "2"
359 // R_PAREN@12..13 ")"
362 // INT_NUMBER@14..15 "3"
363 // SEMICOLON@15..16 ";"
364 // R_CURLY@16..17 "}"
371 fn test_match_group_with_multichar_sep() {
375 (fn $name:ident { $($i:literal)* }) => ( fn $name() -> bool { $($i)&&* } );
377 m! (fn baz { true false } );
381 (fn $name:ident { $($i:literal)* }) => ( fn $name() -> bool { $($i)&&* } );
392 (fn $name:ident { $($i:literal)&&* }) => ( fn $name() -> bool { $($i)&&* } );
394 m! (fn baz { true && false } );
398 (fn $name:ident { $($i:literal)&&* }) => ( fn $name() -> bool { $($i)&&* } );
408 fn test_match_group_zero_match() {
411 macro_rules! m { ( $($i:ident)* ) => (); }
415 macro_rules! m { ( $($i:ident)* ) => (); }
422 fn test_match_group_in_group() {
426 [ $( ( $($i:ident)* ) )* ] => [ ok![$( ( $($i)* ) )*]; ]
432 [ $( ( $($i:ident)* ) )* ] => [ ok![$( ( $($i)* ) )*]; ]
440 fn test_expand_to_item_list() {
443 macro_rules! structs {
444 ($($i:ident),*) => { $(struct $i { field: u32 } )* }
451 macro_rules! structs {
452 ($($i:ident),*) => { $(struct $i { field: u32 } )* }
463 // STRUCT_KW@0..6 "struct"
466 // RECORD_FIELD_LIST@9..20
468 // RECORD_FIELD@10..19
470 // IDENT@10..15 "field"
474 // PATH_SEGMENT@16..19
476 // IDENT@16..19 "u32"
477 // R_CURLY@19..20 "}"
479 // STRUCT_KW@20..26 "struct"
481 // IDENT@26..29 "Bar"
482 // RECORD_FIELD_LIST@29..40
483 // L_CURLY@29..30 "{"
484 // RECORD_FIELD@30..39
486 // IDENT@30..35 "field"
490 // PATH_SEGMENT@36..39
492 // IDENT@36..39 "u32"
493 // R_CURLY@39..40 "}"
500 fn test_two_idents() {
504 ($i:ident, $j:ident) => { fn foo() { let a = $i; let b = $j; } }
510 ($i:ident, $j:ident) => { fn foo() { let a = $i; let b = $j; } }
521 fn test_tt_to_stmts() {
558 // INT_NUMBER@5..6 "0"
559 // SEMICOLON@6..7 ";"
570 // INT_NUMBER@9..11 "10"
573 // INT_NUMBER@12..13 "1"
574 // SEMICOLON@13..14 ";"
577 // PATH_SEGMENT@14..15
587 fn test_match_literal() {
591 ('(') => { fn l_paren() {} }
597 ('(') => { fn l_paren() {} }
605 fn test_parse_macro_def_simple() {
606 cov_mark::check!(parse_macro_def_simple);
609 macro m($id:ident) { fn $id() {} }
613 macro m($id:ident) { fn $id() {} }
620 fn test_parse_macro_def_rules() {
621 cov_mark::check!(parse_macro_def_rules);
626 ($id:ident) => { fn $id() {} }
632 ($id:ident) => { fn $id() {} }
640 fn test_macro_2_0_panic_2015() {
664 ($p:path) => { fn foo() { let a = $p; } }
669 m! { bar::<u8>::baz::<u8> }
673 ($p:path) => { fn foo() { let a = $p; } }
681 let a = bar::<u8>::baz::<u8> ;
688 fn test_two_paths() {
692 ($i:path, $j:path) => { fn foo() { let a = $ i; let b = $j; } }
698 ($i:path, $j:path) => { fn foo() { let a = $ i; let b = $j; } }
709 fn test_path_with_path() {
713 ($p:path) => { fn foo() { let a = $p::bar; } }
719 ($p:path) => { fn foo() { let a = $p::bar; } }
733 ($e:expr) => { fn bar() { $e; } }
736 m! { 2 + 2 * baz(3).quux() }
740 ($e:expr) => { fn bar() { $e; } }
751 fn test_last_expr() {
755 ($($item:expr),*) => {{
756 let mut v = Vec::new();
768 ($($item:expr),*) => {{
769 let mut v = Vec::new();
777 let mut v = Vec::new();
789 fn test_expr_with_attr() {
792 macro_rules! m { ($a:expr) => { ok!(); } }
796 macro_rules! m { ($a:expr) => { ok!(); } }
807 ($t:ty) => ( fn bar() -> $t {} )
813 ($t:ty) => ( fn bar() -> $t {} )
815 fn bar() -> Baz<u8> {}
821 fn test_ty_with_complex_type() {
825 ($t:ty) => ( fn bar() -> $ t {} )
830 m! { extern "Rust" fn() -> Ret }
834 ($t:ty) => ( fn bar() -> $ t {} )
837 fn bar() -> & 'a Baz<u8> {}
839 fn bar() -> extern "Rust"fn() -> Ret {}
849 ($p:pat) => { fn foo() { let $p; } }
855 ($p:pat) => { fn foo() { let $p; } }
869 ($s:stmt) => ( fn bar() { $s; } )
876 ($s:stmt) => ( fn bar() { $s; } )
889 fn test_single_item() {
892 macro_rules! m { ($i:item) => ( $i ) }
896 macro_rules! m { ($i:item) => ( $i ) }
903 fn test_all_items() {
906 macro_rules! m { ($($i:item)*) => ($($i )*) }
924 macro_rules! m { ($($i:item)*) => ($($i )*) }
948 macro_rules! m { ($b:block) => { fn foo() $b } }
952 macro_rules! m { ($b:block) => { fn foo() $b } }
965 ($m:meta) => ( #[$m] fn bar() {} )
967 m! { cfg(target_os = "windows") }
972 ($m:meta) => ( #[$m] fn bar() {} )
974 #[cfg(target_os = "windows")] fn bar() {}
975 #[hello::world] fn bar() {}
981 fn test_meta_doc_comments() {
982 cov_mark::check!(test_meta_doc_comments);
986 ($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
989 /// Single Line Doc 1
997 ($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
999 #[doc = " Single Line Doc 1"]
1000 #[doc = "\n MultiLines Doc\n "] fn bar() {}
1006 fn test_meta_extended_key_value_attributes() {
1010 (#[$m:meta]) => ( #[$m] fn bar() {} )
1012 m! { #[doc = concat!("The `", "bla", "` lang item.")] }
1016 (#[$m:meta]) => ( #[$m] fn bar() {} )
1018 #[doc = concat!("The `", "bla", "` lang item.")] fn bar() {}
1024 fn test_meta_doc_comments_non_latin() {
1028 ($(#[$ m:meta])+) => ( $(#[$m])+ fn bar() {} )
1031 /// 錦瑟無端五十弦,一弦一柱思華年。
1039 ($(#[$ m:meta])+) => ( $(#[$m])+ fn bar() {} )
1041 #[doc = " 錦瑟無端五十弦,一弦一柱思華年。"]
1042 #[doc = "\n 莊生曉夢迷蝴蝶,望帝春心託杜鵑。\n "] fn bar() {}
1048 fn test_meta_doc_comments_escaped_characters() {
1052 ($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
1060 ($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
1062 #[doc = " \\ \" \'"] fn bar() {}
1068 fn test_tt_block() {
1071 macro_rules! m { ($tt:tt) => { fn foo() $tt } }
1075 macro_rules! m { ($tt:tt) => { fn foo() $tt } }
1084 fn test_tt_group() {
1087 macro_rules! m { ($($tt:tt)*) => { $($tt)* } }
1091 macro_rules! m { ($($tt:tt)*) => { $($tt)* } }
1098 fn test_tt_composite() {
1101 macro_rules! m { ($tt:tt) => { ok!(); } }
1106 macro_rules! m { ($tt:tt) => { ok!(); } }
1108 /* error: leftover tokens */ok!();
1114 fn test_tt_composite2() {
1117 macro_rules! m { ($($tt:tt)*) => { abs!(=> $($tt)*); } }
1121 macro_rules! m { ($($tt:tt)*) => { abs!(=> $($tt)*); } }
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
1133 macro_rules! m { ($ op:tt, $j:path) => ( ok!(); ) }
1137 macro_rules! m { ($ op:tt, $j:path) => ( ok!(); ) }
1144 fn test_underscore() {
1147 macro_rules! m { ($_:tt) => { ok!(); } }
1151 macro_rules! m { ($_:tt) => { ok!(); } }
1158 fn test_underscore_not_greedily() {
1161 // `_` overlaps with `$a:ident` but rustc matches it under the `_` token.
1163 ($($a:ident)* _) => { ok!(); }
1167 // `_ => ou` overlaps with `$a:expr => $b:ident` but rustc matches it under `_ => $c:expr`.
1169 ($($a:expr => $b:ident)* _ => $c:expr) => { ok!(); }
1171 m2![a => b c => d _ => ou]
1174 // `_` overlaps with `$a:ident` but rustc matches it under the `_` token.
1176 ($($a:ident)* _) => { ok!(); }
1180 // `_ => ou` overlaps with `$a:expr => $b:ident` but rustc matches it under `_ => $c:expr`.
1182 ($($a:expr => $b:ident)* _ => $c:expr) => { ok!(); }
1190 fn test_underscore_flavors() {
1193 macro_rules! m1 { ($a:ty) => { ok!(); } }
1196 macro_rules! m2 { ($a:lifetime) => { ok!(); } }
1200 macro_rules! m1 { ($a:ty) => { ok!(); } }
1203 macro_rules! m2 { ($a:lifetime) => { ok!(); } }
1210 fn test_vertical_bar_with_pat() {
1213 macro_rules! m { (|$pat:pat| ) => { ok!(); } }
1217 macro_rules! m { (|$pat:pat| ) => { ok!(); } }
1224 fn test_dollar_crate_lhs_is_not_meta() {
1228 ($crate) => { err!(); };
1235 ($crate) => { err!(); };
1244 fn test_lifetime() {
1248 ($lt:lifetime) => { struct Ref<$lt>{ s: &$ lt str } }
1254 ($lt:lifetime) => { struct Ref<$lt>{ s: &$ lt str } }
1268 ($type:ty, $lit:literal) => { const VALUE: $type = $ lit; };
1274 ($type:ty, $lit:literal) => { const VALUE: $type = $ lit; };
1276 const VALUE: u8 = 0;
1283 ($type:ty, $lit:literal) => { const VALUE: $ type = $ lit; };
1289 ($type:ty, $lit:literal) => { const VALUE: $ type = $ lit; };
1291 const VALUE: i32 = -1;
1297 fn test_boolean_is_ident() {
1301 ($lit0:literal, $lit1:literal) => { const VALUE: (bool, bool) = ($lit0, $lit1); };
1307 ($lit0:literal, $lit1:literal) => { const VALUE: (bool, bool) = ($lit0, $lit1); };
1309 const VALUE: (bool, bool) = (true , false );
1319 ($vis:vis $name:ident) => { $vis fn $name() {} }
1326 ($vis:vis $name:ident) => { $vis fn $name() {} }
1335 fn test_inner_macro_rules() {
1339 ($a:ident, $b:ident, $c:tt) => {
1340 macro_rules! inner {
1341 ($bi:ident) => { fn $bi() -> u8 { $c } }
1345 fn $b() -> u8 { $c }
1352 ($a:ident, $b:ident, $c:tt) => {
1353 macro_rules! inner {
1354 ($bi:ident) => { fn $bi() -> u8 { $c } }
1358 fn $b() -> u8 { $c }
1361 macro_rules !inner {
1377 fn test_expr_after_path_colons() {
1381 ($k:expr) => { fn f() { K::$k; } }
1388 ($k:expr) => { fn f() { K::$k; } }
1390 /* parse error: expected identifier */
1391 /* parse error: expected SEMICOLON */
1392 /* parse error: expected SEMICOLON */
1393 /* parse error: expected expression */
1397 // MACRO_ITEMS@0..19
1412 // PATH_SEGMENT@6..7
1417 // L_PAREN@9..10 "("
1422 // PATH_SEGMENT@10..11
1426 // L_PAREN@11..12 "("
1428 // STRING@12..15 "\"0\""
1429 // R_PAREN@15..16 ")"
1431 // R_PAREN@16..17 ")"
1432 // SEMICOLON@17..18 ";"
1433 // R_CURLY@18..19 "}"
1440 fn test_match_is_not_greedy() {
1444 ($($i:ident $(,)*),*) => {};
1450 ($($i:ident $(,)*),*) => {};
1458 fn expr_interpolation() {
1461 macro_rules! m { ($expr:expr) => { map($expr) } }
1463 let _ = m!(x + foo);
1467 macro_rules! m { ($expr:expr) => { map($expr) } }
1469 let _ = map((x+foo));
1476 fn mbe_are_not_attributes() {
1479 macro_rules! error {
1487 macro_rules! error {