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