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