]> git.lizzy.rs Git - rust.git/blob - tests/source/macros.rs
Merge pull request #3116 from kellerkindt/patch-1
[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 gfx_pipeline!(pipe {
188     vbuf: gfx::VertexBuffer<Vertex> = (),
189     out: gfx::RenderTarget<ColorFormat> = "Target0",
190 });
191
192 // #1919
193 #[test]
194 fn __bindgen_test_layout_HandleWithDtor_open0_int_close0_instantiation() {
195     assert_eq!(
196         ::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>(),
197         8usize,
198         concat!(
199             "Size of template specialization: ",
200             stringify ! ( HandleWithDtor < :: std :: os :: raw :: c_int > )
201         )
202     );
203     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 > ) ) );
204 }
205
206 // #878
207 macro_rules! try_opt {
208     ($expr:expr) => (match $expr {  
209         Some(val) => val,
210           
211         None => { return None; }
212     })
213 }
214
215 // #2214
216 // macro call whose argument is an array with trailing comma.
217 fn issue2214() {
218 make_test!(str_searcher_ascii_haystack, "bb", "abbcbbd", [
219     Reject(0, 1),
220     Match (1, 3),
221     Reject(3, 4),
222     Match (4, 6),
223     Reject(6, 7),
224 ]);
225 }
226
227 fn special_case_macros() {
228     let p = eprint!();
229     let q = eprint!("{}", 1);
230     let r = eprint!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
231     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);
232
233     let q = eprintln!("{}", 1);
234     let r = eprintln!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
235     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);
236
237     let q = format!("{}", 1);
238     let r = format!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
239     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);
240
241     let q = format_args!("{}", 1);
242     let r = format_args!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
243     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);
244
245     let q = print!("{}", 1);
246     let r = print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
247     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);
248
249     let q = println!("{}", 1);
250     let r = println!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
251     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);
252
253     let q = unreachable!("{}", 1);
254     let r = unreachable!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
255     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);
256
257     debug!("{}", 1);
258     debug!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
259     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);
260
261     error!("{}", 1);
262     error!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
263     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);
264
265     info!("{}", 1);
266     info!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
267     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);
268
269     panic!("{}", 1);
270     panic!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
271     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);
272
273     warn!("{}", 1);
274     warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
275     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);
276
277     assert!();
278     assert!(result == 42);
279     assert!(result == 42, "Ahoy there, {}!", target);
280     assert!(result == 42, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
281     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);
282
283     assert_eq!();
284     assert_eq!(left);
285     assert_eq!(left, right);
286     assert_eq!(left, right, "Ahoy there, {}!", target);
287     assert_eq!(left, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
288     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);
289     assert_eq!(left + 42, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
290     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);
291
292     write!(&mut s, "Ahoy there, {}!", target);
293     write!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
294     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);
295
296     writeln!(&mut s, "Ahoy there, {}!", target);
297     writeln!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
298     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);
299 }
300
301 // #1209
302 impl Foo {
303     /// foo
304     pub fn foo(&self) -> Bar<foo!(   )> {}
305 }
306
307 // #819
308 fn macro_in_pattern_position () {
309     let x = match y {
310         foo!(  ) => (),
311         bar!(            a, b,
312                          c) => (),
313         bar!(a
314              , b
315              , c
316              ,) => (),
317         baz!( 1 + 2 + 3, quux.kaas(  )
318         ) => (),
319         quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB) => (),
320     };
321 }
322
323 macro foo() {
324
325
326 }
327
328 pub macro bar($x:ident+$y:expr; ) {
329     fn foo($x: Foo) {
330     long_function(a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA),
331                   $x.bar($y));
332     }
333 }
334
335 macro foo() {
336   // a comment
337   fn foo() {
338   // another comment
339   bar();
340   }
341 }
342
343 // #2574
344 macro_rules! test {
345     () => {{}}
346 }
347
348 macro lex_err($kind: ident $(, $body: expr)*) {
349     Err(QlError::LexError(LexError::$kind($($body,)*)))
350 }
351
352 // Preserve trailing comma on item-level macro with `()` or `[]`.
353 methods![ get, post, delete, ];
354 methods!( get, post, delete, );
355
356 // #2588
357 macro_rules! m {
358     () => {
359         r#"
360             test
361         "#
362     };
363 }
364 fn foo() {
365     f!{r#"
366             test
367        "#};
368 }
369
370 // #2591
371 fn foo() {
372     match 0u32 {
373         0 => (),
374         _ => unreachable!(/* obviously */),
375     }
376 }
377
378 fn foo() {
379     let _ = column!(/* here */);
380 }
381
382 // #2616
383 // Preserve trailing comma when using mixed layout for macro call.
384 fn foo() {
385     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);
386     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,);
387 }
388
389 // #2652
390 // Preserve trailing comma inside macro, even if it looks an array.
391 macro_rules! bar {
392     ($m:ident) => {
393         $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]);
394     };
395 }
396
397 // #2830
398 // Preserve trailing comma-less/ness inside nested macro.
399 named!(
400     do_parse_gsv<GsvData>,
401     map_res!(
402         do_parse!(
403             number_of_sentences: map_res!(digit, parse_num::<u16>)
404                 >> char!(',')
405                 >> sentence_index: map_res!(digit, parse_num::<u16>)
406                 >> char!(',')
407                 >> total_number_of_sats: map_res!(digit, parse_num::<u16>)
408                 >> char!(',')
409                 >> sat0: opt!(complete!(parse_gsv_sat_info))
410                 >> sat1: opt!(complete!(parse_gsv_sat_info))
411                 >> sat2: opt!(complete!(parse_gsv_sat_info))
412                 >> sat3: opt!(complete!(parse_gsv_sat_info))
413                 >> (
414                     number_of_sentences,
415                     sentence_index,
416                     total_number_of_sats,
417                     sat0,
418                     sat1,
419                     sat2,
420                     sat3
421                 )
422         ),
423         construct_gsv_data
424     )
425 );
426
427 // #2857
428 convert_args!(vec!(1, 2, 3));
429
430 // #3031
431 thread_local!(
432 /// TLV Holds a set of JSTraceables that need to be rooted
433     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
434         RefCell::new(RootedTraceableSet::new()) ;
435 ) ;
436
437 thread_local![
438     /// TLV Holds a set of JSTraceables that need to be rooted
439     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
440         RefCell::new(RootedTraceableSet::new()) ;
441
442     /// TLV Holds a set of JSTraceables that need to be rooted
443     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
444         RefCell::new(RootedTraceableSet::new(0)) ;
445
446     /// TLV Holds a set of JSTraceables that need to be rooted
447     static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
448         RefCell::new(RootedTraceableSet::new(), xxx, yyy) ;
449
450     /// TLV Holds a set of JSTraceables that need to be rooted
451 static ROOTED_TRACEABLES: RefCell<RootedTraceableSet> =
452         RefCell::new(RootedTraceableSet::new(1234)) ;
453
454 ] ;