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

Error when failing to find macro should suggest pub(crate) #121773

Open
joshtriplett opened this issue Feb 29, 2024 · 4 comments
Open

Error when failing to find macro should suggest pub(crate) #121773

joshtriplett opened this issue Feb 29, 2024 · 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-visibility Area: Visibility / privacy D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@joshtriplett
Copy link
Member

joshtriplett commented Feb 29, 2024

Code

mod macros {
    macro_rules! greet {
        () => { println!("Hello, world!"); }
    }

    //pub(crate) use greet;
}

fn main() {
    macros::greet!();
}

Current output

error[E0433]: failed to resolve: could not find `greet` in `module`
  --> src/main.rs:10:13
   |
10 |     module::greet!();
   |             ^^^^^ could not find `greet` in `module`

warning: unused macro definition: `greet`
 --> src/main.rs:2:18
  |
2 |     macro_rules! greet {
  |                  ^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

Desired output

error[E0603]: macro `greet` is private
  --> src/main.rs:10:13
   |
10 |     module::greet!();
   |             ^^^^^ private macro
   |
note: the macro `greet` is defined here
  --> src/main.rs:2:5
  |
2 |     macro_rules! greet {
  |     ^^^^^^^^^^^^^^^^^^
  = help: consider adding `pub(crate) use greet;` to re-export the macro

(This could be an actual rustfix suggestion, though it should not have high certainty since they may not have intended to use a private macro.)

Rationale and extra context

We already give a better error for attempting to call a private function; this applies the same logic to private macros.

Rust Version

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6
@joshtriplett joshtriplett added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 29, 2024
@jieyouxu jieyouxu added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Feb 29, 2024
@ChAoSUnItY
Copy link
Contributor

@rustbot claim

@ChAoSUnItY
Copy link
Contributor

@rustbot release-assignment

@veera-sivarajan
Copy link
Contributor

@rustbot claim

@veera-sivarajan
Copy link
Contributor

Sorry, I don't understand macro scoping well enough to work on this.

@rustbot release-assignment

@fmease fmease added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. A-visibility Area: Visibility / privacy and removed A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Mar 20, 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-visibility Area: Visibility / privacy D-lack-of-suggestion Diagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic. 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

5 participants