]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/tests/target/macro_rules.rs
Merge commit '0c89065b934397b62838fe3e4ef6f6352fc52daf' into libgccjit-codegen
[rust.git] / src / tools / rustfmt / tests / target / macro_rules.rs
1 // rustfmt-format_macro_matchers: true
2
3 macro_rules! m {
4     () => {};
5     ($x:ident) => {};
6     ($m1:ident, $m2:ident, $x:ident) => {};
7     ($($beginning:ident),*; $middle:ident; $($end:ident),*) => {};
8     (
9         $($beginning:ident),*;
10         $middle:ident;
11         $($end:ident),*;
12         $($beginning:ident),*;
13         $middle:ident;
14         $($end:ident),*
15     ) => {};
16     ($name:ident($($dol:tt $var:ident)*) $($body:tt)*) => {};
17     (
18         $($i:ident : $ty:ty, $def:expr, $stb:expr, $($dstring:tt),+);+ $(;)*
19         $($i:ident : $ty:ty, $def:expr, $stb:expr, $($dstring:tt),+);+ $(;)*
20     ) => {};
21     ($foo:tt foo[$attr:meta] $name:ident) => {};
22     ($foo:tt[$attr:meta] $name:ident) => {};
23     ($foo:tt &'a[$attr:meta] $name:ident) => {};
24     ($foo:tt foo #[$attr:meta] $name:ident) => {};
25     ($foo:tt #[$attr:meta] $name:ident) => {};
26     ($foo:tt &'a #[$attr:meta] $name:ident) => {};
27     ($x:tt foo bar foo bar foo bar $y:tt => x * y * z $z:tt, $($a:tt),*) => {};
28 }
29
30 macro_rules! impl_a_method {
31     ($n:ident($a:ident : $ta:ty) -> $ret:ty { $body:expr }) => {
32         fn $n($a: $ta) -> $ret {
33             $body
34         }
35         macro_rules! $n {
36             ($va: expr) => {
37                 $n($va)
38             };
39         }
40     };
41     ($n:ident($a:ident : $ta:ty, $b:ident : $tb:ty) -> $ret:ty { $body:expr }) => {
42         fn $n($a: $ta, $b: $tb) -> $ret {
43             $body
44         }
45         macro_rules! $n {
46             ($va: expr,$vb: expr) => {
47                 $n($va, $vb)
48             };
49         }
50     };
51     (
52         $n:ident($a:ident : $ta:ty, $b:ident : $tb:ty, $c:ident : $tc:ty) -> $ret:ty { $body:expr }
53     ) => {
54         fn $n($a: $ta, $b: $tb, $c: $tc) -> $ret {
55             $body
56         }
57         macro_rules! $n {
58             ($va: expr,$vb: expr,$vc: expr) => {
59                 $n($va, $vb, $vc)
60             };
61         }
62     };
63     (
64         $n:ident($a:ident : $ta:ty, $b:ident : $tb:ty, $c:ident : $tc:ty, $d:ident : $td:ty) ->
65         $ret:ty { $body:expr }
66     ) => {
67         fn $n($a: $ta, $b: $tb, $c: $tc, $d: $td) -> $ret {
68             $body
69         }
70         macro_rules! $n {
71             ($va: expr,$vb: expr,$vc: expr,$vd: expr) => {
72                 $n($va, $vb, $vc, $vd)
73             };
74         }
75     };
76 }
77
78 macro_rules! m {
79     // a
80     ($expr:expr, $($func:ident)*) => {{
81         let x = $expr;
82         $func(x)
83     }};
84
85     /* b */
86     () => {
87         /* c */
88     };
89
90     (@tag) => {};
91
92     // d
93     ($item:ident) => {
94         mod macro_item {
95             struct $item;
96         }
97     };
98 }
99
100 macro m2 {
101     // a
102     ($expr:expr, $($func:ident)*) => {{
103         let x = $expr;
104         $func(x)
105     }}
106
107     /* b */
108     () => {
109         /* c */
110     }
111
112     (@tag) => {}
113
114     // d
115     ($item:ident) => {
116         mod macro_item {
117             struct $item;
118         }
119     }
120 }
121
122 // #2438, #2476
123 macro_rules! m {
124     () => {
125         fn foo() {
126             this_line_is_98_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx();
127         }
128     };
129 }
130 macro_rules! m {
131     () => {
132         fn foo() {
133             this_line_is_99_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
134             );
135         }
136     };
137 }
138 macro_rules! m {
139     () => {
140         fn foo() {
141             this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
142             );
143         }
144     };
145 }
146 macro_rules! m {
147     () => {
148         fn foo() {
149             this_line_is_101_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(
150             );
151         }
152     };
153 }
154
155 // #2439
156 macro_rules! m {
157     (
158         $line0_xxxxxxxxxxxxxxxxx:expr,
159         $line1_xxxxxxxxxxxxxxxxx:expr,
160         $line2_xxxxxxxxxxxxxxxxx:expr,
161         $line3_xxxxxxxxxxxxxxxxx:expr,
162     ) => {};
163 }
164
165 // #2466
166 // Skip formatting `macro_rules!` that are not using `{}`.
167 macro_rules! m (
168     () => ()
169 );
170 macro_rules! m [
171     () => ()
172 ];
173
174 // #2470
175 macro foo($type_name:ident, $docs:expr) {
176     #[allow(non_camel_case_types)]
177     #[doc=$docs]
178     #[derive(Debug, Clone, Copy)]
179     pub struct $type_name;
180 }
181
182 // #2534
183 macro_rules! foo {
184     ($a:ident : $b:ty) => {};
185     ($a:ident $b:ident $c:ident) => {};
186 }
187
188 // #2538
189 macro_rules! add_message_to_notes {
190     ($msg:expr) => {{
191         let mut lines = message.lines();
192         notes.push_str(&format!("\n{}: {}", level, lines.next().unwrap()));
193         for line in lines {
194             notes.push_str(&format!(
195                 "\n{:indent$}{line}",
196                 "",
197                 indent = level.len() + 2,
198                 line = line,
199             ));
200         }
201     }};
202 }
203
204 // #2560
205 macro_rules! binary {
206     ($_self:ident, $expr:expr, $lhs:expr, $func:ident) => {
207         while $_self.matched($expr) {
208             let op = $_self.get_binary_op()?;
209
210             let rhs = Box::new($_self.$func()?);
211
212             $lhs = Spanned {
213                 span: $lhs.get_span().to(rhs.get_span()),
214                 value: Expression::Binary {
215                     lhs: Box::new($lhs),
216                     op,
217                     rhs,
218                 },
219             }
220         }
221     };
222 }
223
224 // #2558
225 macro_rules! m {
226     ($x:) => {};
227     ($($foo:expr)()?) => {};
228 }
229
230 // #2749
231 macro_rules! foo {
232     ($(x)* {}) => {};
233     ($(x)*()) => {};
234     ($(x)*[]) => {};
235 }
236 macro_rules! __wundergraph_expand_sqlite_mutation {
237     (
238         $mutation_name:ident $((context = $($context:tt)*))* {
239             $(
240                 $entity_name:ident(
241                     $(insert = $insert:ident,)*
242                     $(update = $update:ident,)*
243                     $(delete = $($delete:tt)+)*
244                 ),
245             )*
246         }
247     ) => {};
248 }
249
250 // #2607
251 macro_rules! bench {
252     ($ty:ident) => {
253         criterion_group!(
254             name = benches;
255             config = ::common_bench::reduced_samples();
256             targets = call, map;
257         );
258     };
259 }
260
261 // #2770
262 macro_rules! save_regs {
263     () => {
264         asm!("push rax
265               push rcx
266               push rdx
267               push rsi
268               push rdi
269               push r8
270               push r9
271               push r10
272               push r11"
273              :::: "intel", "volatile");
274     };
275 }
276
277 // #2721
278 macro_rules! impl_as_byte_slice_arrays {
279     ($n:expr,) => {};
280     ($n:expr, $N:ident, $($NN:ident,)*) => {
281         impl_as_byte_slice_arrays!($n - 1, $($NN,)*);
282
283         impl<T> AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut {
284             fn as_byte_slice_mut(&mut self) -> &mut [u8] {
285                 self[..].as_byte_slice_mut()
286             }
287
288             fn to_le(&mut self) {
289                 self[..].to_le()
290             }
291         }
292     };
293     (!div $n:expr,) => {};
294     (!div $n:expr, $N:ident, $($NN:ident,)*) => {
295         impl_as_byte_slice_arrays!(!div $n / 2, $($NN,)*);
296
297         impl<T> AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut {
298             fn as_byte_slice_mut(&mut self) -> &mut [u8] {
299                 self[..].as_byte_slice_mut()
300             }
301
302             fn to_le(&mut self) {
303                 self[..].to_le()
304             }
305         }
306     };
307 }
308
309 // #2919
310 fn foo() {
311     {
312         macro_rules! touch_value {
313             ($func:ident, $value:expr) => {{
314                 let result = API::get_cached().$func(
315                     self,
316                     key.as_ptr(),
317                     $value,
318                     ffi::VSPropAppendMode::paTouch,
319                 );
320                 let result = API::get_cached().$func(self, key.as_ptr(), $value, ffi::VSPropAppend);
321                 let result =
322                     API::get_cached().$func(self, key.as_ptr(), $value, ffi::VSPropAppendM);
323                 let result =
324                     APIIIIIIIII::get_cached().$func(self, key.as_ptr(), $value, ffi::VSPropAppendM);
325                 let result = API::get_cached().$func(
326                     self,
327                     key.as_ptr(),
328                     $value,
329                     ffi::VSPropAppendMMMMMMMMMM,
330                 );
331                 debug_assert!(result == 0);
332             }};
333         }
334     }
335 }
336
337 // #2642
338 macro_rules! template {
339     ($name:expr) => {
340         format_args!(
341             r##"
342 "http://example.com"
343
344 # test
345 "##,
346             $name
347         )
348     };
349 }
350
351 macro_rules! template {
352     () => {
353         format_args!(
354             r"
355 //
356
357 "
358         )
359     };
360 }