rust-lang / rust-lang/rust

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

Open
#121,773 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-macros A-visibility D-lack-of-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/main.rs and run it with rustc to compare the current and desired diagnostics. Trace how macro lookup handles the private greet definition and how private-function diagnostics are produced. Done means the example reports E0603, points to the definition, and suggests pub(crate) use greet;.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.