]> git.lizzy.rs Git - rust.git/blob - tests/source/macros.rs
compute the span after a struct-like item based on the ident description
[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
111 fn issue_1279() {
112     println!("dsfs"); // a comment
113 }
114
115 fn issue_1555() {
116     let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
117                          "65454654654654654654654655464",
118                          "4");
119 }
120
121 fn issue1178() {
122     macro_rules! foo {
123         (#[$attr:meta] $name:ident) => {}
124     }
125
126     foo!(#[doc = "bar"] baz);
127 }
128
129 fn issue1739() {
130     sql_function!(add_rss_item,
131                   add_rss_item_t,
132                   (a: types::Integer,
133                    b: types::Timestamptz,
134                    c: types::Text,
135                    d: types::Text,
136                    e: types::Text));
137
138     w.slice_mut(s![.., init_size[1] - extreeeeeeeeeeeeeeeeeeeeeeeem..init_size[1], ..])
139         .par_map_inplace(|el| *el = 0.);
140 }
141
142 fn issue_1885() {
143     let threads = people.into_iter().map(|name| {
144         chan_select! {
145             rx.recv() => {}
146         }
147     }).collect::<Vec<_>>();
148 }
149
150 fn issue_1917() {
151     mod x {
152         quickcheck! {
153             fn test(a: String, s: String, b: String) -> TestResult {
154                 if a.find(&s).is_none() {
155
156                     TestResult::from_bool(true)
157                 } else {
158                     TestResult::discard()
159                 }
160             }
161         }
162     }
163 }
164
165 fn issue_1921() {
166     // Macro with tabs.
167     lazy_static! {
168         static ref ONE: u32 = 1;
169         static ref TWO: u32 = 2;
170         static ref THREE: u32 = 3;
171         static ref FOUR: u32 = {
172                 let mut acc = 1;
173                 acc += 1;
174                 acc += 2;
175                 acc
176         }
177 }
178 }
179
180 // #1577
181 fn issue1577() {
182     let json = json!({
183         "foo": "bar",
184     });
185 }
186
187 // #3174
188 fn issue_3174() {
189     let data =
190         if let Some(debug) = error.debug_info() {
191             json!({
192                 "errorKind": format!("{:?}", error.err_kind()),
193                 "debugMessage": debug.message,
194             })
195         } else {
196             json!({"errorKind": format!("{:?}", error.err_kind())})
197         };
198 }
199
200 gfx_pipeline!(pipe {
201     vbuf: gfx::VertexBuffer<Vertex> = (),
202     out: gfx::RenderTarget<ColorFormat> = "Target0",
203 });
204
205 // #1919
206 #[test]
207 fn __bindgen_test_layout_HandleWithDtor_open0_int_close0_instantiation() {
208     assert_eq!(
209         ::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>(),
210         8usize,
211         concat!(
212             "Size of template specialization: ",
213             stringify ! ( HandleWithDtor < :: std :: os :: raw :: c_int > )
214         )
215     );
216     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 > ) ) );
217 }
218
219 // #878
220 macro_rules! try_opt {
221     ($expr:expr) => (match $expr {  
222         Some(val) => val,
223           
224         None => { return None; }
225     })
226 }
227
228 // #2214
229 // macro call whose argument is an array with trailing comma.
230 fn issue2214() {
231 make_test!(str_searcher_ascii_haystack, "bb", "abbcbbd", [
232     Reject(0, 1),
233     Match (1, 3),
234     Reject(3, 4),
235     Match (4, 6),
236     Reject(6, 7),
237 ]);
238 }
239
240 fn special_case_macros() {
241     let p = eprint!();
242     let q = eprint!("{}", 1);
243     let r = eprint!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
244     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);
245
246     let q = eprintln!("{}", 1);
247     let r = eprintln!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
248     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);
249
250     let q = format!("{}", 1);
251     let r = format!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
252     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);
253
254     let q = format_args!("{}", 1);
255     let r = format_args!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
256     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);
257
258     let q = print!("{}", 1);
259     let r = print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
260     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);
261
262     let q = println!("{}", 1);
263     let r = println!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
264     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);
265
266     let q = unreachable!("{}", 1);
267     let r = unreachable!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
268     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);
269
270     debug!("{}", 1);
271     debug!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
272     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);
273
274     error!("{}", 1);
275     error!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
276     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);
277
278     info!("{}", 1);
279     info!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
280     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);
281
282     panic!("{}", 1);
283     panic!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
284     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);
285
286     warn!("{}", 1);
287     warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
288     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);
289
290     assert!();
291     assert!(result == 42);
292     assert!(result == 42, "Ahoy there, {}!", target);
293     assert!(result == 42, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
294     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);
295
296     assert_eq!();
297     assert_eq!(left);
298     assert_eq!(left, right);
299     assert_eq!(left, right, "Ahoy there, {}!", target);
300     assert_eq!(left, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
301     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);
302     assert_eq!(left + 42, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
303     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);
304
305     write!(&mut s, "Ahoy there, {}!", target);
306     write!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
307     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);
308
309     writeln!(&mut s, "Ahoy there, {}!", target);
310     writeln!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
311     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);
312 }
313
314 // #1209
315 impl Foo {
316     /// foo
317     pub fn foo(&self) -> Bar<foo!(   )> {}
318 }
319
320 // #819
321 fn macro_in_pattern_position () {
322     let x = match y {
323         foo!(  ) => (),
324         bar!(            a, b,
325                          c) => (),
326         bar!(a
327              , b
328              , c
329              ,) => (),
330         baz!( 1 + 2 + 3, quux.kaas(  )
331         ) => (),
332         quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB) => (),
333     };
334 }
335
336 macro foo() {
337
338
339 }
340
341 pub macro bar($x:ident+$y:expr; ) {
342     fn foo($x: Foo) {
343     long_function(a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA),
344                   $x.bar($y));
345     }
346 }
347
348 macro foo() {
349   // a comment
350   fn foo() {
351   // another comment
352   bar();
353   }
354 }
355
356 // #2574
357 macro_rules! test {
358     () => {{}}
359 }
360
361 macro lex_err($kind: ident $(, $body: expr)*) {
362     Err(QlError::LexError(LexError::$kind($($body,)*)))
363 }
364
365 // Preserve trailing comma on item-level macro with `()` or `[]`.
366 methods![ get, post, delete, ];
367 methods!( get, post, delete, );
368
369 // #2588
370 macro_rules! m {
371     () => {
372         r#"
373             test
374         "#
375     };
376 }
377 fn foo() {
378     f!{r#"
379             test
380        "#};
381 }
382
383 // #2591
384 fn foo() {
385     match 0u32 {
386         0 => (),
387         _ => unreachable!(/* obviously */),
388     }
389 }
390
391 fn foo() {
392     let _ = column!(/* here */);
393 }
394
395 // #2616
396 // Preserve trailing comma when using mixed layout for macro call.
397 fn foo() {
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     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,);
400 }
401
402 // #2652
403 // Preserve trailing comma inside macro, even if it looks an array.
404 macro_rules! bar {
405     ($m:ident) => {
406         $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]);
407     };
408 }
409
410 // #2830
411 // Preserve trailing comma-less/ness inside nested macro.
412 named!(
413     do_parse_gsv<GsvData>,
414     map_res!(
415         do_parse!(
416             number_of_sentences: map_res!(digit, parse_num::<u16>)
417                 >> char!(',')
418                 >> sentence_index: map_res!(digit, parse_num::<u16>)
419                 >> char!(',')
420                 >> total_number_of_sats: map_res!(digit, parse_num::<u16>)
421                 >> char!(',')
422                 >> sat0: opt!(complete!(parse_gsv_sat_info))
423                 >> sat1: opt!(complete!(parse_gsv_sat_info))
424                 >> sat2: opt!(complete!(parse_gsv_sat_info))
425                 >> sat3: opt!(complete!(parse_gsv_sat_info))
426                 >> (
427                     number_of_sentences,
428                     sentence_index,
429                     total_number_of_sats,
430                     sat0,
431                     sat1,
432                     sat2,
433                     sat3
434                 )
435         ),
436         construct_gsv_data
437     )
438 );
439
440 // #2857
441 convert_args!(vec!(1, 2, 3));
442
443 // #3031
444 thread_local!(
445 /// TLV Holds a set of JSTraceables that need to be rooted
446     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
447         RefCell::new(RootedTraceableSet::new()) ;
448 ) ;
449
450 thread_local![
451     /// TLV Holds a set of JSTraceables that need to be rooted
452     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
453         RefCell::new(RootedTraceableSet::new()) ;
454
455     /// TLV Holds a set of JSTraceables that need to be rooted
456     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
457         RefCell::new(RootedTraceableSet::new(0)) ;
458
459     /// TLV Holds a set of JSTraceables that need to be rooted
460     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
461         RefCell::new(RootedTraceableSet::new(), xxx, yyy) ;
462
463     /// TLV Holds a set of JSTraceables that need to be rooted
464 static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
465         RefCell::new(RootedTraceableSet::new(1234)) ;
466
467 ] ;