rust-lang / rust-lang/rust

Feature request: Detect uses of $ in macro_rules! that could be mistyped metavariables

Open
#141,826 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
macro_rules! call_method {
    ($val: expr, $met: ident) => {
        $val.$meth()
    }
}

fn main() {
    let data = vec![10, 20];
    let length = call_method!(data, len);
}
Current output
Compiling playground v0.0.1 (/playground)
error: unexpected token: `$`
 --> src/main.rs:3:14
  |
3 |         $val.$meth()
  |              ^^^^^
...
9 |     let length = call_method!(data, len);
  |                  ----------------------- in this macro invocation
  |
  = note: this error originates in the macro `call_method` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores `$` and any tokens following
 --> src/main.rs:3:14
  |
3 |         $val.$meth()
  |              ^^^^^
...
9 |     let length = call_method!(data, len);
  |                  ----------------------- caused by the macro expansion here
  |
  = note: the usage of `call_method!` is likely invalid in expression context

warning: unused variable: `length`
 --> src/main.rs:9:9
  |
9 |     let length = call_method!(data, len);
  |         ^^^^^^ help: if this is intentional, prefix it with an underscore: `_length`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 2 previous errors; 1 warning emitted
Desired output
Compiling playground v0.0.1 (/playground)
error: unexpected token: `$`
 --> src/main.rs:3:14
  |
3 |         $val.$meth()
  |              ^^^^^ help: a metavariable with a similar name exists: `$met`
...
9 |     let length = call_method!(data, len);
  |                  ----------------------- in this macro invocation
  |
help: `$` in `macro_rules!` macros is treated as a literal `$` if the following identifier is not the name of a metavariable
  = note: this error originates in the macro `call_method` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores `$` and any tokens following
 --> src/main.rs:3:14
  |
3 |         $val.$meth()
  |              ^^^^^
...
9 |     let length = call_method!(data, len);
  |                  ----------------------- caused by the macro expansion here
  |
  = note: the usage of `call_method!` is likely invalid in expression context

warning: unused variable: `length`
 --> src/main.rs:9:9
  |
9 |     let length = call_method!(data, len);
  |         ^^^^^^ help: if this is intentional, prefix it with an underscore: `_length`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 2 previous errors; 1 warning emitted
Rationale and extra context

No response

Other cases

Rust Version
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024
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 by reproducing the provided macro_rules! example with the linked Rust version and inspect the compiler's unexpected-token diagnostic for the mistyped $meth metavariable. Done means the diagnostic suggests the similarly named $met metavariable and includes the requested explanatory help text without changing the other reported errors.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.