]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/tests/source/macros.rs
Rollup merge of #107166 - petrochenkov:nooptable, r=oli-obk
[rust.git] / src / tools / rustfmt / tests / source / macros.rs
1 // rustfmt-normalize_comments: true
2 // rustfmt-format_macro_matchers: true
3 itemmacro!(this, is.now() .formatted(yay));
4
5 itemmacro!(really, long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb() .is.formatted());
6
7 itemmacro!{this, is.brace().formatted()}
8
9 fn main() {
10     foo! ( );
11
12     foo!(,);
13
14     bar!( a , b , c );
15
16     bar!( a , b , c , );
17
18     baz!(1+2+3, quux. kaas());
19
20     quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB);
21
22     kaas!(/* comments */ a /* post macro */, b /* another */);
23
24     trailingcomma!( a , b , c , );
25     // Preserve trailing comma only when necessary.
26     ok!(file.seek(
27         SeekFrom::Start(
28             table.map(|table| fixture.offset(table)).unwrap_or(0),
29         )
30     ));
31
32     noexpr!( i am not an expression, OK? );
33
34     vec! [ a , b , c];
35
36     vec! [AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA,
37           BBBBB, 5, 100-30, 1.33, b, b, b];
38
39     vec! [a /* comment */];
40
41     // Trailing spaces after a comma
42     vec![
43     a,   
44     ];
45     
46     vec![a; b];
47     vec!(a; b);
48     vec!{a; b};
49
50     vec![a, b; c];
51     vec![a; b, c];
52
53     vec![a; (|x| { let y = x + 1; let z = y + 1; z })(2)];
54     vec![a; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx];
55     vec![a; unsafe {
56         x + 1
57     }];
58
59     unknown_bracket_macro__comma_should_not_be_stripped![
60     a,
61     ];
62     
63     foo(makro!(1,   3));
64
65     hamkaas!{ () };
66
67     macrowithbraces! {dont,    format, me}
68
69     x!(fn);
70
71     some_macro!(
72         
73     );
74
75     some_macro![
76     ];
77
78     some_macro!{
79         // comment
80     };
81
82     some_macro!{
83         // comment
84     };
85
86     some_macro!(
87         // comment
88         not function like
89     );
90
91     // #1712
92     let image = gray_image!(
93         00, 01, 02;
94         10, 11, 12;
95         20, 21, 22);
96
97     // #1092
98     chain!(input, a:take!(max_size), || []);
99
100     // #2727
101     foo!("bar")
102 ;
103 }
104
105 impl X {
106     empty_invoc!{}
107     empty_invoc! {}
108 }
109
110 fn issue_1279() {
111     println!("dsfs"); // a comment
112 }
113
114 fn issue_1555() {
115     let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
116                          "65454654654654654654654655464",
117                          "4");
118 }
119
120 fn issue1178() {
121     macro_rules! foo {
122         (#[$attr:meta] $name:ident) => {}
123     }
124
125     foo!(#[doc = "bar"] baz);
126 }
127
128 fn issue1739() {
129     sql_function!(add_rss_item,
130                   add_rss_item_t,
131                   (a: types::Integer,
132                    b: types::Timestamptz,
133                    c: types::Text,
134                    d: types::Text,
135                    e: types::Text));
136
137     w.slice_mut(s![.., init_size[1] - extreeeeeeeeeeeeeeeeeeeeeeeem..init_size[1], ..])
138         .par_map_inplace(|el| *el = 0.);
139 }
140
141 fn issue_1885() {
142     let threads = people.into_iter().map(|name| {
143         chan_select! {
144             rx.recv() => {}
145         }
146     }).collect::<Vec<_>>();
147 }
148
149 fn issue_1917() {
150     mod x {
151         quickcheck! {
152             fn test(a: String, s: String, b: String) -> TestResult {
153                 if a.find(&s).is_none() {
154
155                     TestResult::from_bool(true)
156                 } else {
157                     TestResult::discard()
158                 }
159             }
160         }
161     }
162 }
163
164 fn issue_1921() {
165     // Macro with tabs.
166     lazy_static! {
167         static ref ONE: u32 = 1;
168         static ref TWO: u32 = 2;
169         static ref THREE: u32 = 3;
170         static ref FOUR: u32 = {
171                 let mut acc = 1;
172                 acc += 1;
173                 acc += 2;
174                 acc
175         }
176 }
177 }
178
179 // #1577
180 fn issue1577() {
181     let json = json!({
182         "foo": "bar",
183     });
184 }
185
186 // #3174
187 fn issue_3174() {
188     let data =
189         if let Some(debug) = error.debug_info() {
190             json!({
191                 "errorKind": format!("{:?}", error.err_kind()),
192                 "debugMessage": debug.message,
193             })
194         } else {
195             json!({"errorKind": format!("{:?}", error.err_kind())})
196         };
197 }
198
199 gfx_pipeline!(pipe {
200     vbuf: gfx::VertexBuffer<Vertex> = (),
201     out: gfx::RenderTarget<ColorFormat> = "Target0",
202 });
203
204 // #1919
205 #[test]
206 fn __bindgen_test_layout_HandleWithDtor_open0_int_close0_instantiation() {
207     assert_eq!(
208         ::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>(),
209         8usize,
210         concat!(
211             "Size of template specialization: ",
212             stringify ! ( HandleWithDtor < :: std :: os :: raw :: c_int > )
213         )
214     );
215     assert_eq ! ( :: std :: mem :: align_of :: < HandleWithDtor < :: std :: os :: raw :: c_int > > ( ) , 8usize , concat ! ( "Alignment of template specialization: " , stringify ! ( HandleWithDtor < :: std :: os :: raw :: c_int > ) ) );
216 }
217
218 // #878
219 macro_rules! try_opt {
220     ($expr:expr) => (match $expr {  
221         Some(val) => val,
222           
223         None => { return None; }
224     })
225 }
226
227 // #2214
228 // macro call whose argument is an array with trailing comma.
229 fn issue2214() {
230 make_test!(str_searcher_ascii_haystack, "bb", "abbcbbd", [
231     Reject(0, 1),
232     Match (1, 3),
233     Reject(3, 4),
234     Match (4, 6),
235     Reject(6, 7),
236 ]);
237 }
238
239 fn special_case_macros() {
240     let p = eprint!();
241     let q = eprint!("{}", 1);
242     let r = eprint!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
243     let s = eprint!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
244
245     let q = eprintln!("{}", 1);
246     let r = eprintln!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
247     let s = eprintln!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
248
249     let q = format!("{}", 1);
250     let r = format!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
251     let s = format!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
252
253     let q = format_args!("{}", 1);
254     let r = format_args!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
255     let s = format_args!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
256
257     let q = print!("{}", 1);
258     let r = print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
259     let s = print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
260
261     let q = println!("{}", 1);
262     let r = println!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
263     let s = println!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
264
265     let q = unreachable!("{}", 1);
266     let r = unreachable!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
267     let s = unreachable!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
268
269     debug!("{}", 1);
270     debug!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
271     debug!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
272
273     error!("{}", 1);
274     error!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
275     error!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
276
277     info!("{}", 1);
278     info!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
279     info!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
280
281     panic!("{}", 1);
282     panic!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
283     panic!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
284
285     warn!("{}", 1);
286     warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
287     warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
288
289     assert!();
290     assert!(result == 42);
291     assert!(result == 42, "Ahoy there, {}!", target);
292     assert!(result == 42, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
293     assert!(result == 42, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
294
295     assert_eq!();
296     assert_eq!(left);
297     assert_eq!(left, right);
298     assert_eq!(left, right, "Ahoy there, {}!", target);
299     assert_eq!(left, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
300     assert_eq!(first_realllllllllllly_long_variable_that_doesnt_fit_one_one_line, second_reallllllllllly_long_variable_that_doesnt_fit_one_one_line, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
301     assert_eq!(left + 42, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
302     assert_eq!(left, right, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
303
304     write!(&mut s, "Ahoy there, {}!", target);
305     write!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
306     write!(&mut s, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
307
308     writeln!(&mut s, "Ahoy there, {}!", target);
309     writeln!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
310     writeln!(&mut s, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
311 }
312
313 // #1209
314 impl Foo {
315     /// foo
316     pub fn foo(&self) -> Bar<foo!(   )> {}
317 }
318
319 // #819
320 fn macro_in_pattern_position () {
321     let x = match y {
322         foo!(  ) => (),
323         bar!(            a, b,
324                          c) => (),
325         bar!(a
326              , b
327              , c
328              ,) => (),
329         baz!( 1 + 2 + 3, quux.kaas(  )
330         ) => (),
331         quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB) => (),
332     };
333 }
334
335 macro foo() {
336
337
338 }
339
340 pub macro bar($x:ident+$y:expr; ) {
341     fn foo($x: Foo) {
342     long_function(a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA),
343                   $x.bar($y));
344     }
345 }
346
347 macro foo() {
348   // a comment
349   fn foo() {
350   // another comment
351   bar();
352   }
353 }
354
355 // #2574
356 macro_rules! test {
357     () => {{}}
358 }
359
360 macro lex_err($kind: ident $(, $body: expr)*) {
361     Err(QlError::LexError(LexError::$kind($($body,)*)))
362 }
363
364 // Preserve trailing comma on item-level macro with `()` or `[]`.
365 methods![ get, post, delete, ];
366 methods!( get, post, delete, );
367
368 // #2588
369 macro_rules! m {
370     () => {
371         r#"
372             test
373         "#
374     };
375 }
376 fn foo() {
377     f!{r#"
378             test
379        "#};
380 }
381
382 // #2591
383 fn foo() {
384     match 0u32 {
385         0 => (),
386         _ => unreachable!(/* obviously */),
387     }
388 }
389
390 fn foo() {
391     let _ = column!(/* here */);
392 }
393
394 // #2616
395 // Preserve trailing comma when using mixed layout for macro call.
396 fn foo() {
397     foo!(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
398     foo!(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,);
399 }
400
401 // #2830
402 // Preserve trailing comma-less/ness inside nested macro.
403 named!(
404     do_parse_gsv<GsvData>,
405     map_res!(
406         do_parse!(
407             number_of_sentences: map_res!(digit, parse_num::<u16>)
408                 >> char!(',')
409                 >> sentence_index: map_res!(digit, parse_num::<u16>)
410                 >> char!(',')
411                 >> total_number_of_sats: map_res!(digit, parse_num::<u16>)
412                 >> char!(',')
413                 >> sat0: opt!(complete!(parse_gsv_sat_info))
414                 >> sat1: opt!(complete!(parse_gsv_sat_info))
415                 >> sat2: opt!(complete!(parse_gsv_sat_info))
416                 >> sat3: opt!(complete!(parse_gsv_sat_info))
417                 >> (
418                     number_of_sentences,
419                     sentence_index,
420                     total_number_of_sats,
421                     sat0,
422                     sat1,
423                     sat2,
424                     sat3
425                 )
426         ),
427         construct_gsv_data
428     )
429 );
430
431 // #2857
432 convert_args!(vec!(1, 2, 3));
433
434 // #3031
435 thread_local!(
436 /// TLV Holds a set of JSTraceables that need to be rooted
437     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
438         RefCell::new(RootedTraceableSet::new()) ;
439 ) ;
440
441 thread_local![
442     /// TLV Holds a set of JSTraceables that need to be rooted
443     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
444         RefCell::new(RootedTraceableSet::new()) ;
445
446     /// TLV Holds a set of JSTraceables that need to be rooted
447     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
448         RefCell::new(RootedTraceableSet::new(0)) ;
449
450     /// TLV Holds a set of JSTraceables that need to be rooted
451     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
452         RefCell::new(RootedTraceableSet::new(), xxx, yyy) ;
453
454     /// TLV Holds a set of JSTraceables that need to be rooted
455 static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
456         RefCell::new(RootedTraceableSet::new(1234)) ;
457
458 ] ;
459
460 fn issue3004() {
461     foo!(|_| { (  ) });
462     stringify!(( foo+ ));
463 }
464
465 // #3331
466 pub fn fold_abi<V: Fold + ?Sized>(_visitor: &mut V, _i: Abi) -> Abi {
467     Abi {
468         extern_token: Token ! [ extern ](tokens_helper(_visitor, &_i.extern_token.span)),
469         name: (_i.name).map(|it| _visitor.fold_lit_str(it)),
470     }
471 }
472
473 // #3463
474 x ! {()}
475
476 // #3746
477 f!(match a {
478     4 =>
479         &[
480             (3, false), // Missing
481             (4, true)   // I-frame
482         ]  [..],
483 });
484
485 // #3583
486 foo!(|x = y|);