rust/tests/ui/macros/macro-comma-behavior-rpass.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
2.7 KiB
Rust
Raw Normal View History

//@ run-pass
//@ needs-unwind
#![allow(unused_imports)]
2018-02-07 04:03:14 +00:00
// Ideally, any macro call with a trailing comma should behave
// identically to a call without the comma.
//
// This checks the behavior of macros with trailing commas in key
// places where regressions in behavior seem highly possible (due
// to it being e.g., a place where the addition of an argument
2018-02-07 04:03:14 +00:00
// causes it to go down a code path with subtly different behavior).
//
2020-12-28 17:15:16 +00:00
// There is a companion failing test.
2018-02-07 04:03:14 +00:00
//@ compile-flags: --test -C debug_assertions=yes
//@ revisions: std core
#![cfg_attr(core, no_std)]
2021-06-29 18:33:31 +00:00
#[cfg(core)]
use core::fmt;
#[cfg(std)]
use std::fmt;
2018-02-07 04:03:14 +00:00
// an easy mistake in the implementation of 'assert!'
// would cause this to say "explicit panic"
#[test]
#[should_panic(expected = "assertion failed")]
fn assert_1arg() {
assert!(false,);
}
// same as 'assert_1arg'
#[test]
#[should_panic(expected = "assertion failed")]
fn debug_assert_1arg() {
debug_assert!(false,);
}
// make sure we don't accidentally forward to `write!("text")`
#[cfg(std)]
#[test]
2018-02-14 07:15:27 +00:00
fn writeln_1arg() {
2018-02-07 04:03:14 +00:00
use fmt::Write;
let mut s = String::new();
2018-02-14 07:15:27 +00:00
writeln!(&mut s,).unwrap();
assert_eq!(&s, "\n");
2018-02-07 04:03:14 +00:00
}
// A number of format_args-like macros have special-case treatment
// for a single message string, which is not formatted.
//
// This test ensures that the addition of a trailing comma does not
// suddenly cause these strings to get formatted when they otherwise
// would not be. This is an easy mistake to make by having such a macro
// accept ", $($tok:tt)*" instead of ", $($tok:tt)+" after its minimal
// set of arguments.
//
// (Example: Issue #48042)
#[test]
2021-06-29 18:33:31 +00:00
#[allow(non_fmt_panics)]
2018-02-07 04:03:14 +00:00
fn to_format_or_not_to_format() {
// ("{}" is the easiest string to test because if this gets
// sent to format_args!, it'll simply fail to compile.
// "{{}}" is an example of an input that could compile and
// produce an incorrect program, but testing the panics
// would be burdensome.)
let falsum = || false;
assert!(true, "{}",);
2020-12-28 17:15:16 +00:00
// assert_eq!(1, 1, "{}",); // see check-fail
// assert_ne!(1, 2, "{}",); // see check-fail
2018-02-07 04:03:14 +00:00
debug_assert!(true, "{}",);
2020-12-28 17:15:16 +00:00
// debug_assert_eq!(1, 1, "{}",); // see check-fail
// debug_assert_ne!(1, 2, "{}",); // see check-fail
// eprint!("{}",); // see check-fail
// eprintln!("{}",); // see check-fail
// format!("{}",); // see check-fail
// format_args!("{}",); // see check-fail
2018-02-07 04:03:14 +00:00
2021-06-29 18:33:31 +00:00
if falsum() {
panic!("{}",);
}
2018-02-07 04:03:14 +00:00
2020-12-28 17:15:16 +00:00
// print!("{}",); // see check-fail
// println!("{}",); // see check-fail
// unimplemented!("{}",); // see check-fail
2018-02-07 04:03:14 +00:00
2021-06-29 18:33:31 +00:00
if falsum() {
unreachable!("{}",);
}
2018-02-07 04:03:14 +00:00
2020-12-28 17:15:16 +00:00
// write!(&mut stdout, "{}",); // see check-fail
// writeln!(&mut stdout, "{}",); // see check-fail
2018-02-07 04:03:14 +00:00
}