Skip to content

Terse diagnostic "unexpected end of macro invocation" when misusing assert_eq! #103238

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

Open
jruderman opened this issue Oct 19, 2022 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jruderman
Copy link
Contributor

jruderman commented Oct 19, 2022

Given the following code (playground):

fn main() {
    assert_eq!(1 + 2 == 3);
}

Current output: terse

error: unexpected end of macro invocation
 --> src/main.rs:2:26
  |
2 |     assert_eq!(1 + 2 == 3);
  |                          ^ missing tokens in macro arguments

Better output: show macro correspondence

note:          This expression was treated as the $left argument to assert_eq!
  |
2 |     assert_eq!(1 + 2 == 3);
  |                ^^^^^^^^^^

note:          Following $left, assert_eq! expects ","

(See also #97108, which also asks for more verbose "missing tokens in macro arguments" errors.)

Ideal output: fix my mistake

help: To assert equality, separate the two expressions with a comma:
  |
2 |     assert_eq!(1 + 2, 3);
  |                     ^

(See also #57507, which asks for specific diagnostics when making a similar mistake with the assert! macro.)

@jruderman jruderman added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 19, 2022
@lyming2007
Copy link

@rustbot claim

@lyming2007
Copy link

I'm afraid we can't suggest some thing like:

help: To assert equality, separate the two expressions with a comma:
  |
2 |     assert_eq!(1 + 2, 3);
  |                     ^

Because 1 + 2 == 3 is a legal expression for RUST so we don't know if the programmer really wanted to write code like:
assert_eq!(1+2 == 3, true); or assert_eq!(1+2 , 3)

@lyming2007 lyming2007 removed their assignment Oct 19, 2022
@jruderman
Copy link
Contributor Author

I think it would be okay to suggest the more likely intended code.

Otoh, Clippy has disabled lints suggesting to replace assert with assert_eq in anticipation of Rust changes. Perhaps it would be better to hold off for now on suggestions that commit to preferring a particular assert syntax.

@jruderman
Copy link
Contributor Author

#103439 will improve diagnostics for macro use by showing what token or ast node was expected :)

@Noratrieb
Copy link
Member

With that PR, the new error is

error: unexpected end of macro invocation
 --> aaa.rs:2:26
  |
2 |     assert_eq!(1 + 2 == 3);
  |                          ^ missing tokens in macro arguments
  |
note: while trying to match `,`

error: aborting due to previous error

which is better but still not great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants