rust-lang / rust-lang/rust-analyzer

Expanding macro gives wrong result when trying to "specialize" metavariables

Open
#12,603 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macro C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Consider the following macro:

macro_rules! what_number {
    ($num:literal) => {
        what_number!(@inner $num)
    };
    (@inner 42) => {
        "the answer to everything"
    };
    (@inner $num:literal) => {
        stringify!($num)
    };
}

fn main() {
    let result = what_number!(42);
    println!("{result}");
}

The compiler expands what_number!(42) to "42", but RA shows "the answer to everything".

See here for an explanation of why "42" should be the correct result.


rust-analyzer version: rust-analyzer version: 0.0.0 (427061da1 2022-06-19)

rustc version: rustc 1.63.0-nightly (ca122c7eb 2022-06-13)

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

Reproduce the macro example from the issue in rust-analyzer and compare its expansion with rustc, focusing on the recursive what_number! rules and metavariable matching. Done means rust-analyzer reports or displays the same result as rustc: "42" rather than "the answer to everything".

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.