]> git.lizzy.rs Git - rust.git/blob - src/test/ui/macros/macro-first-set.rs
Rollup merge of #87596 - jesyspa:issue-87318-hidden-whitespace, r=estebank
[rust.git] / src / test / ui / macros / macro-first-set.rs
1 // run-pass
2
3 //{{{ issue 40569 ==============================================================
4
5 macro_rules! my_struct {
6     ($(#[$meta:meta])* $ident:ident) => {
7         $(#[$meta])* struct $ident;
8     }
9 }
10
11 my_struct!(#[derive(Debug, PartialEq)] Foo40569);
12
13 fn test_40569() {
14     assert_eq!(Foo40569, Foo40569);
15 }
16
17 //}}}
18
19 //{{{ issue 26444 ==============================================================
20
21 macro_rules! foo_26444 {
22     ($($beginning:ident),*; $middle:ident; $($end:ident),*) => {
23         stringify!($($beginning,)* $middle $(,$end)*)
24     }
25 }
26
27 fn test_26444() {
28     assert_eq!("a, b, c, d, e", foo_26444!(a, b; c; d, e));
29     assert_eq!("f", foo_26444!(; f ;));
30 }
31
32 macro_rules! pat_26444 {
33     ($fname:ident $($arg:pat)* =) => {}
34 }
35
36 pat_26444!(foo 1 2 5...7 =);
37 pat_26444!(bar Some(ref x) Ok(ref mut y) &(w, z) =);
38
39 //}}}
40
41 //{{{ issue 40984 ==============================================================
42
43 macro_rules! thread_local_40984 {
44     () => {};
45     ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => {
46         thread_local_40984!($($rest)*);
47     };
48     ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => {};
49 }
50
51 thread_local_40984! {
52     // no docs
53     #[allow(unused)]
54     static FOO: i32 = 42;
55     /// docs
56     pub static BAR: String = String::from("bar");
57
58     // look at these restrictions!!
59     pub(crate) static BAZ: usize = 0;
60     pub(in foo) static QUUX: usize = 0;
61 }
62
63 //}}}
64
65 //{{{ issue 35650 ==============================================================
66
67 macro_rules! size {
68     ($ty:ty) => {
69         std::mem::size_of::<$ty>()
70     };
71     ($size:tt) => {
72         $size
73     };
74 }
75
76 fn test_35650() {
77     assert_eq!(size!(u64), 8);
78     assert_eq!(size!(5), 5);
79 }
80
81 //}}}
82
83 //{{{ issue 27832 ==============================================================
84
85 macro_rules! m {
86     ( $i:ident ) => ();
87     ( $t:tt $j:tt ) => ();
88 }
89
90 m!(c);
91 m!(t 9);
92 m!(0 9);
93 m!(struct);
94 m!(struct Foo);
95
96 macro_rules! m2 {
97     ( $b:expr ) => ();
98     ( $t:tt $u:tt ) => ();
99 }
100
101 m2!(3);
102 m2!(1 2);
103 m2!(_ 1);
104 m2!(enum Foo);
105
106 //}}}
107
108 //{{{ issue 39964 ==============================================================
109
110 macro_rules! foo_39964 {
111     ($a:ident) => {};
112     (_) => {};
113 }
114
115 foo_39964!(_);
116
117 //}}}
118
119 //{{{ issue 34030 ==============================================================
120
121 macro_rules! foo_34030 {
122     ($($t:ident),* /) => {};
123 }
124
125 foo_34030!(a, b/);
126 foo_34030!(a/);
127 foo_34030!(/);
128
129 //}}}
130
131 //{{{ issue 24189 ==============================================================
132
133 macro_rules! foo_24189 {
134     (
135         pub enum $name:ident {
136             $( #[$attr:meta] )* $var:ident
137         }
138     ) => {
139         pub enum $name {
140             $( #[$attr] )* $var
141         }
142     };
143 }
144
145 foo_24189! {
146     pub enum Foo24189 {
147         #[doc = "Bar"] Baz
148     }
149 }
150
151 macro_rules! serializable {
152     (
153         $(#[$struct_meta:meta])*
154         pub struct $name:ident {
155             $(
156                 $(#[$field_meta:meta])*
157                 $field:ident: $type_:ty
158             ),* ,
159         }
160     ) => {
161         $(#[$struct_meta])*
162         pub struct $name {
163             $(
164                 $(#[$field_meta])*
165                 $field: $type_
166             ),* ,
167         }
168     }
169 }
170
171 serializable! {
172     #[allow(dead_code)]
173     /// This is a test
174     pub struct Tester {
175         #[allow(dead_code)]
176         name: String,
177     }
178 }
179
180 macro_rules! foo_24189_c {
181     ( $( > )* $x:ident ) => { };
182 }
183 foo_24189_c!( > a );
184
185 fn test_24189() {
186     let _ = Foo24189::Baz;
187     let _ = Tester { name: "".to_owned() };
188 }
189
190 //}}}
191
192 //{{{ issue 50903 ==============================================================
193
194 macro_rules! foo_50903 {
195     ($($lif:lifetime ,)* #) => {};
196 }
197
198 foo_50903!('a, 'b, #);
199 foo_50903!('a, #);
200 foo_50903!(#);
201
202 //}}}
203
204 //{{{ issue 51477 ==============================================================
205
206 macro_rules! foo_51477 {
207     ($lifetime:lifetime) => {
208         "last token is lifetime"
209     };
210     ($other:tt) => {
211         "last token is other"
212     };
213     ($first:tt $($rest:tt)*) => {
214         foo_51477!($($rest)*)
215     };
216 }
217
218 fn test_51477() {
219     assert_eq!("last token is lifetime", foo_51477!('a));
220     assert_eq!("last token is other", foo_51477!(@));
221     assert_eq!("last token is lifetime", foo_51477!(@ {} 'a));
222 }
223
224 //}}}
225
226 //{{{ some more tests ==========================================================
227
228 macro_rules! test_block {
229     (< $($b:block)* >) => {}
230 }
231
232 test_block!(<>);
233 test_block!(<{}>);
234 test_block!(<{1}{2}>);
235
236 macro_rules! test_ty {
237     ($($t:ty),* $(,)*) => {}
238 }
239
240 test_ty!();
241 test_ty!(,);
242 test_ty!(u8);
243 test_ty!(u8,);
244
245 macro_rules! test_path {
246     ($($t:path),* $(,)*) => {}
247 }
248
249 test_path!();
250 test_path!(,);
251 test_path!(::std);
252 test_path!(std::ops,);
253 test_path!(any, super, super::super::self::path, X<Y>::Z<'a, T=U>);
254
255 macro_rules! test_lifetime {
256     (1. $($l:lifetime)* $($b:block)*) => {};
257     (2. $($b:block)* $($l:lifetime)*) => {};
258 }
259
260 test_lifetime!(1. 'a 'b {} {});
261 test_lifetime!(2. {} {} 'a 'b);
262
263 //}}}
264
265 fn main() {
266     test_26444();
267     test_40569();
268     test_35650();
269     test_24189();
270     test_51477();
271 }