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