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.
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.
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).
17 // std and core are both tested because they may contain separate
18 // implementations for some macro_rules! macros as an implementation
21 // ignore-pretty issue #37195
23 // compile-flags: --test -C debug_assertions=yes
24 // revisions: std core
26 #![cfg_attr(core, no_std)]
28 #![feature(concat_idents)]
30 #[cfg(std)] use std::fmt;
31 #[cfg(core)] use core::fmt;
37 assert!(true, "hello");
38 assert!(true, "hello",);
39 assert!(true, "hello {}", "world");
40 assert!(true, "hello {}", "world",);
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",);
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",);
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,),);
80 // compile_error! is in a companion to this test in compile-fail
85 let _ = concat!("hello");
86 let _ = concat!("hello",);
87 let _ = concat!("hello", " world");
88 let _ = concat!("hello", " world",);
96 concat_idents!(foo)();
97 concat_idents!(foo,)();
98 concat_idents!(foo, bar)();
99 concat_idents!(foo, bar,)();
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",);
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",);
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",);
134 let _ = env!("PATH");
135 let _ = env!("PATH",);
136 let _ = env!("PATH", "not found");
137 let _ = env!("PATH", "not found",);
145 eprint!("hello {}", "world");
146 eprint!("hello {}", "world",);
155 eprintln!("hello {}", "world");
156 eprintln!("hello {}", "world",);
167 let _ = format!("hello");
168 let _ = format!("hello",);
169 let _ = format!("hello {}", "world");
170 let _ = format!("hello {}", "world",);
175 let _ = format_args!("hello");
176 let _ = format_args!("hello",);
177 let _ = format_args!("hello {}", "world");
178 let _ = format_args!("hello {}", "world",);
183 let _ = include!("auxiliary/macro-comma-support.rs");
184 let _ = include!("auxiliary/macro-comma-support.rs",);
189 let _ = include_bytes!("auxiliary/macro-comma-support.rs");
190 let _ = include_bytes!("auxiliary/macro-comma-support.rs",);
195 let _ = include_str!("auxiliary/macro-comma-support.rs");
196 let _ = include_str!("auxiliary/macro-comma-support.rs",);
206 let _ = module_path!();
211 let _ = option_env!("PATH");
212 let _ = option_env!("PATH",);
217 // prevent 'unreachable code' warnings
218 let falsum = || false;
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",); }
232 print!("hello {}", "world");
233 print!("hello {}", "world",);
242 println!("hello {}", "world");
243 println!("hello {}", "world",);
246 // select! is too troublesome and unlikely to be stabilized
253 // this has an optional trailing *semicolon*
255 #[allow(unused)] pub static A: () = ()
259 #[allow(unused)] pub static AA: () = ();
263 #[allow(unused)] pub static AAA: () = ();
264 #[allow(unused)] pub static AAAA: () = ()
268 #[allow(unused)] pub static AAAAG: () = ();
269 #[allow(unused)] pub static AAAAGH: () = ();
275 fn inner() -> Result<(), ()> {
286 // prevent 'unreachable code' warnings
287 let falsum = || false;
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",); }
298 // prevent 'unreachable code' warnings
299 let falsum = || false;
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",); }
311 let _: Vec<()> = vec![];
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 {
323 fn $fname:ident($f:ident: &mut fmt::Formatter) $block:block
328 impl fmt::Display for Struct {
329 fn fmt(&self, $f: &mut fmt::Formatter) -> fmt::Result {
335 assert!(true, "{}", Struct);
340 test_with_formatter! {
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",);
350 test_with_formatter! {
352 fn writeln(f: &mut fmt::Formatter) {
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",);