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