Skip to content

Misleading diagnostic when passing a doc comment to macro #97850

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

Closed
zachwolfe opened this issue Jun 7, 2022 · 0 comments · Fixed by #98882
Closed

Misleading diagnostic when passing a doc comment to macro #97850

zachwolfe opened this issue Jun 7, 2022 · 0 comments · Fixed by #98882
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

@zachwolfe
Copy link

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=ef09a8b3babfcc78309404420421b477

macro_rules! foo {
    () => {};
}
foo!(
    ///
);

The current output is:

error: no rules expected the token `#`
 --> src/lib.rs:5:5
  |
1 | macro_rules! foo {
  | ---------------- when calling this macro
...
5 |     ///
  |     ^^^ no rules expected this token in macro call

error: could not compile `playground` due to previous error

Replacing the empty macro pattern with #, #[], #[doc] or #[doc=] will result in near-identical error messages about the [, doc, = or string literal tokens, respectively. This is misleading, because these tokens do not exist in the original source code. If I didn't already know that doc comments get turned into doc attributes in the form #[doc = "..."], I probably would have assumed this was a compiler bug. A possible solution would be to point this out in the diagnostic (formatting and contents very much up for debate):

error: no rules expected the token `#`
 --> src/lib.rs:5:5
  |
1 | macro_rules! foo {
  | ---------------- when calling this macro
...
5 |     ///
  |     ^^^ no rules expected `#` token in macro call
hint: `#` token appears because doc comments are transformed into doc attributes of the form `#[doc = "..."]`

error: could not compile `playground` due to previous error

This is related to (and possibly a generalization of) #92846.

@zachwolfe zachwolfe 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 Jun 7, 2022
@bors bors closed this as completed in 9431520 Jul 11, 2022
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

Successfully merging a pull request may close this issue.

1 participant