]> git.lizzy.rs Git - rust.git/blob - src/test/ui/run-pass/macros/macro-comma-support.rs
Partially revert 674a5db "Fix undesirable fallout [from macro modularization]"
[rust.git] / src / test / ui / run-pass / macros / macro-comma-support.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // run-pass
12 // This is meant to be a comprehensive test of invocations with/without
13 // trailing commas (or other, similar optionally-trailing separators).
14 // Every macro is accounted for, even those not tested in this file.
15 // (There will be a note indicating why).
16
17 // std and core are both tested because they may contain separate
18 // implementations for some macro_rules! macros as an implementation
19 // detail.
20
21 // ignore-pretty issue #37195
22
23 // compile-flags: --test -C debug_assertions=yes
24 // revisions: std core
25
26 #![cfg_attr(core, no_std)]
27
28 #![feature(concat_idents)]
29
30 #[cfg(std)] use std::fmt;
31 #[cfg(core)] use core::fmt;
32
33 #[test]
34 fn assert() {
35     assert!(true);
36     assert!(true,);
37     assert!(true, "hello");
38     assert!(true, "hello",);
39     assert!(true, "hello {}", "world");
40     assert!(true, "hello {}", "world",);
41 }
42
43 #[test]
44 fn assert_eq() {
45     assert_eq!(1, 1);
46     assert_eq!(1, 1,);
47     assert_eq!(1, 1, "hello");
48     assert_eq!(1, 1, "hello",);
49     assert_eq!(1, 1, "hello {}", "world");
50     assert_eq!(1, 1, "hello {}", "world",);
51 }
52
53 #[test]
54 fn assert_ne() {
55     assert_ne!(1, 2);
56     assert_ne!(1, 2,);
57     assert_ne!(1, 2, "hello");
58     assert_ne!(1, 2, "hello",);
59     assert_ne!(1, 2, "hello {}", "world");
60     assert_ne!(1, 2, "hello {}", "world",);
61 }
62
63 #[test]
64 fn cfg() {
65     let _ = cfg!(pants);
66     let _ = cfg!(pants,);
67     let _ = cfg!(pants = "pants");
68     let _ = cfg!(pants = "pants",);
69     let _ = cfg!(all(pants));
70     let _ = cfg!(all(pants),);
71     let _ = cfg!(all(pants,));
72     let _ = cfg!(all(pants,),);
73 }
74
75 #[test]
76 fn column() {
77     let _ = column!();
78 }
79
80 // compile_error! is in a companion to this test in compile-fail
81
82 #[test]
83 fn concat() {
84     let _ = concat!();
85     let _ = concat!("hello");
86     let _ = concat!("hello",);
87     let _ = concat!("hello", " world");
88     let _ = concat!("hello", " world",);
89 }
90
91 #[test]
92 fn concat_idents() {
93     fn foo() {}
94     fn foobar() {}
95
96     concat_idents!(foo)();
97     concat_idents!(foo,)();
98     concat_idents!(foo, bar)();
99     concat_idents!(foo, bar,)();
100 }
101
102 #[test]
103 fn debug_assert() {
104     debug_assert!(true);
105     debug_assert!(true, );
106     debug_assert!(true, "hello");
107     debug_assert!(true, "hello",);
108     debug_assert!(true, "hello {}", "world");
109     debug_assert!(true, "hello {}", "world",);
110 }
111
112 #[test]
113 fn debug_assert_eq() {
114     debug_assert_eq!(1, 1);
115     debug_assert_eq!(1, 1,);
116     debug_assert_eq!(1, 1, "hello");
117     debug_assert_eq!(1, 1, "hello",);
118     debug_assert_eq!(1, 1, "hello {}", "world");
119     debug_assert_eq!(1, 1, "hello {}", "world",);
120 }
121
122 #[test]
123 fn debug_assert_ne() {
124     debug_assert_ne!(1, 2);
125     debug_assert_ne!(1, 2,);
126     debug_assert_ne!(1, 2, "hello");
127     debug_assert_ne!(1, 2, "hello",);
128     debug_assert_ne!(1, 2, "hello {}", "world");
129     debug_assert_ne!(1, 2, "hello {}", "world",);
130 }
131
132 #[test]
133 fn env() {
134     let _ = env!("PATH");
135     let _ = env!("PATH",);
136     let _ = env!("PATH", "not found");
137     let _ = env!("PATH", "not found",);
138 }
139
140 #[cfg(std)]
141 #[test]
142 fn eprint() {
143     eprint!("hello");
144     eprint!("hello",);
145     eprint!("hello {}", "world");
146     eprint!("hello {}", "world",);
147 }
148
149 #[cfg(std)]
150 #[test]
151 fn eprintln() {
152     eprintln!();
153     eprintln!("hello");
154     eprintln!("hello",);
155     eprintln!("hello {}", "world");
156     eprintln!("hello {}", "world",);
157 }
158
159 #[test]
160 fn file() {
161     let _ = file!();
162 }
163
164 #[cfg(std)]
165 #[test]
166 fn format() {
167     let _ = format!("hello");
168     let _ = format!("hello",);
169     let _ = format!("hello {}", "world");
170     let _ = format!("hello {}", "world",);
171 }
172
173 #[test]
174 fn format_args() {
175     let _ = format_args!("hello");
176     let _ = format_args!("hello",);
177     let _ = format_args!("hello {}", "world");
178     let _ = format_args!("hello {}", "world",);
179 }
180
181 #[test]
182 fn include() {
183     let _ = include!("auxiliary/macro-comma-support.rs");
184     let _ = include!("auxiliary/macro-comma-support.rs",);
185 }
186
187 #[test]
188 fn include_bytes() {
189     let _ = include_bytes!("auxiliary/macro-comma-support.rs");
190     let _ = include_bytes!("auxiliary/macro-comma-support.rs",);
191 }
192
193 #[test]
194 fn include_str() {
195     let _ = include_str!("auxiliary/macro-comma-support.rs");
196     let _ = include_str!("auxiliary/macro-comma-support.rs",);
197 }
198
199 #[test]
200 fn line() {
201     let _ = line!();
202 }
203
204 #[test]
205 fn module_path() {
206     let _ = module_path!();
207 }
208
209 #[test]
210 fn option_env() {
211     let _ = option_env!("PATH");
212     let _ = option_env!("PATH",);
213 }
214
215 #[test]
216 fn panic() {
217     // prevent 'unreachable code' warnings
218     let falsum = || false;
219
220     if falsum() { panic!(); }
221     if falsum() { panic!("hello"); }
222     if falsum() { panic!("hello",); }
223     if falsum() { panic!("hello {}", "world"); }
224     if falsum() { panic!("hello {}", "world",); }
225 }
226
227 #[cfg(std)]
228 #[test]
229 fn print() {
230     print!("hello");
231     print!("hello",);
232     print!("hello {}", "world");
233     print!("hello {}", "world",);
234 }
235
236 #[cfg(std)]
237 #[test]
238 fn println() {
239     println!();
240     println!("hello");
241     println!("hello",);
242     println!("hello {}", "world");
243     println!("hello {}", "world",);
244 }
245
246 // select! is too troublesome and unlikely to be stabilized
247
248 // stringify! is N/A
249
250 #[cfg(std)]
251 #[test]
252 fn thread_local() {
253     // this has an optional trailing *semicolon*
254     thread_local! {
255         #[allow(unused)] pub static A: () = ()
256     }
257
258     thread_local! {
259         #[allow(unused)] pub static AA: () = ();
260     }
261
262     thread_local! {
263         #[allow(unused)] pub static AAA: () = ();
264         #[allow(unused)] pub static AAAA: () = ()
265     }
266
267     thread_local! {
268         #[allow(unused)] pub static AAAAG: () = ();
269         #[allow(unused)] pub static AAAAGH: () = ();
270     }
271 }
272
273 #[test]
274 fn try() {
275     fn inner() -> Result<(), ()> {
276         try!(Ok(()));
277         try!(Ok(()),);
278         Ok(())
279     }
280
281     inner().unwrap();
282 }
283
284 #[test]
285 fn unimplemented() {
286     // prevent 'unreachable code' warnings
287     let falsum = || false;
288
289     if falsum() { unimplemented!(); }
290     if falsum() { unimplemented!("hello"); }
291     if falsum() { unimplemented!("hello",); }
292     if falsum() { unimplemented!("hello {}", "world"); }
293     if falsum() { unimplemented!("hello {}", "world",); }
294 }
295
296 #[test]
297 fn unreachable() {
298     // prevent 'unreachable code' warnings
299     let falsum = || false;
300
301     if falsum() { unreachable!(); }
302     if falsum() { unreachable!("hello"); }
303     if falsum() { unreachable!("hello",); }
304     if falsum() { unreachable!("hello {}", "world"); }
305     if falsum() { unreachable!("hello {}", "world",); }
306 }
307
308 #[cfg(std)]
309 #[test]
310 fn vec() {
311     let _: Vec<()> = vec![];
312     let _ = vec![0];
313     let _ = vec![0,];
314     let _ = vec![0, 1];
315     let _ = vec![0, 1,];
316 }
317
318 // give a test body access to a fmt::Formatter, which seems
319 // to be the easiest way to use 'write!' on core.
320 macro_rules! test_with_formatter {
321     (
322         #[test]
323         fn $fname:ident($f:ident: &mut fmt::Formatter) $block:block
324     ) => {
325         #[test]
326         fn $fname() {
327             struct Struct;
328             impl fmt::Display for Struct {
329                 fn fmt(&self, $f: &mut fmt::Formatter) -> fmt::Result {
330                     Ok($block)
331                 }
332             }
333
334             // suppress "unused"
335             assert!(true, "{}", Struct);
336         }
337     };
338 }
339
340 test_with_formatter! {
341     #[test]
342     fn write(f: &mut fmt::Formatter) {
343         let _ = write!(f, "hello");
344         let _ = write!(f, "hello",);
345         let _ = write!(f, "hello {}", "world");
346         let _ = write!(f, "hello {}", "world",);
347     }
348 }
349
350 test_with_formatter! {
351     #[test]
352     fn writeln(f: &mut fmt::Formatter) {
353         let _ = writeln!(f);
354         let _ = writeln!(f,);
355         let _ = writeln!(f, "hello");
356         let _ = writeln!(f, "hello",);
357         let _ = writeln!(f, "hello {}", "world");
358         let _ = writeln!(f, "hello {}", "world",);
359     }
360 }