1 use ::parser::ParserEntryPoint;
3 SyntaxKind::{ERROR, IDENT},
6 use test_utils::assert_eq_text;
10 // Good first issue (although a slightly challenging one):
12 // * Pick a random test from here
13 // https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt
14 // * Port the test to rust and add it to this module
18 fn test_token_id_shift() {
19 let expansion = parse_macro(
22 ($e:ident) => { foo bar $e }
26 .expand_tt("foobar!(baz);");
28 fn get_id(t: &tt::TokenTree) -> Option<u32> {
29 if let tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) = t {
30 return Some(ident.id.0);
35 assert_eq!(expansion.token_trees.len(), 3);
36 // {($e:ident) => { foo bar $e }}
38 assert_eq!(get_id(&expansion.token_trees[0]), Some(9));
39 assert_eq!(get_id(&expansion.token_trees[1]), Some(10));
41 // The input args of macro call include parentheses:
43 // So baz should be 12+1+1
44 assert_eq!(get_id(&expansion.token_trees[2]), Some(14));
49 let expanded = parse_macro(
52 ($e:ident) => { fn $e() {} }
56 .expand_tt("foobar!(baz);");
58 let (node, token_map) = token_tree_to_syntax_node(&expanded, ParserEntryPoint::Items).unwrap();
59 let content = node.syntax_node().to_string();
61 let get_text = |id, kind| -> String {
62 content[token_map.first_range_by_token(id, kind).unwrap()].to_string()
65 assert_eq!(expanded.token_trees.len(), 4);
66 // {($e:ident) => { fn $e() {} }}
67 // 012345 67 8 9 T12 3
69 assert_eq!(get_text(tt::TokenId(9), IDENT), "fn");
70 assert_eq!(get_text(tt::TokenId(12), T!['(']), "(");
71 assert_eq!(get_text(tt::TokenId(13), T!['{']), "{");
74 fn to_subtree(tt: &tt::TokenTree) -> &tt::Subtree {
75 if let tt::TokenTree::Subtree(subtree) = tt {
78 unreachable!("It is not a subtree");
81 fn to_punct(tt: &tt::TokenTree) -> &tt::Punct {
82 if let tt::TokenTree::Leaf(tt::Leaf::Punct(lit)) = tt {
85 unreachable!("It is not a Punct");
89 fn test_attr_to_token_tree() {
90 let expansion = parse_to_token_tree_by_syntax(
97 assert_eq!(to_punct(&expansion.token_trees[0]).char, '#');
99 to_subtree(&expansion.token_trees[1]).delimiter_kind(),
100 Some(tt::DelimiterKind::Bracket)
105 fn test_meta_doc_comments() {
109 ($(#[$ i:meta])+) => (
118 /// Single Line Doc 1
123 "# [doc = \" Single Line Doc 1\"] # [doc = \"\\n MultiLines Doc\\n \"] fn bar () {}",
128 fn test_meta_extended_key_value_attributes() {
139 .assert_expand_items(
140 r#"foo! { #[doc = concat!("The `", "bla", "` lang item.")] }"#,
141 r#"# [doc = concat ! ("The `" , "bla" , "` lang item.")] fn bar () {}"#,
146 fn test_meta_doc_comments_non_latin() {
150 ($(#[$ i:meta])+) => (
164 "# [doc = \" 錦瑟無端五十弦,一弦一柱思華年。\"] # [doc = \"\\n 莊生曉夢迷蝴蝶,望帝春心託杜鵑。\\n \"] fn bar () {}",
169 fn test_meta_doc_comments_escaped_characters() {
173 ($(#[$ i:meta])+) => (
180 .assert_expand_items(
184 r#"# [doc = " \\ \" \'"] fn bar () {}"#,
193 ($ i:tt) => { fn foo() $ i }
197 .assert_expand_items(r#"foo! { { 1; } }"#, r#"fn foo () {1 ;}"#);
205 ($($ i:tt)*) => { $($ i)* }
209 .assert_expand_items(r#"foo! { fn foo() {} }"#, r#"fn foo () {}"#);
213 fn test_tt_composite() {
221 .assert_expand_items(r#"foo! { => }"#, r#"0"#);
225 fn test_tt_composite2() {
226 let node = parse_macro(
229 ($($tt:tt)*) => { abs!(=> $($tt)*) }
233 .expand_items(r#"foo!{#}"#);
235 let res = format!("{:#?}", &node);
237 r###"MACRO_ITEMS@0..10
250 R_PAREN@9..10 ")""###,
256 fn test_tt_with_composite_without_space() {
260 ($ op:tt, $j:path) => (
266 // Test macro input without any spaces
267 // See https://github.com/rust-analyzer/rust-analyzer/issues/6692
268 .assert_expand_items("foo!(==,Foo::Bool)", "0");
272 fn test_underscore() {
280 .assert_expand_items(r#"foo! { => }"#, r#"0"#);
284 fn test_underscore_not_greedily() {
288 ($($a:ident)* _) => {0};
292 // `_` overlaps with `$a:ident` but rustc matches it under the `_` token
293 .assert_expand_items(r#"q![a b c d _]"#, r#"0"#);
298 ($($a:expr => $b:ident)* _ => $c:expr) => {0};
302 // `_ => ou` overlaps with `$a:expr => $b:ident` but rustc matches it under `_ => $c:expr`
303 .assert_expand_items(r#"q![a => b c => d _ => ou]"#, r#"0"#);
307 fn test_underscore_as_type() {
315 // Underscore is a type
316 .assert_expand_items(r#"q![_]"#, r#"0"#);
320 fn test_underscore_lifetime() {
321 parse_macro(r#"macro_rules! q { ($a:lifetime) => {0}; }"#)
322 .assert_expand_items(r#"q!['_]"#, r#"0"#);
326 fn test_vertical_bar_with_pat() {
330 (| $pat:pat | ) => { 0 }
334 .assert_expand_items(r#"foo! { | x | }"#, r#"0"#);
338 fn test_dollar_crate_lhs_is_not_meta() {
347 .assert_expand_items(r#"foo!{}"#, r#"0"#);
355 ($ lt:lifetime) => { struct Ref<$ lt>{ s: &$ lt str } }
359 .assert_expand_items(r#"foo!{'a}"#, r#"struct Ref <'a > {s : &'a str}"#);
367 ($ type:ty , $ lit:literal) => { const VALUE: $ type = $ lit;};
371 .assert_expand_items(r#"foo!(u8,0);"#, r#"const VALUE : u8 = 0 ;"#);
376 ($ type:ty , $ lit:literal) => { const VALUE: $ type = $ lit;};
380 .assert_expand_items(r#"foo!(i32,-1);"#, r#"const VALUE : i32 = - 1 ;"#);
384 fn test_boolean_is_ident() {
388 ($lit0:literal, $lit1:literal) => { const VALUE: (bool,bool) = ($lit0,$lit1); };
393 r#"foo!(true,false);"#,
418 ($ vis:vis $ name:ident) => { $ vis fn $ name() {}};
422 .assert_expand_items(r#"foo!(pub foo);"#, r#"pub fn foo () {}"#)
423 // test optional cases
424 .assert_expand_items(r#"foo!(foo);"#, r#"fn foo () {}"#);
428 fn test_inner_macro_rules() {
432 ($a:ident, $b:ident, $c:tt) => {
448 r#"macro_rules ! bar {($ bi : ident) => {fn $ bi () -> u8 {1}}} bar ! (x) ; fn y () -> u8 {1}"#,
453 fn test_expr_after_path_colons() {
463 .expand_statements(r#"m!(C("0"))"#)
465 .any(|token| token.kind() == ERROR));
469 fn test_match_is_not_greedy() {
473 ($($i:ident $(,)*),*) => {};
477 .assert_expand_items(r#"foo!(a,b);"#, r#""#);
480 // The following tests are based on real world situations
483 let fixture = parse_macro(
486 ($($item:expr),*) => {
488 let mut v = Vec::new();
499 .assert_expand_items(r#"vec!();"#, r#"{let mut v = Vec :: new () ; v}"#)
500 .assert_expand_items(
502 r#"{let mut v = Vec :: new () ; v . push (1u32) ; v . push (2) ; v}"#,
505 let tree = fixture.expand_expr(r#"vec![1u32,2];"#);
508 &format!("{:#?}", tree),
535 METHOD_CALL_EXPR@20..32
547 INT_NUMBER@27..31 "1u32"
551 METHOD_CALL_EXPR@33..42
563 INT_NUMBER@40..41 "2"
577 fn test_winapi_struct() {
578 // from https://github.com/retep998/winapi-rs/blob/a7ef2bca086aae76cf6c4ce4c2552988ed9798ad/src/macros.rs#L366
582 macro_rules! STRUCT {
583 ($(#[$attrs:meta])* struct $name:ident {
584 $($field:ident: $ftype:ty,)+
586 #[repr(C)] #[derive(Copy)] $(#[$attrs])*
588 $(pub $field: $ftype,)+
590 impl Clone for $name {
592 fn clone(&self) -> $name { *self }
594 #[cfg(feature = "impl-default")]
595 impl Default for $name {
597 fn default() -> $name { unsafe { $crate::_core::mem::zeroed() } }
603 // from https://github.com/retep998/winapi-rs/blob/a7ef2bca086aae76cf6c4ce4c2552988ed9798ad/src/shared/d3d9caps.rs
604 assert_expand_items(r#"STRUCT!{struct D3DVSHADERCAPS2_0 {Caps: u8,}}"#,
605 "# [repr (C)] # [derive (Copy)] pub struct D3DVSHADERCAPS2_0 {pub Caps : u8 ,} impl Clone for D3DVSHADERCAPS2_0 {# [inline] fn clone (& self) -> D3DVSHADERCAPS2_0 {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DVSHADERCAPS2_0 {# [inline] fn default () -> D3DVSHADERCAPS2_0 {unsafe {$crate :: _core :: mem :: zeroed ()}}}"
607 .assert_expand_items(r#"STRUCT!{#[cfg_attr(target_arch = "x86", repr(packed))] struct D3DCONTENTPROTECTIONCAPS {Caps : u8 ,}}"#,
608 "# [repr (C)] # [derive (Copy)] # [cfg_attr (target_arch = \"x86\" , repr (packed))] pub struct D3DCONTENTPROTECTIONCAPS {pub Caps : u8 ,} impl Clone for D3DCONTENTPROTECTIONCAPS {# [inline] fn clone (& self) -> D3DCONTENTPROTECTIONCAPS {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DCONTENTPROTECTIONCAPS {# [inline] fn default () -> D3DCONTENTPROTECTIONCAPS {unsafe {$crate :: _core :: mem :: zeroed ()}}}"
616 macro_rules! int_base {
617 ($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => {
618 #[stable(feature = "rust1", since = "1.0.0")]
619 impl fmt::$Trait for $T {
620 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
621 $Radix.fmt_int(*self as $U, f)
627 ).assert_expand_items(r#" int_base!{Binary for isize as usize -> Binary}"#,
628 "# [stable (feature = \"rust1\" , since = \"1.0.0\")] impl fmt ::Binary for isize {fn fmt (& self , f : & mut fmt :: Formatter < \'_ >) -> fmt :: Result {Binary . fmt_int (* self as usize , f)}}"
633 fn test_generate_pattern_iterators() {
634 // from https://github.com/rust-lang/rust/blob/316a391dcb7d66dc25f1f9a4ec9d368ef7615005/src/libcore/str/mod.rs
637 macro_rules! generate_pattern_iterators {
638 { double ended; with $(#[$common_stability_attribute:meta])*,
639 $forward_iterator:ident,
640 $reverse_iterator:ident, $iterty:ty
646 ).assert_expand_items(
647 r#"generate_pattern_iterators ! ( double ended ; with # [ stable ( feature = "rust1" , since = "1.0.0" ) ] , Split , RSplit , & 'a str );"#,
653 fn test_impl_fn_for_zst() {
654 // from https://github.com/rust-lang/rust/blob/5d20ff4d2718c820632b38c1e49d4de648a9810b/src/libcore/internal_macros.rs
657 macro_rules! impl_fn_for_zst {
658 { $( $( #[$attr: meta] )*
659 struct $Name: ident impl$( <$( $lifetime : lifetime ),+> )? Fn =
660 |$( $arg: ident: $ArgTy: ty ),*| -> $ReturnTy: ty
667 impl $( <$( $lifetime ),+> )? Fn<($( $ArgTy, )*)> for $Name {
669 extern "rust-call" fn call(&self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
674 impl $( <$( $lifetime ),+> )? FnMut<($( $ArgTy, )*)> for $Name {
676 extern "rust-call" fn call_mut(
678 ($( $arg, )*): ($( $ArgTy, )*)
680 Fn::call(&*self, ($( $arg, )*))
684 impl $( <$( $lifetime ),+> )? FnOnce<($( $ArgTy, )*)> for $Name {
685 type Output = $ReturnTy;
688 extern "rust-call" fn call_once(self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
689 Fn::call(&self, ($( $arg, )*))
696 ).assert_expand_items(r#"
698 # [ derive ( Clone ) ]
699 struct CharEscapeDebugContinue impl Fn = | c : char | -> char :: EscapeDebug {
700 c . escape_debug_ext ( false )
703 # [ derive ( Clone ) ]
704 struct CharEscapeUnicode impl Fn = | c : char | -> char :: EscapeUnicode {
705 c . escape_unicode ( )
707 # [ derive ( Clone ) ]
708 struct CharEscapeDefault impl Fn = | c : char | -> char :: EscapeDefault {
709 c . escape_default ( )
713 "# [derive (Clone)] struct CharEscapeDebugContinue ; impl Fn < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDebug {{c . escape_debug_ext (false)}}} impl FnMut < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDebugContinue {type Output = char :: EscapeDebug ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeUnicode ; impl Fn < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeUnicode {{c . escape_unicode ()}}} impl FnMut < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeUnicode {type Output = char :: EscapeUnicode ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeDefault ; impl Fn < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDefault {{c . escape_default ()}}} impl FnMut < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDefault {type Output = char :: EscapeDefault ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (& self , (c ,))}}"
718 fn test_impl_nonzero_fmt() {
719 // from https://github.com/rust-lang/rust/blob/316a391dcb7d66dc25f1f9a4ec9d368ef7615005/src/libcore/num/mod.rs#L12
722 macro_rules! impl_nonzero_fmt {
723 ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
728 ).assert_expand_items(
729 r#"impl_nonzero_fmt! { # [stable(feature= "nonzero",since="1.28.0")] (Debug,Display,Binary,Octal,LowerHex,UpperHex) for NonZeroU8}"#,
735 fn test_cfg_if_items() {
736 // from https://github.com/rust-lang/rust/blob/33fe1131cadba69d317156847be9a402b89f11bb/src/libstd/macros.rs#L986
739 macro_rules! __cfg_if_items {
740 (($($not:meta,)*) ; ) => {};
741 (($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
742 __cfg_if_items! { ($($not,)* $($m,)*) ; $($rest)* }
746 ).assert_expand_items(
747 r#"__cfg_if_items ! { ( rustdoc , ) ; ( ( ) ( # [ cfg ( any ( target_os = "redox" , unix ) ) ] # [ stable ( feature = "rust1" , since = "1.0.0" ) ] pub use sys :: ext as unix ; # [ cfg ( windows ) ] # [ stable ( feature = "rust1" , since = "1.0.0" ) ] pub use sys :: ext as windows ; # [ cfg ( any ( target_os = "linux" , target_os = "l4re" ) ) ] pub mod linux ; ) ) , }"#,
748 "__cfg_if_items ! {(rustdoc ,) ;}",
753 fn test_cfg_if_main() {
754 // from https://github.com/rust-lang/rust/blob/3d211248393686e0f73851fc7548f6605220fbe1/src/libpanic_unwind/macros.rs#L9
757 macro_rules! cfg_if {
759 if #[cfg($($meta:meta),*)] { $($it:item)* }
765 $( ( ($($meta),*) ($($it)*) ), )*
770 // Internal macro to Apply a cfg attribute to a list of items
771 (@__apply $m:meta, $($it:item)*) => {
776 ).assert_expand_items(r#"
778 if # [ cfg ( target_env = "msvc" ) ] {
779 // no extra unwinder support needed
780 } else if # [ cfg ( all ( target_arch = "wasm32" , not ( target_os = "emscripten" ) ) ) ] {
781 // no unwinder on the system!
784 pub use libunwind :: * ;
788 "__cfg_if_items ! {() ; ((target_env = \"msvc\") ()) , ((all (target_arch = \"wasm32\" , not (target_os = \"emscripten\"))) ()) , (() (mod libunwind ; pub use libunwind :: * ;)) ,}"
789 ).assert_expand_items(
791 cfg_if ! { @ __apply cfg ( all ( not ( any ( not ( any ( target_os = "solaris" , target_os = "illumos" ) ) ) ) ) ) , }
798 fn test_proptest_arbitrary() {
799 // from https://github.com/AltSysrq/proptest/blob/d1c4b049337d2f75dd6f49a095115f7c532e5129/proptest/src/arbitrary/macros.rs#L16
802 macro_rules! arbitrary {
803 ([$($bounds : tt)*] $typ: ty, $strat: ty, $params: ty;
804 $args: ident => $logic: expr) => {
805 impl<$($bounds)*> $crate::arbitrary::Arbitrary for $typ {
806 type Parameters = $params;
807 type Strategy = $strat;
808 fn arbitrary_with($args: Self::Parameters) -> Self::Strategy {
815 ).assert_expand_items(r#"arbitrary ! ( [ A : Arbitrary ]
817 VecStrategy < A :: Strategy > ,
818 RangedParams1 < A :: Parameters > ;
819 args => { let product_unpack ! [ range , a ] = args ; vec ( any_with :: < A > ( a ) , range ) }
821 "impl <A : Arbitrary > $crate :: arbitrary :: Arbitrary for Vec < A > {type Parameters = RangedParams1 < A :: Parameters > ; type Strategy = VecStrategy < A :: Strategy > ; fn arbitrary_with (args : Self :: Parameters) -> Self :: Strategy {{let product_unpack ! [range , a] = args ; vec (any_with :: < A > (a) , range)}}}"
827 // This is from winapi 2.8, which do not have a link from github
829 let expanded = parse_macro(
833 (interface $interface:ident ($vtbl:ident) : $pinterface:ident ($pvtbl:ident)
835 fn $method:ident(&mut self $(,$p:ident : $t:ty)*) -> $rtr:ty
839 $(pub unsafe fn $method(&mut self) -> $rtr {
840 ((*self.lpVtbl).$method)(self $(,$p)*)
846 RIDL!{interface ID3D11Asynchronous(ID3D11AsynchronousVtbl): ID3D11DeviceChild(ID3D11DeviceChildVtbl) {
847 fn GetDataSize(&mut self) -> UINT
850 assert_eq!(expanded.to_string(), "impl ID3D11Asynchronous {pub unsafe fn GetDataSize (& mut self) -> UINT {((* self . lpVtbl) .GetDataSize) (self)}}");
854 fn test_quick_error() {
855 let expanded = parse_macro(
857 macro_rules! quick_error {
859 (SORT [enum $name:ident $( #[$meta:meta] )*]
860 items [$($( #[$imeta:meta] )*
861 => $iitem:ident: $imode:tt [$( $ivar:ident: $ityp:ty ),*]
862 {$( $ifuncs:tt )*} )* ]
866 quick_error!(ENUMINITION [enum $name $( #[$meta] )*]
871 $iitem: $imode [$( $ivar: $ityp ),*]
881 quick_error ! (SORT [enum Wrapped # [derive (Debug)]] items [
883 => Two : TUPLE [s :String] {display ("two: {}" , s) from ()}
888 assert_eq!(expanded.to_string(), "quick_error ! (ENUMINITION [enum Wrapped # [derive (Debug)]] body [] queue [=> One : UNIT [] => Two : TUPLE [s : String]]) ;");
892 fn test_empty_repeat_vars_in_empty_repeat_vars() {
895 macro_rules! delegate_impl {
896 ([$self_type:ident, $self_wrap:ty, $self_map:ident]
897 pub trait $name:ident $(: $sup:ident)* $(+ $more_sup:ident)* {
900 @escape [type $assoc_name_ext:ident]
905 $(#[$_assoc_attr:meta])*
906 type $assoc_name:ident $(: $assoc_bound:ty)*;
912 $(#[$_method_attr:meta])*
913 fn $method_name:ident(self $(: $self_selftype:ty)* $(,$marg:ident : $marg_ty:ty)*) -> $mret:ty;
921 impl<> $name for $self_wrap where $self_type: $name {
924 fn $method_name(self $(: $self_selftype)* $(,$marg: $marg_ty)*) -> $mret {
925 $self_map!(self).$method_name($($marg),*)
933 ).assert_expand_items(
934 r#"delegate_impl ! {[G , & 'a mut G , deref] pub trait Data : GraphBase {@ section type type NodeWeight ;}}"#,
935 "impl <> Data for & \'a mut G where G : Data {}",
940 fn expr_interpolation() {
941 let expanded = parse_macro(
950 .expand_expr("id!(x + foo);");
952 assert_eq!(expanded.to_string(), "map(x+foo)");
956 fn test_issue_2520() {
957 let macro_fixture = parse_macro(
959 macro_rules! my_macro {
962 $( [] $sname:ident : $stype:ty )?
963 $( [$expr:expr] $nname:ident : $ntype:ty )?
976 macro_fixture.assert_expand_items(
978 ([] p1 : u32 , [|_| S0K0] s : S0K0 , [] k0 : i32)
985 fn test_issue_3861() {
986 let macro_fixture = parse_macro(
988 macro_rules! rgb_color {
989 ($p:expr, $t: ty) => {
991 let _ = 0 as $t << $p;
998 macro_fixture.expand_items(r#"rgb_color!(8 + 8, u32);"#);
1002 fn test_repeat_bad_var() {
1003 // FIXME: the second rule of the macro should be removed and an error about
1004 // `$( $c )+` raised
1008 ($( $b:ident )+) => {
1011 ($( $b:ident )+) => {
1017 .assert_expand_items("foo!(b0 b1);", "b0 b1");
1021 fn test_no_space_after_semi_colon() {
1022 let expanded = parse_macro(
1024 macro_rules! with_std { ($($i:item)*) => ($(#[cfg(feature = "std")]$i)*) }
1027 .expand_items(r#"with_std! {mod m;mod f;}"#);
1029 let dump = format!("{:#?}", expanded);
1031 r###"MACRO_ITEMS@0..52
1043 IDENT@6..13 "feature"
1045 STRING@14..19 "\"std\""
1051 SEMICOLON@25..26 ";"
1063 IDENT@32..39 "feature"
1065 STRING@40..45 "\"std\""
1071 SEMICOLON@51..52 ";""###,
1076 // https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-57597.rs
1078 fn test_rustc_issue_57597() {
1079 fn test_error(fixture: &str) {
1080 assert_eq!(parse_macro_error(fixture), ParseError::RepetitionEmptyTokenTree);
1083 test_error("macro_rules! foo { ($($($i:ident)?)+) => {}; }");
1084 test_error("macro_rules! foo { ($($($i:ident)?)*) => {}; }");
1085 test_error("macro_rules! foo { ($($($i:ident)?)?) => {}; }");
1086 test_error("macro_rules! foo { ($($($($i:ident)?)?)?) => {}; }");
1087 test_error("macro_rules! foo { ($($($($i:ident)*)?)?) => {}; }");
1088 test_error("macro_rules! foo { ($($($($i:ident)?)*)?) => {}; }");
1089 test_error("macro_rules! foo { ($($($($i:ident)?)?)*) => {}; }");
1090 test_error("macro_rules! foo { ($($($($i:ident)*)*)?) => {}; }");
1091 test_error("macro_rules! foo { ($($($($i:ident)?)*)*) => {}; }");
1092 test_error("macro_rules! foo { ($($($($i:ident)?)*)+) => {}; }");
1093 test_error("macro_rules! foo { ($($($($i:ident)+)?)*) => {}; }");
1094 test_error("macro_rules! foo { ($($($($i:ident)+)*)?) => {}; }");
1098 fn test_expand_bad_literal() {
1101 macro_rules! foo { ($i:literal) => {}; }
1104 .assert_expand_err(r#"foo!(&k");"#, &ExpandError::BindingError("".into()));
1108 fn test_empty_comments() {
1111 macro_rules! one_arg_macro { ($fmt:expr) => (); }
1115 r#"one_arg_macro!(/**/)"#,
1116 &ExpandError::BindingError("expected Expr".into()),