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

Non-found macro_rules! should suggest moving if they actually exist in the same file #121061

Closed
MultisampledNight opened this issue Feb 14, 2024 · 2 comments · Fixed by #121130
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@MultisampledNight
Copy link
Contributor

MultisampledNight commented Feb 14, 2024

Code

fn main() {
    something_later!();
}

macro_rules! something_later {
    () => {
        println!("successfully expanded!");
    };
}

Current output

error: cannot find macro `something_later` in this scope
 --> src/main.rs:2:5
  |
2 |     something_later!();
  |     ^^^^^^^^^^^^^^^
  |
  = help: have you added the `#[macro_use]` on the module/import?

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

Desired output

error: cannot find macro `something_later` in this scope
 --> src/main.rs:2:5
  |
2 |     something_later!();
  |     ^^^^^^^^^^^^^^^ help: a macro with the same name exists, but it appears later
  |                     help: consider moving the definition of
  |                           `something_later` before this call
  |
  = help: have you added the `#[macro_use]` on the module/import?

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

Rationale and extra context

Often times,

  • the actual error of the attempted call and
  • the warning of the unused macro

can be apart and/or soaked in other errors. Due to this, it might be desirable to couple a help message addressing this specific situation of a later definition.
This might also be useful advice for beginners who are just starting out with Rust and might not be aware of the horrors of declarative macros using macro_rules!, and as such could appreciate a helping hand by the compiler here.

Other cases

No response

Rust Version

= rustc --version --verbose
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

Anything else?

I tried to keep the suggested output close to the one for similarly named macros/variables, but that might not be applicable here and instead deserve its own section under the code excerpt. Open for bikeshedding. Like all of this, actually. Hmm.

(The message is the same on nightly b381d3a 2024-02-12.)

@MultisampledNight MultisampledNight 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 14, 2024
@jieyouxu
Copy link
Contributor

@rustbot label D-terse D-newcomer-roadblock

@rustbot rustbot added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Feb 14, 2024
@chenyukang chenyukang self-assigned this Feb 15, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 19, 2024
…-later, r=cjgillot

Suggest moving definition if non-found macro_rules! is defined later

Fixes rust-lang#121061
jhpratt added a commit to jhpratt/rust that referenced this issue Feb 29, 2024
…-later, r=matthiaskrgr

Suggest moving definition if non-found macro_rules! is defined later

Fixes rust-lang#121061
@bors bors closed this as completed in c620ae5 Mar 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 4, 2024
Rollup merge of rust-lang#121130 - chenyukang:yukang-fix-121061-macro-later, r=matthiaskrgr

Suggest moving definition if non-found macro_rules! is defined later

Fixes rust-lang#121061
@MultisampledNight
Copy link
Contributor Author

@chenyukang Thank you!!

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 D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants