Skip to content

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

Closed

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 ) * ) -> (

Activity

  1. added a commit that references this issue on Jan 19, 2016
  2. durka commented on Jan 19, 2016

    @durka
    Contributor

    When the expanded code contains a non-macro-related error (say, a type mismatch), it at least shows a macro expansion backtrace. It's very hard to read, but gives spans at least. I guess that would be a start.

  3. durka commented on Jan 19, 2016

    @durka
    Contributor

    The trouble is you get the same error if you screw up writing the macro, e.g. misspell @inner or order something backwards in one of the arms. It isn't the caller's fault for sure.

  4. SimonSapin commented on Jan 20, 2016

    @SimonSapin
    ContributorAuthor

    Yes, I don’t have an example at hand to copy/past but I see the kind of error message you mean. It may be the caller’s fault even if it isn’t necessarily. A macro expansion backtrace would be useful here.

  5. removed
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 9, 2017
  6. estebank commented on Sep 21, 2017

    @estebank
    Contributor

    Current output:

    error: recursion limit reached while expanding the macro `match_ignore_ascii_case`
      --> src/main.rs:3:27
       |
    3  |     ( $($rest:tt)* ) => { match_ignore_ascii_case!(@inner $($rest)*) };
       |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...
    12 |     match_ignore_ascii_case!(2 => 3);
       |     --------------------------------- in this macro invocation
       |
       = help: consider adding a `#![recursion_limit="128"]` attribute to your crate
    
  7. estebank commented on May 22, 2019

    @estebank
    Contributor

    Triage: no change.

  8. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 11, 2020
  9. estebank commented on Jun 8, 2022

    @estebank
    Contributor

    Triage: no change.

  10. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    on Jun 8, 2022
  11. estebank commented on Dec 28, 2022

    @estebank
    Contributor

    Triage: no change.

  12. estebank commented on Aug 8, 2026

    @estebank
    Contributor

    @SimonSapin do you have any recollection of this ticket? I am tempted to close (we now do point at line 12 after all) and I'm not sure how we can improve it. Do you feel we should keep this ticket around?

  13. SimonSapin commented on Aug 8, 2026

    @SimonSapin
    ContributorAuthor

    I don’t remember this but based given that I wrote:

    the error message does not include the real location of the error, which is line 12

    … and that the new message does point to line 12, I think we can close this as fixed. Looks like we could even have closed in 2017, sorry I didn’t realize!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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