]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/macros/macro-comma-behavior.rs
New upstream version 1.37.0+dfsg1
[rustc.git] / src / test / run-pass / macros / macro-comma-behavior.rs
CommitLineData
b7449926 1// run-pass
0bf4aa26 2#![allow(unused_imports)]
0531ce1d
XL
3// Ideally, any macro call with a trailing comma should behave
4// identically to a call without the comma.
5//
6// This checks the behavior of macros with trailing commas in key
7// places where regressions in behavior seem highly possible (due
0731742a 8// to it being e.g., a place where the addition of an argument
0531ce1d
XL
9// causes it to go down a code path with subtly different behavior).
10//
11// There is a companion test in compile-fail.
12
13// compile-flags: --test -C debug_assertions=yes
14// revisions: std core
15
16#![cfg_attr(core, no_std)]
17
18#[cfg(std)] use std::fmt;
19#[cfg(core)] use core::fmt;
20
21// an easy mistake in the implementation of 'assert!'
22// would cause this to say "explicit panic"
23#[test]
24#[should_panic(expected = "assertion failed")]
25fn assert_1arg() {
26 assert!(false,);
27}
28
29// same as 'assert_1arg'
30#[test]
31#[should_panic(expected = "assertion failed")]
32fn debug_assert_1arg() {
33 debug_assert!(false,);
34}
35
36// make sure we don't accidentally forward to `write!("text")`
37#[cfg(std)]
38#[test]
39fn writeln_1arg() {
40 use fmt::Write;
41
42 let mut s = String::new();
43 writeln!(&mut s,).unwrap();
44 assert_eq!(&s, "\n");
45}
46
47// A number of format_args-like macros have special-case treatment
48// for a single message string, which is not formatted.
49//
50// This test ensures that the addition of a trailing comma does not
51// suddenly cause these strings to get formatted when they otherwise
52// would not be. This is an easy mistake to make by having such a macro
53// accept ", $($tok:tt)*" instead of ", $($tok:tt)+" after its minimal
54// set of arguments.
55//
56// (Example: Issue #48042)
57#[test]
58fn to_format_or_not_to_format() {
59 // ("{}" is the easiest string to test because if this gets
60 // sent to format_args!, it'll simply fail to compile.
61 // "{{}}" is an example of an input that could compile and
62 // produce an incorrect program, but testing the panics
63 // would be burdensome.)
64 let falsum = || false;
65
66 assert!(true, "{}",);
67
68 // assert_eq!(1, 1, "{}",); // see compile-fail
69 // assert_ne!(1, 2, "{}",); // see compile-fail
70
71 debug_assert!(true, "{}",);
72
73 // debug_assert_eq!(1, 1, "{}",); // see compile-fail
74 // debug_assert_ne!(1, 2, "{}",); // see compile-fail
75 // eprint!("{}",); // see compile-fail
76 // eprintln!("{}",); // see compile-fail
77 // format!("{}",); // see compile-fail
78 // format_args!("{}",); // see compile-fail
79
80 if falsum() { panic!("{}",); }
81
82 // print!("{}",); // see compile-fail
83 // println!("{}",); // see compile-fail
84 // unimplemented!("{}",); // see compile-fail
85
86 if falsum() { unreachable!("{}",); }
87
88 // write!(&mut stdout, "{}",); // see compile-fail
89 // writeln!(&mut stdout, "{}",); // see compile-fail
90}