]> git.lizzy.rs Git - rust.git/blob - tests/ui/macros/macro-comma-behavior-rpass.rs
Rollup merge of #106714 - Ezrashaw:remove-e0490, r=davidtwco
[rust.git] / tests / 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 #![cfg_attr(core, no_std)]
18
19 #[cfg(core)]
20 use core::fmt;
21 #[cfg(std)]
22 use std::fmt;
23
24 // an easy mistake in the implementation of 'assert!'
25 // would cause this to say "explicit panic"
26 #[test]
27 #[should_panic(expected = "assertion failed")]
28 fn assert_1arg() {
29     assert!(false,);
30 }
31
32 // same as 'assert_1arg'
33 #[test]
34 #[should_panic(expected = "assertion failed")]
35 fn debug_assert_1arg() {
36     debug_assert!(false,);
37 }
38
39 // make sure we don't accidentally forward to `write!("text")`
40 #[cfg(std)]
41 #[test]
42 fn writeln_1arg() {
43     use fmt::Write;
44
45     let mut s = String::new();
46     writeln!(&mut s,).unwrap();
47     assert_eq!(&s, "\n");
48 }
49
50 // A number of format_args-like macros have special-case treatment
51 // for a single message string, which is not formatted.
52 //
53 // This test ensures that the addition of a trailing comma does not
54 // suddenly cause these strings to get formatted when they otherwise
55 // would not be. This is an easy mistake to make by having such a macro
56 // accept ", $($tok:tt)*" instead of ", $($tok:tt)+" after its minimal
57 // set of arguments.
58 //
59 // (Example: Issue #48042)
60 #[test]
61 #[allow(non_fmt_panics)]
62 fn to_format_or_not_to_format() {
63     // ("{}" is the easiest string to test because if this gets
64     // sent to format_args!, it'll simply fail to compile.
65     // "{{}}" is an example of an input that could compile and
66     // produce an incorrect program, but testing the panics
67     // would be burdensome.)
68     let falsum = || false;
69
70     assert!(true, "{}",);
71
72     // assert_eq!(1, 1, "{}",); // see check-fail
73     // assert_ne!(1, 2, "{}",); // see check-fail
74
75     debug_assert!(true, "{}",);
76
77     // debug_assert_eq!(1, 1, "{}",); // see check-fail
78     // debug_assert_ne!(1, 2, "{}",); // see check-fail
79     // eprint!("{}",); // see check-fail
80     // eprintln!("{}",); // see check-fail
81     // format!("{}",); // see check-fail
82     // format_args!("{}",); // see check-fail
83
84     if falsum() {
85         panic!("{}",);
86     }
87
88     // print!("{}",); // see check-fail
89     // println!("{}",); // see check-fail
90     // unimplemented!("{}",); // see check-fail
91
92     if falsum() {
93         unreachable!("{}",);
94     }
95
96     // write!(&mut stdout, "{}",); // see check-fail
97     // writeln!(&mut stdout, "{}",); // see check-fail
98 }