Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert_eq! has a terrible error message when given a trailing comma #39369

Closed
cramertj opened this issue Jan 28, 2017 · 5 comments · Fixed by #39554
Closed

assert_eq! has a terrible error message when given a trailing comma #39369

cramertj opened this issue Jan 28, 2017 · 5 comments · Fixed by #39554

Comments

@cramertj
Copy link
Member

cramertj commented Jan 28, 2017

Trailing commas are an easy mistake to make, especially in a language like Rust where they are often accepted. Erroneously adding a trailing comma to assert_eq! triggers the error message "requires at least a format string argument", which is confusing and leads the user to think (incorrectly) that they need to add some kind of format string. Example.

@KalitaAlexey
Copy link
Contributor

I think we should add a hint to remove the last comma.

@abhijeetbhagat
Copy link
Contributor

er... aren't you supposed to provide one then? :)
assert_eq!(1, 2, "Nah, some other day maybe... {} {}", 1, 2,)

@cramertj
Copy link
Member Author

@abhijeetbhagat You certainly can do that, yes, but you don't have to.

@zackmdavis
Copy link
Member

This looks tricky. What's happening is that the comma puts us into the "match a 'token tree' of zero or more additional tokens after the $left and $right args" branch of assert_eq!, those tokens being passed to format_args!, which is what emits the "requires at least a format string argument" message.

What we would want is to only match one or more additional tokens (or emulate this by conditioning on the length of the token tree or something), but I don't immediately see how or whether this can be done with the macro system (my naïve hopes that + instead of * would work as in regular expressions seems to be unfounded).

@zackmdavis
Copy link
Member

my naïve hopes that + instead of * would work as in regular expressions seems to be unfounded

Wait, it looks like I was wrong about this!

zackmdavis added a commit to zackmdavis/rust that referenced this issue Feb 7, 2017
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 rust-lang#39369.
bors added a commit that referenced this issue Feb 12, 2017
…age_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,);
  |                    ^
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants