Skip to content

rustc shouldn't say 'not in scope' when using a derive macro as a macro_rules macro #76429

Closed

Description

@jyn514

Consider the following code (playground):

fn f() {
    Debug!()
}

#[derive(Debug)]
struct S;
error: cannot find macro `Debug` in this scope
 --> src/lib.rs:2:5
  |
2 |     Debug!()
  |     ^^^^^

This is not particularly helpful (in fact it's incorrect). Debug is in scope, it's just not a macro rules macro. Instead the error should say something like

error: `Debug` is a derive macro, not a macro_rules! macro
 --> src/lib.rs:2:5
  |
2 |     Debug!()
  |     ^^^^^ note: attempting to use a derive as a macro_rules! macro

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
    on Sep 7, 2020
  2. jyn514 commented on Sep 7, 2020

    @jyn514
    MemberAuthor

    This would have caught the second 'library error' in https://jyn514.github.io/2020/09/05/Rust-in-2021.html.

  3. petrochenkov commented on Sep 7, 2020

    @petrochenkov
    Contributor

    The desirable error was reported previously, but it changed with introduction of two sub-namespaces in macro namespace - #54069.

    So, derive Debug is never found during regular name lookup now for names in fn-like macro positions (Debug!).
    So it needs a second lookup for diagnostics, which is currently done for other namespaces, but not other sub-namespaces in the macro namespace.
    Compare with:

    type A = Debug; // ERROR: expected type, found derive macro `Debug`
  4. added
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    A-resolveArea: Name/path resolution done by `rustc_resolve` specifically
    on Mar 25, 2023
  5. dario23 commented on May 17, 2023

    @dario23
    Contributor

    Current playground correctly says

    error: cannot find macro `Debug` in this scope
     --> src/lib.rs:2:5
      |
    2 |     Debug!()
      |     ^^^^^
      |
      = note: `Debug` is in scope, but it is a derive macro: `#[derive(Debug)]`
    

    so let's close this? @jyn514

  6. jyn514 commented on May 17, 2023

    @jyn514
    MemberAuthor

    oh amazing, good find!

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, ..)A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.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