Skip to content

Bad macro usage error message does not include correct error location #31022

Open
@SimonSapin

Description

@SimonSapin

Consider this reduced test case:

macro_rules! match_ignore_ascii_case {
    (@inner $value:expr) => { () };
    ( $($rest:tt)* ) => { match_ignore_ascii_case!(@inner $($rest)*) };
}

fn main() {
    // This is fine
    match_ignore_ascii_case!(1);

    // This causes an error as it doesn’t match the expected syntax
    // but the error message does not the location of the actual error.
    match_ignore_ascii_case!(2 => 3);
}

The @inner indirection exists because the non-reduced macro is recursive:

This fails to compile (as it should) but the error message does not include the real location of the error, which is line 12. It can be hard to track down in a large crate with many users of the macro.

a.rs:3:52: 3:53 error: unexpected token: `@`
a.rs:3     ( $($rest:tt)* ) => { match_ignore_ascii_case!(@inner $($rest)*) };

The error message looks even worse when the macro is used (with incorrect syntax) from another crate

<cssparser macros>:12:1: 12:2 error: unexpected token: `@`
<cssparser macros>:12 @ inner $ value , ( $ ( $ rest ) * ) -> (

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-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