Skip to content

Commit 410d807

Browse files
committed
Auto merge of #39554 - zackmdavis:assert_eq_has_a_terrible_error_message_when_given_a_trailing_comma, r=BurntSushi
improve error message when two-arg assert_eq! receives a trailing comma Previously, `assert_eq!(left, right,)` (respectively, `assert_ne!(left, right,)`; note the trailing comma) would result in a confusing "requires at least a format string argument" error. In reality, a format string is optional, but the trailing comma puts us into the "match a token tree of zero or more tokens" branch of the macro (in order to support the optional format string), and passing the empty token tree into `format_args!` results in the confusing error. If instead we match a token tree of one or more tokens, we get a much more sensible "unexpected end of macro invocation" error. While we're here, fix up a stray space before a comma in the match guards. Resolves #39369. ----- **Before:** ``` $ rustc scratch.rs error: requires at least a format string argument --> scratch.rs:2:5 | 2 | assert_eq!(1, 2,); | ^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro outside of the current crate error: aborting due to previous error ``` **After:** ``` $ ./build/x86_64-unknown-linux-gnu/stage1/bin/rustc scratch.rs error: unexpected end of macro invocation --> scratch.rs:2:20 | 2 | assert_eq!(1, 2,); | ^ ```
2 parents 912bc14 + 65435e1 commit 410d807

5 files changed

+44
-6
lines changed

src/libcore/macros.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ macro_rules! assert {
103103
#[macro_export]
104104
#[stable(feature = "rust1", since = "1.0.0")]
105105
macro_rules! assert_eq {
106-
($left:expr , $right:expr) => ({
106+
($left:expr, $right:expr) => ({
107107
match (&$left, &$right) {
108108
(left_val, right_val) => {
109109
if !(*left_val == *right_val) {
@@ -113,13 +113,13 @@ macro_rules! assert_eq {
113113
}
114114
}
115115
});
116-
($left:expr , $right:expr, $($arg:tt)*) => ({
116+
($left:expr, $right:expr, $($arg:tt)+) => ({
117117
match (&($left), &($right)) {
118118
(left_val, right_val) => {
119119
if !(*left_val == *right_val) {
120120
panic!("assertion failed: `(left == right)` \
121121
(left: `{:?}`, right: `{:?}`): {}", left_val, right_val,
122-
format_args!($($arg)*))
122+
format_args!($($arg)+))
123123
}
124124
}
125125
}
@@ -146,7 +146,7 @@ macro_rules! assert_eq {
146146
#[macro_export]
147147
#[stable(feature = "assert_ne", since = "1.12.0")]
148148
macro_rules! assert_ne {
149-
($left:expr , $right:expr) => ({
149+
($left:expr, $right:expr) => ({
150150
match (&$left, &$right) {
151151
(left_val, right_val) => {
152152
if *left_val == *right_val {
@@ -156,13 +156,13 @@ macro_rules! assert_ne {
156156
}
157157
}
158158
});
159-
($left:expr , $right:expr, $($arg:tt)*) => ({
159+
($left:expr, $right:expr, $($arg:tt)+) => ({
160160
match (&($left), &($right)) {
161161
(left_val, right_val) => {
162162
if *left_val == *right_val {
163163
panic!("assertion failed: `(left != right)` \
164164
(left: `{:?}`, right: `{:?}`): {}", left_val, right_val,
165-
format_args!($($arg)*))
165+
format_args!($($arg)+))
166166
}
167167
}
168168
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 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+
fn main() {
12+
assert_eq!(1, 1,);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: unexpected end of macro invocation
2+
--> $DIR/assert_eq_trailing_comma.rs:12:20
3+
|
4+
12 | assert_eq!(1, 1,);
5+
| ^
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 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+
fn main() {
12+
assert_ne!(1, 2,);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: unexpected end of macro invocation
2+
--> $DIR/assert_ne_trailing_comma.rs:12:20
3+
|
4+
12 | assert_ne!(1, 2,);
5+
| ^
6+

0 commit comments

Comments
 (0)