]>
Commit | Line | Data |
---|---|---|
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 | ||
16 | macro_rules! my_struct { | |
17 | ($(#[$meta:meta])* $ident:ident) => { | |
18 | $(#[$meta])* struct $ident; | |
19 | } | |
20 | } | |
21 | ||
22 | my_struct!(#[derive(Debug, PartialEq)] Foo40569); | |
23 | ||
24 | fn test_40569() { | |
25 | assert_eq!(Foo40569, Foo40569); | |
26 | } | |
27 | ||
28 | //}}} | |
29 | ||
30 | //{{{ issue 26444 ============================================================== | |
31 | ||
32 | macro_rules! foo_26444 { | |
33 | ($($beginning:ident),*; $middle:ident; $($end:ident),*) => { | |
34 | stringify!($($beginning,)* $middle $(,$end)*) | |
35 | } | |
36 | } | |
37 | ||
38 | fn 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 | ||
43 | macro_rules! pat_26444 { | |
44 | ($fname:ident $($arg:pat)* =) => {} | |
45 | } | |
46 | ||
47 | pat_26444!(foo 1 2 5...7 =); | |
48 | pat_26444!(bar Some(ref x) Ok(ref mut y) &(w, z) =); | |
49 | ||
50 | //}}} | |
51 | ||
52 | //{{{ issue 40984 ============================================================== | |
53 | ||
54 | macro_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 | ||
62 | thread_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 | ||
78 | macro_rules! size { | |
79 | ($ty:ty) => { | |
80 | std::mem::size_of::<$ty>() | |
81 | }; | |
82 | ($size:tt) => { | |
83 | $size | |
84 | }; | |
85 | } | |
86 | ||
87 | fn test_35650() { | |
88 | assert_eq!(size!(u64), 8); | |
89 | assert_eq!(size!(5), 5); | |
90 | } | |
91 | ||
92 | //}}} | |
93 | ||
94 | //{{{ issue 27832 ============================================================== | |
95 | ||
96 | macro_rules! m { | |
97 | ( $i:ident ) => (); | |
98 | ( $t:tt $j:tt ) => (); | |
99 | } | |
100 | ||
101 | m!(c); | |
102 | m!(t 9); | |
103 | m!(0 9); | |
104 | m!(struct); | |
105 | m!(struct Foo); | |
106 | ||
107 | macro_rules! m2 { | |
108 | ( $b:expr ) => (); | |
109 | ( $t:tt $u:tt ) => (); | |
110 | } | |
111 | ||
112 | m2!(3); | |
113 | m2!(1 2); | |
114 | m2!(_ 1); | |
115 | m2!(enum Foo); | |
116 | ||
117 | //}}} | |
118 | ||
119 | //{{{ issue 39964 ============================================================== | |
120 | ||
121 | macro_rules! foo_39964 { | |
122 | ($a:ident) => {}; | |
123 | (_) => {}; | |
124 | } | |
125 | ||
126 | foo_39964!(_); | |
127 | ||
128 | //}}} | |
129 | ||
130 | //{{{ issue 34030 ============================================================== | |
131 | ||
132 | macro_rules! foo_34030 { | |
133 | ($($t:ident),* /) => {}; | |
134 | } | |
135 | ||
136 | foo_34030!(a, b/); | |
137 | foo_34030!(a/); | |
138 | foo_34030!(/); | |
139 | ||
140 | //}}} | |
141 | ||
142 | //{{{ issue 24189 ============================================================== | |
143 | ||
144 | macro_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 | ||
156 | foo_24189! { | |
157 | pub enum Foo24189 { | |
158 | #[doc = "Bar"] Baz | |
159 | } | |
160 | } | |
161 | ||
162 | macro_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 | ||
182 | serializable! { | |
183 | #[allow(dead_code)] | |
184 | /// This is a test | |
185 | pub struct Tester { | |
186 | #[allow(dead_code)] | |
187 | name: String, | |
188 | } | |
189 | } | |
190 | ||
191 | macro_rules! foo_24189_c { | |
192 | ( $( > )* $x:ident ) => { }; | |
193 | } | |
194 | foo_24189_c!( > a ); | |
195 | ||
196 | fn test_24189() { | |
197 | let _ = Foo24189::Baz; | |
198 | let _ = Tester { name: "".to_owned() }; | |
199 | } | |
200 | ||
201 | //}}} | |
202 | ||
94b46f34 XL |
203 | //{{{ issue 50903 ============================================================== |
204 | ||
205 | macro_rules! foo_50903 { | |
206 | ($($lif:lifetime ,)* #) => {}; | |
207 | } | |
208 | ||
209 | foo_50903!('a, 'b, #); | |
210 | foo_50903!('a, #); | |
211 | foo_50903!(#); | |
212 | ||
213 | //}}} | |
214 | ||
215 | //{{{ issue 51477 ============================================================== | |
216 | ||
217 | macro_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 | ||
229 | fn 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 | ||
239 | macro_rules! test_block { | |
240 | (< $($b:block)* >) => {} | |
241 | } | |
242 | ||
243 | test_block!(<>); | |
244 | test_block!(<{}>); | |
245 | test_block!(<{1}{2}>); | |
246 | ||
247 | macro_rules! test_ty { | |
248 | ($($t:ty),* $(,)*) => {} | |
249 | } | |
250 | ||
251 | test_ty!(); | |
252 | test_ty!(,); | |
253 | test_ty!(u8); | |
254 | test_ty!(u8,); | |
255 | ||
256 | macro_rules! test_path { | |
257 | ($($t:path),* $(,)*) => {} | |
258 | } | |
259 | ||
260 | test_path!(); | |
261 | test_path!(,); | |
262 | test_path!(::std); | |
263 | test_path!(std::u8,); | |
264 | test_path!(any, super, super::super::self::path, X<Y>::Z<'a, T=U>); | |
265 | ||
266 | macro_rules! test_meta_block { | |
267 | ($($m:meta)* $b:block) => {}; | |
268 | } | |
269 | ||
270 | test_meta_block!(windows {}); | |
271 | ||
94b46f34 XL |
272 | macro_rules! test_lifetime { |
273 | (1. $($l:lifetime)* $($b:block)*) => {}; | |
274 | (2. $($b:block)* $($l:lifetime)*) => {}; | |
275 | } | |
276 | ||
277 | test_lifetime!(1. 'a 'b {} {}); | |
278 | test_lifetime!(2. {} {} 'a 'b); | |
279 | ||
041b39d2 XL |
280 | //}}} |
281 | ||
282 | fn main() { | |
283 | test_26444(); | |
284 | test_40569(); | |
285 | test_35650(); | |
286 | test_24189(); | |
94b46f34 | 287 | test_51477(); |
041b39d2 XL |
288 | } |
289 |