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