Skip to content

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

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

Open
schuelermine opened this issue May 31, 2025 · 0 comments
Labels
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.

Comments

@schuelermine
Copy link
Contributor

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

@schuelermine schuelermine 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 May 31, 2025
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant