Skip to content

Misleading diagnostic when passing a doc comment to macro #97850

Closed
@zachwolfe

Description

@zachwolfe

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions