]> git.lizzy.rs Git - rust.git/blob - tests/target/macros.rs
d430f5711dd8e7d034cfc9aa68b5c380d1250caf
[rust.git] / tests / target / macros.rs
1 // rustfmt-normalize_comments: true
2 itemmacro!(this, is.now().formatted(yay));
3
4 itemmacro!(
5     really,
6     long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb()
7         .is
8         .formatted()
9 );
10
11 itemmacro!{this, is.bracket().formatted()}
12
13 peg_file! modname("mygrammarfile.rustpeg");
14
15 fn main() {
16     foo!();
17
18     foo!(,);
19
20     bar!(a, b, c);
21
22     bar!(a, b, c,);
23
24     baz!(1 + 2 + 3, quux.kaas());
25
26     quux!(
27         AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
28         BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
29     );
30
31     kaas!(
32         // comments
33         a, // post macro
34         b  // another
35     );
36
37     trailingcomma!(a, b, c,);
38     // Preserve trailing comma only when necessary.
39     ok!(file.seek(SeekFrom::Start(
40         table.map(|table| fixture.offset(table)).unwrap_or(0),
41     )));
42
43     noexpr!( i am not an expression, OK? );
44
45     vec![a, b, c];
46
47     vec![
48         AAAAAA,
49         AAAAAA,
50         AAAAAA,
51         AAAAAA,
52         AAAAAA,
53         AAAAAA,
54         AAAAAA,
55         AAAAAA,
56         AAAAAA,
57         BBBBB,
58         5,
59         100 - 30,
60         1.33,
61         b,
62         b,
63         b,
64     ];
65
66     vec![a /* comment */];
67
68     // Trailing spaces after a comma
69     vec![a];
70
71     vec![a; b];
72     vec![a; b];
73     vec![a; b];
74
75     vec![a, b; c];
76     vec![a; b, c];
77
78     vec![
79         a;
80         (|x| {
81             let y = x + 1;
82             let z = y + 1;
83             z
84         })(2)
85     ];
86     vec![
87         a;
88         xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
89     ];
90     vec![a; unsafe { x + 1 }];
91
92     unknown_bracket_macro__comma_should_not_be_stripped![a,];
93
94     foo(makro!(1, 3));
95
96     hamkaas!{ () };
97
98     macrowithbraces! {dont,    format, me}
99
100     x!(fn);
101
102     some_macro!();
103
104     some_macro![];
105
106     some_macro!{
107         // comment
108     };
109
110     some_macro!{
111         // comment
112     };
113
114     some_macro!(
115         // comment
116         not function like
117     );
118
119     // #1712
120     let image = gray_image!(
121         00, 01, 02;
122         10, 11, 12;
123         20, 21, 22);
124
125     // #1092
126     chain!(input, a: take!(max_size), || []);
127 }
128
129 impl X {
130     empty_invoc!{}
131 }
132
133 fn issue_1279() {
134     println!("dsfs"); // a comment
135 }
136
137 fn issue_1555() {
138     let hello = &format!(
139         "HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
140         "65454654654654654654654655464", "4"
141     );
142 }
143
144 fn issue1178() {
145     macro_rules! foo {
146         (#[$attr:meta] $name:ident) => {};
147     }
148
149     foo!(
150         #[doc = "bar"]
151         baz
152     );
153 }
154
155 fn issue1739() {
156     sql_function!(
157         add_rss_item,
158         add_rss_item_t,
159         (
160             a: types::Integer,
161             b: types::Timestamptz,
162             c: types::Text,
163             d: types::Text,
164             e: types::Text
165         )
166     );
167
168     w.slice_mut(s![
169         ..,
170         init_size[1] - extreeeeeeeeeeeeeeeeeeeeeeeem..init_size[1],
171         ..
172     ]).par_map_inplace(|el| *el = 0.);
173 }
174
175 fn issue_1885() {
176     let threads = people
177         .into_iter()
178         .map(|name| {
179             chan_select! {
180                 rx.recv() => {}
181             }
182         })
183         .collect::<Vec<_>>();
184 }
185
186 fn issue_1917() {
187     mod x {
188         quickcheck! {
189             fn test(a: String, s: String, b: String) -> TestResult {
190                 if a.find(&s).is_none() {
191
192                     TestResult::from_bool(true)
193                 } else {
194                     TestResult::discard()
195                 }
196             }
197         }
198     }
199 }
200
201 fn issue_1921() {
202     // Macro with tabs.
203     lazy_static! {
204         static ref ONE: u32 = 1;
205         static ref TWO: u32 = 2;
206         static ref THREE: u32 = 3;
207         static ref FOUR: u32 = {
208             let mut acc = 1;
209             acc += 1;
210             acc += 2;
211             acc
212         };
213     }
214 }
215
216 // #1577
217 fn issue1577() {
218     let json = json!({
219         "foo": "bar",
220     });
221 }
222
223 gfx_pipeline!(pipe {
224     vbuf: gfx::VertexBuffer<Vertex> = (),
225     out: gfx::RenderTarget<ColorFormat> = "Target0",
226 });
227
228 // #1919
229 #[test]
230 fn __bindgen_test_layout_HandleWithDtor_open0_int_close0_instantiation() {
231     assert_eq!(
232         ::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>(),
233         8usize,
234         concat!(
235             "Size of template specialization: ",
236             stringify!(HandleWithDtor<::std::os::raw::c_int>)
237         )
238     );
239     assert_eq!(
240         ::std::mem::align_of::<HandleWithDtor<::std::os::raw::c_int>>(),
241         8usize,
242         concat!(
243             "Alignment of template specialization: ",
244             stringify!(HandleWithDtor<::std::os::raw::c_int>)
245         )
246     );
247 }
248
249 // #878
250 macro_rules! try_opt {
251     ($expr:expr) => {
252         match $expr {
253             Some(val) => val,
254
255             None => {
256                 return None;
257             }
258         }
259     };
260 }
261
262 // #2214
263 // macro call whose argument is an array with trailing comma.
264 fn issue2214() {
265     make_test!(
266         str_searcher_ascii_haystack,
267         "bb",
268         "abbcbbd",
269         [
270             Reject(0, 1),
271             Match(1, 3),
272             Reject(3, 4),
273             Match(4, 6),
274             Reject(6, 7),
275         ]
276     );
277 }
278
279 fn special_case_macros() {
280     let p = eprint!();
281     let q = eprint!("{}", 1);
282     let r = eprint!(
283         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
284         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
285     );
286     let s = eprint!(
287         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
288         1,
289         2,
290         3,
291         4,
292         5,
293         6,
294         7,
295         8,
296         9,
297         10,
298         11,
299         12,
300         13,
301         14,
302         15,
303         16,
304         17,
305         18,
306         19,
307         20,
308         21,
309         22,
310         23,
311         24,
312         25,
313         26
314     );
315
316     let q = eprintln!("{}", 1);
317     let r = eprintln!(
318         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
319         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
320     );
321     let s = eprintln!(
322         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
323         1,
324         2,
325         3,
326         4,
327         5,
328         6,
329         7,
330         8,
331         9,
332         10,
333         11,
334         12,
335         13,
336         14,
337         15,
338         16,
339         17,
340         18,
341         19,
342         20,
343         21,
344         22,
345         23,
346         24,
347         25,
348         26
349     );
350
351     let q = format!("{}", 1);
352     let r = format!(
353         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
354         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
355     );
356     let s = format!(
357         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
358         1,
359         2,
360         3,
361         4,
362         5,
363         6,
364         7,
365         8,
366         9,
367         10,
368         11,
369         12,
370         13,
371         14,
372         15,
373         16,
374         17,
375         18,
376         19,
377         20,
378         21,
379         22,
380         23,
381         24,
382         25,
383         26
384     );
385
386     let q = format_args!("{}", 1);
387     let r = format_args!(
388         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
389         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
390     );
391     let s = format_args!(
392         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
393         1,
394         2,
395         3,
396         4,
397         5,
398         6,
399         7,
400         8,
401         9,
402         10,
403         11,
404         12,
405         13,
406         14,
407         15,
408         16,
409         17,
410         18,
411         19,
412         20,
413         21,
414         22,
415         23,
416         24,
417         25,
418         26
419     );
420
421     let q = print!("{}", 1);
422     let r = print!(
423         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
424         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
425     );
426     let s = print!(
427         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
428         1,
429         2,
430         3,
431         4,
432         5,
433         6,
434         7,
435         8,
436         9,
437         10,
438         11,
439         12,
440         13,
441         14,
442         15,
443         16,
444         17,
445         18,
446         19,
447         20,
448         21,
449         22,
450         23,
451         24,
452         25,
453         26
454     );
455
456     let q = println!("{}", 1);
457     let r = println!(
458         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
459         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
460     );
461     let s = println!(
462         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
463         1,
464         2,
465         3,
466         4,
467         5,
468         6,
469         7,
470         8,
471         9,
472         10,
473         11,
474         12,
475         13,
476         14,
477         15,
478         16,
479         17,
480         18,
481         19,
482         20,
483         21,
484         22,
485         23,
486         24,
487         25,
488         26
489     );
490
491     let q = unreachable!("{}", 1);
492     let r = unreachable!(
493         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
494         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
495     );
496     let s = unreachable!(
497         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
498         1,
499         2,
500         3,
501         4,
502         5,
503         6,
504         7,
505         8,
506         9,
507         10,
508         11,
509         12,
510         13,
511         14,
512         15,
513         16,
514         17,
515         18,
516         19,
517         20,
518         21,
519         22,
520         23,
521         24,
522         25,
523         26
524     );
525
526     debug!("{}", 1);
527     debug!(
528         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
529         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
530     );
531     debug!(
532         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
533         1,
534         2,
535         3,
536         4,
537         5,
538         6,
539         7,
540         8,
541         9,
542         10,
543         11,
544         12,
545         13,
546         14,
547         15,
548         16,
549         17,
550         18,
551         19,
552         20,
553         21,
554         22,
555         23,
556         24,
557         25,
558         26
559     );
560
561     error!("{}", 1);
562     error!(
563         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
564         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
565     );
566     error!(
567         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
568         1,
569         2,
570         3,
571         4,
572         5,
573         6,
574         7,
575         8,
576         9,
577         10,
578         11,
579         12,
580         13,
581         14,
582         15,
583         16,
584         17,
585         18,
586         19,
587         20,
588         21,
589         22,
590         23,
591         24,
592         25,
593         26
594     );
595
596     info!("{}", 1);
597     info!(
598         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
599         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
600     );
601     info!(
602         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
603         1,
604         2,
605         3,
606         4,
607         5,
608         6,
609         7,
610         8,
611         9,
612         10,
613         11,
614         12,
615         13,
616         14,
617         15,
618         16,
619         17,
620         18,
621         19,
622         20,
623         21,
624         22,
625         23,
626         24,
627         25,
628         26
629     );
630
631     panic!("{}", 1);
632     panic!(
633         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
634         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
635     );
636     panic!(
637         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
638         1,
639         2,
640         3,
641         4,
642         5,
643         6,
644         7,
645         8,
646         9,
647         10,
648         11,
649         12,
650         13,
651         14,
652         15,
653         16,
654         17,
655         18,
656         19,
657         20,
658         21,
659         22,
660         23,
661         24,
662         25,
663         26
664     );
665
666     warn!("{}", 1);
667     warn!(
668         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
669         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
670     );
671     warn!(
672         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
673         1,
674         2,
675         3,
676         4,
677         5,
678         6,
679         7,
680         8,
681         9,
682         10,
683         11,
684         12,
685         13,
686         14,
687         15,
688         16,
689         17,
690         18,
691         19,
692         20,
693         21,
694         22,
695         23,
696         24,
697         25,
698         26
699     );
700
701     assert!();
702     assert!(result == 42);
703     assert!(result == 42, "Ahoy there, {}!", target);
704     assert!(
705         result == 42,
706         "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
707         result,
708         input,
709         expected
710     );
711     assert!(
712         result == 42,
713         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
714         1,
715         2,
716         3,
717         4,
718         5,
719         6,
720         7,
721         8,
722         9,
723         10,
724         11,
725         12,
726         13,
727         14,
728         15,
729         16,
730         17,
731         18,
732         19,
733         20,
734         21,
735         22,
736         23,
737         24,
738         25,
739         26
740     );
741
742     assert_eq!();
743     assert_eq!(left);
744     assert_eq!(left, right);
745     assert_eq!(left, right, "Ahoy there, {}!", target);
746     assert_eq!(
747         left, right,
748         "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
749         result, input, expected
750     );
751     assert_eq!(
752         first_realllllllllllly_long_variable_that_doesnt_fit_one_one_line,
753         second_reallllllllllly_long_variable_that_doesnt_fit_one_one_line,
754         "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
755         result,
756         input,
757         expected
758     );
759     assert_eq!(
760         left + 42,
761         right,
762         "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
763         result,
764         input,
765         expected
766     );
767     assert_eq!(
768         left,
769         right,
770         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
771         1,
772         2,
773         3,
774         4,
775         5,
776         6,
777         7,
778         8,
779         9,
780         10,
781         11,
782         12,
783         13,
784         14,
785         15,
786         16,
787         17,
788         18,
789         19,
790         20,
791         21,
792         22,
793         23,
794         24,
795         25,
796         26
797     );
798
799     write!(&mut s, "Ahoy there, {}!", target);
800     write!(
801         &mut s,
802         "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
803         result, input, expected
804     );
805     write!(
806         &mut s,
807         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
808         1,
809         2,
810         3,
811         4,
812         5,
813         6,
814         7,
815         8,
816         9,
817         10,
818         11,
819         12,
820         13,
821         14,
822         15,
823         16,
824         17,
825         18,
826         19,
827         20,
828         21,
829         22,
830         23,
831         24,
832         25,
833         26
834     );
835
836     writeln!(&mut s, "Ahoy there, {}!", target);
837     writeln!(
838         &mut s,
839         "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')",
840         result, input, expected
841     );
842     writeln!(
843         &mut s,
844         "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
845         1,
846         2,
847         3,
848         4,
849         5,
850         6,
851         7,
852         8,
853         9,
854         10,
855         11,
856         12,
857         13,
858         14,
859         15,
860         16,
861         17,
862         18,
863         19,
864         20,
865         21,
866         22,
867         23,
868         24,
869         25,
870         26
871     );
872 }
873
874 // #1209
875 impl Foo {
876     /// foo
877     pub fn foo(&self) -> Bar<foo!()> {}
878 }
879
880 // #819
881 fn macro_in_pattern_position() {
882     let x = match y {
883         foo!() => (),
884         bar!(a, b, c) => (),
885         bar!(a, b, c,) => (),
886         baz!(1 + 2 + 3, quux.kaas()) => (),
887         quux!(
888             AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
889             BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
890         ) => (),
891     };
892 }
893
894 macro foo() {}
895
896 pub macro bar($x:ident + $y:expr;) {
897     fn foo($x: Foo) {
898         long_function(
899             a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA),
900             $x.bar($y),
901         );
902     }
903 }
904
905 macro foo() {
906     // a comment
907     fn foo() {
908         // another comment
909         bar();
910     }
911 }
912
913 // #2574
914 macro_rules! test {
915     () => {{}};
916 }
917
918 macro lex_err($kind: ident $(, $body: expr)*) {
919     Err(QlError::LexError(LexError::$kind($($body,)*)))
920 }
921
922 // Preserve trailing comma on item-level macro with `()` or `[]`.
923 methods![get, post, delete,];
924 methods!(get, post, delete,);
925
926 // #2588
927 macro_rules! m {
928     () => {
929         r#"
930             test
931         "#
932     };
933 }
934 fn foo() {
935     f!{r#"
936             test
937        "#};
938 }
939
940 // #2591
941 fn foo() {
942     match 0u32 {
943         0 => (),
944         _ => unreachable!(/* obviously */),
945     }
946 }
947
948 fn foo() {
949     let _ = column!(/* here */);
950 }