]> git.lizzy.rs Git - rust.git/blob - tests/source/macros.rs
Tidy up and pass tests
[rust.git] / tests / source / macros.rs
1 // rustfmt-normalize_comments: true
2 itemmacro!(this, is.now() .formatted(yay));
3
4 itemmacro!(really, long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb() .is.formatted());
5
6 itemmacro!{this, is.bracket().formatted()}
7
8 peg_file!   modname  ("mygrammarfile.rustpeg");
9
10 fn main() {
11     foo! ( );
12
13     bar!( a , b , c );
14
15     bar!( a , b , c , );
16
17     baz!(1+2+3, quux. kaas());
18
19     quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB);
20
21     kaas!(/* comments */ a /* post macro */, b /* another */);
22
23     trailingcomma!( a , b , c , );
24     // Preserve trailing comma only when necessary.
25     ok!(file.seek(
26         SeekFrom::Start(
27             table.map(|table| fixture.offset(table)).unwrap_or(0),
28         )
29     ));
30
31     noexpr!( i am not an expression, OK? );
32
33     vec! [ a , b , c];
34
35     vec! [AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA,
36           BBBBB, 5, 100-30, 1.33, b, b, b];
37
38     vec! [a /* comment */];
39
40     // Trailing spaces after a comma
41     vec![
42     a,   
43     ];
44     
45     vec![a; b];
46     vec!(a; b);
47     vec!{a; b};
48
49     vec![a, b; c];
50     vec![a; b, c];
51
52     vec![a; (|x| { let y = x + 1; let z = y + 1; z })(2)];
53     vec![a; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx];
54     vec![a; unsafe {
55         x + 1
56     }];
57
58     unknown_bracket_macro__comma_should_not_be_stripped![
59     a,
60     ];
61     
62     foo(makro!(1,   3));
63
64     hamkaas!{ () };
65
66     macrowithbraces! {dont,    format, me}
67
68     x!(fn);
69
70     some_macro!(
71         
72     );
73
74     some_macro![
75     ];
76
77     some_macro!{
78         // comment
79     };
80
81     some_macro!{
82         // comment
83     };
84
85     some_macro!(
86         // comment
87         not function like
88     );
89
90     // #1712
91     let image = gray_image!(
92         00, 01, 02;
93         10, 11, 12;
94         20, 21, 22);
95
96     // #1092
97     chain!(input, a:take!(max_size), || []);
98 }
99
100 impl X {
101     empty_invoc!{}
102 }
103
104 fn issue_1279() {
105     println!("dsfs"); // a comment
106 }
107
108 fn issue_1555() {
109     let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
110                          "65454654654654654654654655464",
111                          "4");
112 }
113
114 fn issue1178() {
115     macro_rules! foo {
116         (#[$attr:meta] $name:ident) => {}
117     }
118
119     foo!(#[doc = "bar"] baz);
120 }
121
122 fn issue1739() {
123     sql_function!(add_rss_item,
124                   add_rss_item_t,
125                   (a: types::Integer,
126                    b: types::Timestamptz,
127                    c: types::Text,
128                    d: types::Text,
129                    e: types::Text));
130
131     w.slice_mut(s![.., init_size[1] - extreeeeeeeeeeeeeeeeeeeeeeeem..init_size[1], ..])
132         .par_map_inplace(|el| *el = 0.);
133 }
134
135 fn issue_1885() {
136     let threads = people.into_iter().map(|name| {
137         chan_select! {
138             rx.recv() => {}
139         }
140     }).collect::<Vec<_>>();
141 }
142
143 fn issue_1917() {
144     mod x {
145         quickcheck! {
146             fn test(a: String, s: String, b: String) -> TestResult {
147                 if a.find(&s).is_none() {
148
149                     TestResult::from_bool(true)
150                 } else {
151                     TestResult::discard()
152                 }
153             }
154         }
155     }
156 }
157
158 fn issue_1921() {
159     // Macro with tabs.
160     lazy_static! {
161         static ref ONE: u32 = 1;
162         static ref TWO: u32 = 2;
163         static ref THREE: u32 = 3;
164         static ref FOUR: u32 = {
165                 let mut acc = 1;
166                 acc += 1;
167                 acc += 2;
168                 acc
169         }
170 }
171 }
172
173 // #1577
174 fn issue1577() {
175     let json = json!({
176         "foo": "bar",
177     });
178 }
179
180 gfx_pipeline!(pipe {
181     vbuf: gfx::VertexBuffer<Vertex> = (),
182     out: gfx::RenderTarget<ColorFormat> = "Target0",
183 });
184
185 // #1919
186 #[test]
187 fn __bindgen_test_layout_HandleWithDtor_open0_int_close0_instantiation() {
188     assert_eq!(
189         ::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>(),
190         8usize,
191         concat!(
192             "Size of template specialization: ",
193             stringify ! ( HandleWithDtor < :: std :: os :: raw :: c_int > )
194         )
195     );
196     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 > ) ) );
197 }
198
199 // #878
200 macro_rules! try_opt {
201     ($expr:expr) => (match $expr {  
202         Some(val) => val,
203           
204         None => { return None; }
205     })
206 }
207
208 // #2214
209 // macro call whose argument is an array with trailing comma.
210 fn issue2214() {
211 make_test!(str_searcher_ascii_haystack, "bb", "abbcbbd", [
212     Reject(0, 1),
213     Match (1, 3),
214     Reject(3, 4),
215     Match (4, 6),
216     Reject(6, 7),
217 ]);
218 }
219
220 fn special_case_macros() {
221     let p = eprint!();
222     let q = eprint!("{}", 1);
223     let r = eprint!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
224     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);
225
226     let q = eprintln!("{}", 1);
227     let r = eprintln!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
228     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);
229
230     let q = format!("{}", 1);
231     let r = format!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
232     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);
233
234     let q = format_args!("{}", 1);
235     let r = format_args!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
236     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);
237
238     let q = print!("{}", 1);
239     let r = print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
240     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);
241
242     let q = println!("{}", 1);
243     let r = println!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
244     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);
245
246     let q = unreachable!("{}", 1);
247     let r = unreachable!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
248     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);
249
250     debug!("{}", 1);
251     debug!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
252     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);
253
254     error!("{}", 1);
255     error!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
256     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);
257
258     info!("{}", 1);
259     info!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
260     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);
261
262     panic!("{}", 1);
263     panic!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
264     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);
265
266     warn!("{}", 1);
267     warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
268     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);
269
270     assert!();
271     assert!(result == 42);
272     assert!(result == 42, "Ahoy there, {}!", target);
273     assert!(result == 42, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
274     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);
275
276     assert_eq!();
277     assert_eq!(left);
278     assert_eq!(left, right);
279     assert_eq!(left, right, "Ahoy there, {}!", target);
280     assert_eq!(left, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
281     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);
282     assert_eq!(left + 42, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
283     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);
284
285     write!(&mut s, "Ahoy there, {}!", target);
286     write!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
287     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);
288
289     writeln!(&mut s, "Ahoy there, {}!", target);
290     writeln!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
291     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);
292 }
293
294 // #1209
295 impl Foo {
296     /// foo
297     pub fn foo(&self) -> Bar<foo!(   )> {}
298 }
299
300 // #819
301 fn macro_in_pattern_position () {
302     let x = match y {
303         foo!(  ) => (),
304         bar!(            a, b,
305                          c) => (),
306         bar!(a
307              , b
308              , c
309              ,) => (),
310         baz!( 1 + 2 + 3, quux.kaas(  )
311         ) => (),
312         quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB) => (),
313     };
314 }
315
316 macro foo() {
317
318
319 }
320
321 pub macro bar($x:ident+$y:expr; ) {
322     fn foo($x: Foo) {
323     long_function(a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA),
324                   $x.bar($y));
325     }
326 }
327
328 macro foo() {
329   // a comment
330   fn foo() {
331   // another comment
332   bar();
333   }
334 }
335
336 macro lex_err($kind: ident $(, $body: expr)*) {
337     Err(QlError::LexError(LexError::$kind($($body,)*)))
338 }
339
340 // Preserve trailing comma on item-level macro with `()` or `[]`.
341 methods![ get, post, delete, ];
342 methods!( get, post, delete, );