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