2 // This is meant to be a comprehensive test of invocations with/without
3 // trailing commas (or other, similar optionally-trailing separators).
4 // Every macro is accounted for, even those not tested in this file.
5 // (There will be a note indicating why).
7 // std and core are both tested because they may contain separate
8 // implementations for some macro_rules! macros as an implementation
11 // ignore-pretty issue #37195
13 // compile-flags: --test -C debug_assertions=yes
14 // revisions: std core
16 #![cfg_attr(core, no_std)]
18 #![allow(deprecated)] // for deprecated `try!()` macro
19 #![feature(concat_idents)]
21 #[cfg(std)] use std::fmt;
22 #[cfg(core)] use core::fmt;
28 assert!(true, "hello");
29 assert!(true, "hello",);
30 assert!(true, "hello {}", "world");
31 assert!(true, "hello {}", "world",);
38 assert_eq!(1, 1, "hello");
39 assert_eq!(1, 1, "hello",);
40 assert_eq!(1, 1, "hello {}", "world");
41 assert_eq!(1, 1, "hello {}", "world",);
48 assert_ne!(1, 2, "hello");
49 assert_ne!(1, 2, "hello",);
50 assert_ne!(1, 2, "hello {}", "world");
51 assert_ne!(1, 2, "hello {}", "world",);
58 let _ = cfg!(pants = "pants");
59 let _ = cfg!(pants = "pants",);
60 let _ = cfg!(all(pants));
61 let _ = cfg!(all(pants),);
62 let _ = cfg!(all(pants,));
63 let _ = cfg!(all(pants,),);
71 // compile_error! is in a check-fail companion to this test
76 let _ = concat!("hello");
77 let _ = concat!("hello",);
78 let _ = concat!("hello", " world");
79 let _ = concat!("hello", " world",);
87 concat_idents!(foo)();
88 concat_idents!(foo,)();
89 concat_idents!(foo, bar)();
90 concat_idents!(foo, bar,)();
96 debug_assert!(true, );
97 debug_assert!(true, "hello");
98 debug_assert!(true, "hello",);
99 debug_assert!(true, "hello {}", "world");
100 debug_assert!(true, "hello {}", "world",);
104 fn debug_assert_eq() {
105 debug_assert_eq!(1, 1);
106 debug_assert_eq!(1, 1,);
107 debug_assert_eq!(1, 1, "hello");
108 debug_assert_eq!(1, 1, "hello",);
109 debug_assert_eq!(1, 1, "hello {}", "world");
110 debug_assert_eq!(1, 1, "hello {}", "world",);
114 fn debug_assert_ne() {
115 debug_assert_ne!(1, 2);
116 debug_assert_ne!(1, 2,);
117 debug_assert_ne!(1, 2, "hello");
118 debug_assert_ne!(1, 2, "hello",);
119 debug_assert_ne!(1, 2, "hello {}", "world");
120 debug_assert_ne!(1, 2, "hello {}", "world",);
125 let _ = env!("PATH");
126 let _ = env!("PATH",);
127 let _ = env!("PATH", "not found");
128 let _ = env!("PATH", "not found",);
136 eprint!("hello {}", "world");
137 eprint!("hello {}", "world",);
146 eprintln!("hello {}", "world");
147 eprintln!("hello {}", "world",);
158 let _ = format!("hello");
159 let _ = format!("hello",);
160 let _ = format!("hello {}", "world");
161 let _ = format!("hello {}", "world",);
166 let _ = format_args!("hello");
167 let _ = format_args!("hello",);
168 let _ = format_args!("hello {}", "world");
169 let _ = format_args!("hello {}", "world",);
174 let _ = include!("auxiliary/macro-comma-support.rs");
175 let _ = include!("auxiliary/macro-comma-support.rs",);
180 let _ = include_bytes!("auxiliary/macro-comma-support.rs");
181 let _ = include_bytes!("auxiliary/macro-comma-support.rs",);
186 let _ = include_str!("auxiliary/macro-comma-support.rs");
187 let _ = include_str!("auxiliary/macro-comma-support.rs",);
197 let _ = module_path!();
202 let _ = option_env!("PATH");
203 let _ = option_env!("PATH",);
208 // prevent 'unreachable code' warnings
209 let falsum = || false;
211 if falsum() { panic!(); }
212 if falsum() { panic!("hello"); }
213 if falsum() { panic!("hello",); }
214 if falsum() { panic!("hello {}", "world"); }
215 if falsum() { panic!("hello {}", "world",); }
223 print!("hello {}", "world");
224 print!("hello {}", "world",);
233 println!("hello {}", "world");
234 println!("hello {}", "world",);
242 // this has an optional trailing *semicolon*
244 #[allow(unused)] pub static A: () = ()
248 #[allow(unused)] pub static AA: () = ();
252 #[allow(unused)] pub static AAA: () = ();
253 #[allow(unused)] pub static AAAA: () = ()
257 #[allow(unused)] pub static AAAAG: () = ();
258 #[allow(unused)] pub static AAAAGH: () = ();
264 fn inner() -> Result<(), ()> {
275 // prevent 'unreachable code' warnings
276 let falsum = || false;
278 if falsum() { unimplemented!(); }
279 if falsum() { unimplemented!("hello"); }
280 if falsum() { unimplemented!("hello",); }
281 if falsum() { unimplemented!("hello {}", "world"); }
282 if falsum() { unimplemented!("hello {}", "world",); }
287 // prevent 'unreachable code' warnings
288 let falsum = || false;
290 if falsum() { unreachable!(); }
291 if falsum() { unreachable!("hello"); }
292 if falsum() { unreachable!("hello",); }
293 if falsum() { unreachable!("hello {}", "world"); }
294 if falsum() { unreachable!("hello {}", "world",); }
300 let _: Vec<()> = vec![];
307 // give a test body access to a fmt::Formatter, which seems
308 // to be the easiest way to use 'write!' on core.
309 macro_rules! test_with_formatter {
312 fn $fname:ident($f:ident: &mut fmt::Formatter) $block:block
317 impl fmt::Display for Struct {
318 fn fmt(&self, $f: &mut fmt::Formatter) -> fmt::Result {
324 assert!(true, "{}", Struct);
329 test_with_formatter! {
331 fn write(f: &mut fmt::Formatter) {
332 let _ = write!(f, "hello");
333 let _ = write!(f, "hello",);
334 let _ = write!(f, "hello {}", "world");
335 let _ = write!(f, "hello {}", "world",);
339 test_with_formatter! {
341 fn writeln(f: &mut fmt::Formatter) {
343 let _ = writeln!(f,);
344 let _ = writeln!(f, "hello");
345 let _ = writeln!(f, "hello",);
346 let _ = writeln!(f, "hello {}", "world");
347 let _ = writeln!(f, "hello {}", "world",);