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 #![feature(concat_idents)]
20 #[cfg(std)] use std::fmt;
21 #[cfg(core)] use core::fmt;
27 assert!(true, "hello");
28 assert!(true, "hello",);
29 assert!(true, "hello {}", "world");
30 assert!(true, "hello {}", "world",);
37 assert_eq!(1, 1, "hello");
38 assert_eq!(1, 1, "hello",);
39 assert_eq!(1, 1, "hello {}", "world");
40 assert_eq!(1, 1, "hello {}", "world",);
47 assert_ne!(1, 2, "hello");
48 assert_ne!(1, 2, "hello",);
49 assert_ne!(1, 2, "hello {}", "world");
50 assert_ne!(1, 2, "hello {}", "world",);
57 let _ = cfg!(pants = "pants");
58 let _ = cfg!(pants = "pants",);
59 let _ = cfg!(all(pants));
60 let _ = cfg!(all(pants),);
61 let _ = cfg!(all(pants,));
62 let _ = cfg!(all(pants,),);
70 // compile_error! is in a companion to this test in compile-fail
75 let _ = concat!("hello");
76 let _ = concat!("hello",);
77 let _ = concat!("hello", " world");
78 let _ = concat!("hello", " world",);
86 concat_idents!(foo)();
87 concat_idents!(foo,)();
88 concat_idents!(foo, bar)();
89 concat_idents!(foo, bar,)();
95 debug_assert!(true, );
96 debug_assert!(true, "hello");
97 debug_assert!(true, "hello",);
98 debug_assert!(true, "hello {}", "world");
99 debug_assert!(true, "hello {}", "world",);
103 fn debug_assert_eq() {
104 debug_assert_eq!(1, 1);
105 debug_assert_eq!(1, 1,);
106 debug_assert_eq!(1, 1, "hello");
107 debug_assert_eq!(1, 1, "hello",);
108 debug_assert_eq!(1, 1, "hello {}", "world");
109 debug_assert_eq!(1, 1, "hello {}", "world",);
113 fn debug_assert_ne() {
114 debug_assert_ne!(1, 2);
115 debug_assert_ne!(1, 2,);
116 debug_assert_ne!(1, 2, "hello");
117 debug_assert_ne!(1, 2, "hello",);
118 debug_assert_ne!(1, 2, "hello {}", "world");
119 debug_assert_ne!(1, 2, "hello {}", "world",);
124 let _ = env!("PATH");
125 let _ = env!("PATH",);
126 let _ = env!("PATH", "not found");
127 let _ = env!("PATH", "not found",);
135 eprint!("hello {}", "world");
136 eprint!("hello {}", "world",);
145 eprintln!("hello {}", "world");
146 eprintln!("hello {}", "world",);
157 let _ = format!("hello");
158 let _ = format!("hello",);
159 let _ = format!("hello {}", "world");
160 let _ = format!("hello {}", "world",);
165 let _ = format_args!("hello");
166 let _ = format_args!("hello",);
167 let _ = format_args!("hello {}", "world");
168 let _ = format_args!("hello {}", "world",);
173 let _ = include!("auxiliary/macro-comma-support.rs");
174 let _ = include!("auxiliary/macro-comma-support.rs",);
179 let _ = include_bytes!("auxiliary/macro-comma-support.rs");
180 let _ = include_bytes!("auxiliary/macro-comma-support.rs",);
185 let _ = include_str!("auxiliary/macro-comma-support.rs");
186 let _ = include_str!("auxiliary/macro-comma-support.rs",);
196 let _ = module_path!();
201 let _ = option_env!("PATH");
202 let _ = option_env!("PATH",);
207 // prevent 'unreachable code' warnings
208 let falsum = || false;
210 if falsum() { panic!(); }
211 if falsum() { panic!("hello"); }
212 if falsum() { panic!("hello",); }
213 if falsum() { panic!("hello {}", "world"); }
214 if falsum() { panic!("hello {}", "world",); }
222 print!("hello {}", "world");
223 print!("hello {}", "world",);
232 println!("hello {}", "world");
233 println!("hello {}", "world",);
241 // this has an optional trailing *semicolon*
243 #[allow(unused)] pub static A: () = ()
247 #[allow(unused)] pub static AA: () = ();
251 #[allow(unused)] pub static AAA: () = ();
252 #[allow(unused)] pub static AAAA: () = ()
256 #[allow(unused)] pub static AAAAG: () = ();
257 #[allow(unused)] pub static AAAAGH: () = ();
263 fn inner() -> Result<(), ()> {
274 // prevent 'unreachable code' warnings
275 let falsum = || false;
277 if falsum() { unimplemented!(); }
278 if falsum() { unimplemented!("hello"); }
279 if falsum() { unimplemented!("hello",); }
280 if falsum() { unimplemented!("hello {}", "world"); }
281 if falsum() { unimplemented!("hello {}", "world",); }
286 // prevent 'unreachable code' warnings
287 let falsum = || false;
289 if falsum() { unreachable!(); }
290 if falsum() { unreachable!("hello"); }
291 if falsum() { unreachable!("hello",); }
292 if falsum() { unreachable!("hello {}", "world"); }
293 if falsum() { unreachable!("hello {}", "world",); }
299 let _: Vec<()> = vec![];
306 // give a test body access to a fmt::Formatter, which seems
307 // to be the easiest way to use 'write!' on core.
308 macro_rules! test_with_formatter {
311 fn $fname:ident($f:ident: &mut fmt::Formatter) $block:block
316 impl fmt::Display for Struct {
317 fn fmt(&self, $f: &mut fmt::Formatter) -> fmt::Result {
323 assert!(true, "{}", Struct);
328 test_with_formatter! {
330 fn write(f: &mut fmt::Formatter) {
331 let _ = write!(f, "hello");
332 let _ = write!(f, "hello",);
333 let _ = write!(f, "hello {}", "world");
334 let _ = write!(f, "hello {}", "world",);
338 test_with_formatter! {
340 fn writeln(f: &mut fmt::Formatter) {
342 let _ = writeln!(f,);
343 let _ = writeln!(f, "hello");
344 let _ = writeln!(f, "hello",);
345 let _ = writeln!(f, "hello {}", "world");
346 let _ = writeln!(f, "hello {}", "world",);