Skip to content

"Unexpected end of macro invocation" error in nested macro has misleading span, doesn't specify which macro #33871

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
mbrubeck opened this issue May 25, 2016 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mbrubeck
Copy link
Contributor

The following code (based on a test case by @apoelstra):

macro_rules! check_enum {
    ($e:ident, $($var:ident),*) => ({
        $(assert_eq!(0 == $e::$var);)*
    })
}

fn main() {
    check_enum!(Foo, Bar, Baz);
}

generates this error message:

error: unexpected end of macro invocation
 --> test.rs:9:25
9 |>     check_enum!(Foo, Bar, Baz);
  |>                         ^

The problem is that assert_eq! is expecting a comma. This is very hard to decipher though, because the span points to the outer macro invocation. (This may be related to #26615.) If a better span can't be found, it would be useful to at least include the name of the macro in the error message, e.g. "unepected end of assert_eq! invocation."

@mbrubeck mbrubeck changed the title "Unexpected end of macro invokation" error in nested macro has misleading span, doesn't specify which macro "Unexpected end of macro invocation" error in nested macro has misleading span, doesn't specify which macro May 25, 2016
@Mark-Simulacrum
Copy link
Member

We now generate this error, which to me is still largely incomprehensible. This is better, though; @mbrubeck Could you provide the "ideal" error message so we can either implement that or as close as possible?

error: unexpected end of macro invocation
 --> ./test.rs:3:31
  |
3 |         $(assert_eq!(0 == $e::$var);)*
  |                               ^^^^
...
8 |     check_enum!(Foo, Bar, Baz);
  |     --------------------------- in this macro invocation


@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Jun 22, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@konstin
Copy link

konstin commented Mar 28, 2018

Not mbrubeck, but imho the ideal message would expected "," not found with the point where the comma should be highlighted. When I stumbled across this with another macro, the "unexpected end" part made me think I had mismatching brackets.

@estebank
Copy link
Contributor

Current output:

error: expected identifier, found `,`
 --> src/main.rs:3:31
  |
3 |         $(assert_eq!(0 == $e::$var);)*
  |                               ^
  |                               |
  |                               expected identifier
  |                               help: remove this comma
...
8 |     check_enum!(Foo, Bar, Baz);
  |     --------------------------- in this macro invocation

@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

error: unexpected end of macro invocation
 --> src/main.rs:3:35
  |
3 |         $(assert_eq!(0 == $e::$var);)*
  |                                   ^ missing tokens in macro arguments
  |
note: while trying to match `,`
 --> /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/macros/mod.rs:37:16

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 A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

5 participants