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 _ = matches!(1, x if x > 0);
198 let _ = matches!(1, x if x > 0,);
203 let _ = module_path!();
208 let _ = option_env!("PATH");
209 let _ = option_env!("PATH",);
214 // prevent 'unreachable code' warnings
215 let falsum = || false;
217 if falsum() { panic!(); }
218 if falsum() { panic!("hello"); }
219 if falsum() { panic!("hello",); }
220 if falsum() { panic!("hello {}", "world"); }
221 if falsum() { panic!("hello {}", "world",); }
229 print!("hello {}", "world");
230 print!("hello {}", "world",);
239 println!("hello {}", "world");
240 println!("hello {}", "world",);
248 // this has an optional trailing *semicolon*
250 #[allow(unused)] pub static A: () = ()
254 #[allow(unused)] pub static AA: () = ();
258 #[allow(unused)] pub static AAA: () = ();
259 #[allow(unused)] pub static AAAA: () = ()
263 #[allow(unused)] pub static AAAAG: () = ();
264 #[allow(unused)] pub static AAAAGH: () = ();
270 fn inner() -> Result<(), ()> {
281 // prevent 'unreachable code' warnings
282 let falsum = || false;
284 if falsum() { unimplemented!(); }
285 if falsum() { unimplemented!("hello"); }
286 if falsum() { unimplemented!("hello",); }
287 if falsum() { unimplemented!("hello {}", "world"); }
288 if falsum() { unimplemented!("hello {}", "world",); }
293 // prevent 'unreachable code' warnings
294 let falsum = || false;
296 if falsum() { unreachable!(); }
297 if falsum() { unreachable!("hello"); }
298 if falsum() { unreachable!("hello",); }
299 if falsum() { unreachable!("hello {}", "world"); }
300 if falsum() { unreachable!("hello {}", "world",); }
306 let _: Vec<()> = vec![];
313 // give a test body access to a fmt::Formatter, which seems
314 // to be the easiest way to use 'write!' on core.
315 macro_rules! test_with_formatter {
318 fn $fname:ident($f:ident: &mut fmt::Formatter) $block:block
323 impl fmt::Display for Struct {
324 fn fmt(&self, $f: &mut fmt::Formatter) -> fmt::Result {
330 assert!(true, "{}", Struct);
335 test_with_formatter! {
337 fn write(f: &mut fmt::Formatter) {
338 let _ = write!(f, "hello");
339 let _ = write!(f, "hello",);
340 let _ = write!(f, "hello {}", "world");
341 let _ = write!(f, "hello {}", "world",);
345 test_with_formatter! {
347 fn writeln(f: &mut fmt::Formatter) {
349 let _ = writeln!(f,);
350 let _ = writeln!(f, "hello");
351 let _ = writeln!(f, "hello",);
352 let _ = writeln!(f, "hello {}", "world");
353 let _ = writeln!(f, "hello {}", "world",);