]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/macro-first-set.rs
99e5d22fb476e9d8b96c13a4bf3fbefdd749ffa2
[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 //{{{ some more tests ==========================================================
203
204 macro_rules! test_block {
205     (< $($b:block)* >) => {}
206 }
207
208 test_block!(<>);
209 test_block!(<{}>);
210 test_block!(<{1}{2}>);
211
212 macro_rules! test_ty {
213     ($($t:ty),* $(,)*) => {}
214 }
215
216 test_ty!();
217 test_ty!(,);
218 test_ty!(u8);
219 test_ty!(u8,);
220
221 macro_rules! test_path {
222     ($($t:path),* $(,)*) => {}
223 }
224
225 test_path!();
226 test_path!(,);
227 test_path!(::std);
228 test_path!(std::u8,);
229 test_path!(any, super, super::super::self::path, X<Y>::Z<'a, T=U>);
230
231 macro_rules! test_meta_block {
232     ($($m:meta)* $b:block) => {};
233 }
234
235 test_meta_block!(windows {});
236
237 //}}}
238
239 fn main() {
240     test_26444();
241     test_40569();
242     test_35650();
243     test_24189();
244 }
245