Skip to content
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

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

Closed
jyn514 opened this issue Sep 7, 2020 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Sep 7, 2020

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
@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. A-proc-macros Area: Procedural macros labels Sep 7, 2020
@jyn514
Copy link
Member Author

jyn514 commented Sep 7, 2020

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

@jyn514 jyn514 removed the A-proc-macros Area: Procedural macros label Sep 7, 2020
@petrochenkov
Copy link
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`

@jyn514 jyn514 added E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. A-resolve Area: Name/path resolution done by `rustc_resolve` specifically labels Mar 25, 2023
@dario23
Copy link
Contributor

dario23 commented May 17, 2023

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

@jyn514
Copy link
Member Author

jyn514 commented May 17, 2023

oh amazing, good find!

@jyn514 jyn514 closed this as completed May 17, 2023
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 A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants