Skip to content

Suggested fix for "cannot find macro ... in this scope" does not compile #136140

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

Open
rusty-snake opened this issue Jan 27, 2025 · 1 comment
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rusty-snake
Copy link
Contributor

rusty-snake commented Jan 27, 2025

I tried this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a095133990477eb48c9b42343e5593f4

fn main() {}

// STEP 1: Try to compile, will fail with "error: cannot find macro `nothing` in this scope".
// STEP 2: Uncomment line 18 as per suggestion.
// STEP 3: Try to compile, will fail with "macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths".
// STEP 4: Comment line 9.

mod a {
    #[rustfmt::skip]
    #[macro_export]
    macro_rules! nothing {
        () => {};
    }
}

mod b {
    fn foo() {
        //use crate::nothing;
        nothing!();
    }
}

I expected to see this happen: Using a suggestion "just works".

Instead, this happened: Unexpected and even more confusing error message caused because of #98291.

Meta

Tested on playground with 1.84.0 (stable) and 1.86.0-nightly (2025-01-26 f85c6de55206dbee5ffe).

@rusty-snake rusty-snake added the C-bug Category: This is a bug. label Jan 27, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 27, 2025
@lolbinarycat lolbinarycat added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 30, 2025
@lolbinarycat
Copy link
Contributor

tried to find a workaround and found an even more broken diagnostic instead

mod a {
    #[rustfmt::skip]
    #[macro_export]
    macro_rules! nothing {
        () => {};
    }
}

mod b {
    fn foo() {
        use crate::a::nothing;
        nothing!();
    }
}
error[E0432]: unresolved import `crate::a::nothing`
  --> src/lib.rs:11:13
   |
11 |         use crate::a::nothing;
   |             ^^^^^^^^^^^^^^^^^ no `nothing` in `a`
   |
   = note: this could be because a macro annotated with `#[macro_export]` will be exported at the root of the crate instead of the module where it is defined
help: a macro with this name exists at the root of the crate
   |
11 |         use ::nothing;
   |             ~~~~~~~~~

For more information about this error, try `rustc --explain E0432`.

@lolbinarycat lolbinarycat added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Jan 30, 2025
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 3, 2025
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

4 participants