|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Ideally, any macro call with a trailing comma should behave |
| 12 | +// identically to a call without the comma. |
| 13 | +// |
| 14 | +// This checks the behavior of macros with trailing commas in key |
| 15 | +// places where regressions in behavior seem highly possible (due |
| 16 | +// to it being e.g. a place where the addition of an argument |
| 17 | +// causes it to go down a code path with subtly different behavior). |
| 18 | +// |
| 19 | +// There is a companion test in compile-fail. |
| 20 | + |
| 21 | +// compile-flags: --test -C debug_assertions=yes |
| 22 | +// revisions: std core |
| 23 | + |
| 24 | +#![cfg_attr(core, no_std)] |
| 25 | + |
| 26 | +#[cfg(std)] use std::fmt; |
| 27 | +#[cfg(core)] use core::fmt; |
| 28 | + |
| 29 | +// an easy mistake in the implementation of 'assert!' |
| 30 | +// would cause this to say "explicit panic" |
| 31 | +#[test] |
| 32 | +#[should_panic(expected = "assertion failed")] |
| 33 | +fn assert_1arg() { |
| 34 | + assert!(false,); |
| 35 | +} |
| 36 | + |
| 37 | +// same as 'assert_1arg' |
| 38 | +#[test] |
| 39 | +#[should_panic(expected = "assertion failed")] |
| 40 | +fn debug_assert_1arg() { |
| 41 | + debug_assert!(false,); |
| 42 | +} |
| 43 | + |
| 44 | +// make sure we don't accidentally forward to `write!("text")` |
| 45 | +#[cfg(std)] |
| 46 | +#[test] |
| 47 | +fn writeln_2arg() { |
| 48 | + use fmt::Write; |
| 49 | + |
| 50 | + let mut s = String::new(); |
| 51 | + writeln!(&mut s, "hi",).unwrap(); |
| 52 | + assert_eq!(&s, "hi\n"); |
| 53 | +} |
| 54 | + |
| 55 | +// A number of format_args-like macros have special-case treatment |
| 56 | +// for a single message string, which is not formatted. |
| 57 | +// |
| 58 | +// This test ensures that the addition of a trailing comma does not |
| 59 | +// suddenly cause these strings to get formatted when they otherwise |
| 60 | +// would not be. This is an easy mistake to make by having such a macro |
| 61 | +// accept ", $($tok:tt)*" instead of ", $($tok:tt)+" after its minimal |
| 62 | +// set of arguments. |
| 63 | +// |
| 64 | +// (Example: Issue #48042) |
| 65 | +#[test] |
| 66 | +fn to_format_or_not_to_format() { |
| 67 | + // ("{}" is the easiest string to test because if this gets |
| 68 | + // sent to format_args!, it'll simply fail to compile. |
| 69 | + // "{{}}" is an example of an input that could compile and |
| 70 | + // produce an incorrect program, but testing the panics |
| 71 | + // would be burdensome.) |
| 72 | + let falsum = || false; |
| 73 | + |
| 74 | + assert!(true, "{}",); |
| 75 | + |
| 76 | + // assert_eq!(1, 1, "{}",); // see compile-fail |
| 77 | + // assert_ne!(1, 2, "{}",); // see compile-fail |
| 78 | + |
| 79 | + debug_assert!(true, "{}",); |
| 80 | + |
| 81 | + // debug_assert_eq!(1, 1, "{}",); // see compile-fail |
| 82 | + // debug_assert_ne!(1, 2, "{}",); // see compile-fail |
| 83 | + // eprint!("{}",); // see compile-fail |
| 84 | + // eprintln!("{}",); // see compile-fail |
| 85 | + // format!("{}",); // see compile-fail |
| 86 | + // format_args!("{}",); // see compile-fail |
| 87 | + |
| 88 | + if falsum() { panic!("{}",); } |
| 89 | + |
| 90 | + // print!("{}",); // see compile-fail |
| 91 | + // println!("{}",); // see compile-fail |
| 92 | + // unimplemented!("{}",); // see compile-fail |
| 93 | + |
| 94 | + if falsum() { unreachable!("{}",); } |
| 95 | + |
| 96 | + // write!(&mut stdout, "{}",); // see compile-fail |
| 97 | + // writeln!(&mut stdout, "{}",); // see compile-fail |
| 98 | +} |
0 commit comments