Skip to content

Commit 5fa97c3

Browse files
committed
add tests for macro trailing commas
1 parent ca7d839 commit 5fa97c3

File tree

5 files changed

+601
-0
lines changed

5 files changed

+601
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
// Companion test to the similarly-named file in run-pass.
12+
13+
// compile-flags: -C debug_assertions=yes
14+
// revisions: std core
15+
16+
#![cfg_attr(core, no_std)]
17+
18+
#[cfg(std)] use std::fmt;
19+
#[cfg(core)] use core::fmt;
20+
21+
// (see documentation of the similarly-named test in run-pass)
22+
fn to_format_or_not_to_format() {
23+
let falsum = || false;
24+
25+
// assert!(true, "{}",); // see run-pass
26+
27+
assert_eq!(1, 1, "{}",);
28+
//[core]~^ ERROR no arguments
29+
//[std]~^^ ERROR no arguments
30+
assert_ne!(1, 2, "{}",);
31+
//[core]~^ ERROR no arguments
32+
//[std]~^^ ERROR no arguments
33+
34+
// debug_assert!(true, "{}",); // see run-pass
35+
36+
debug_assert_eq!(1, 1, "{}",);
37+
//[core]~^ ERROR no arguments
38+
//[std]~^^ ERROR no arguments
39+
debug_assert_ne!(1, 2, "{}",);
40+
//[core]~^ ERROR no arguments
41+
//[std]~^^ ERROR no arguments
42+
43+
#[cfg(std)] {
44+
eprint!("{}",);
45+
//[std]~^ ERROR no arguments
46+
}
47+
48+
#[cfg(std)] {
49+
// FIXME: compile-fail says "expected error not found" even though
50+
// rustc does emit an error
51+
// eprintln!("{}",);
52+
// <DISABLED> [std]~^ ERROR no arguments
53+
}
54+
55+
#[cfg(std)] {
56+
format!("{}",);
57+
//[std]~^ ERROR no arguments
58+
}
59+
60+
format_args!("{}",);
61+
//[core]~^ ERROR no arguments
62+
//[std]~^^ ERROR no arguments
63+
64+
// if falsum() { panic!("{}",); } // see run-pass
65+
66+
#[cfg(std)] {
67+
print!("{}",);
68+
//[std]~^ ERROR no arguments
69+
}
70+
71+
#[cfg(std)] {
72+
// FIXME: compile-fail says "expected error not found" even though
73+
// rustc does emit an error
74+
// println!("{}",);
75+
// <DISABLED> [std]~^ ERROR no arguments
76+
}
77+
78+
unimplemented!("{}",);
79+
//[core]~^ ERROR no arguments
80+
//[std]~^^ ERROR no arguments
81+
82+
// if falsum() { unreachable!("{}",); } // see run-pass
83+
84+
struct S;
85+
impl fmt::Display for S {
86+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87+
write!(f, "{}",)?;
88+
//[core]~^ ERROR no arguments
89+
//[std]~^^ ERROR no arguments
90+
91+
// FIXME: compile-fail says "expected error not found" even though
92+
// rustc does emit an error
93+
// writeln!(f, "{}",)?;
94+
// <DISABLED> [core]~^ ERROR no arguments
95+
// <DISABLED> [std]~^^ ERROR no arguments
96+
Ok(())
97+
}
98+
}
99+
}
100+
101+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
// This is a companion to the similarly-named test in run-pass.
12+
//
13+
// It tests macros that unavoidably produce compile errors.
14+
15+
fn compile_error() {
16+
compile_error!("lel"); //~ ERROR lel
17+
compile_error!("lel",); //~ ERROR lel
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
()
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)