rust-lang / rust-lang/rust

Declarative macros: warn on unreachable macro rule

Open
#130,222 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints A-macros C-feature-request T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#[macro_export]
macro_rules! example {
    ($lit:expr) => {{
        {}
    }};
    ($lit:literal) => {{
        unreachable!()
    }};
}

fn main() {
    example!(1)
}
Current output

Builds without any warnings, and the resulting binary runs without a panic.

Desired output

A warning telling me that the second macro rule is unreachable, because expr captures the same and more than literal.

Rationale and extra context

I think it's probably impossible to solve this for the general case, but I think it's a very helpful warning for users new to declarative macros.

Other cases

No response

Rust Version
rustc 1.83.0-nightly (0ee7cb5e3 2024-09-10)
binary: rustc
commit-hash: 0ee7cb5e3633502d9a90a85c3c367eccd59a0aba
commit-date: 2024-09-10
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
Anything else?

No response

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 macro_rules! example in the issue and investigate how declarative macro rules are analyzed for overlap and reachability. Determine whether the compiler can identify that an expr matcher subsumes a literal matcher, then validate the intended warning against the provided example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.