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