rust-lang / rust-lang/reference

mbe matchers can contain dollar ($)

Open
#1,229 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros
Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

Macro matchers allow $ as the last token of a token tree:

macro_rules! foo {
    ($) => {};
    (last$) => {};
    ({$}) => {};
    ([$]) => {};
    (($)) => {};
}

fn main() {
    foo!($);
    foo!(last$);
    foo!({$});
    foo!([$]);
    foo!(($));
}

This was changed in https://github.com/rust-lang/rust/pull/39419 in 1.17. That PR didn't mention this behavior change, so it is not clear if it was intentional. I believe this behavior is due to this line.

I see quite a lot of crates on crates.io relying on this behavior, so I don't think it is something that can be (easily) changed. I would lean towards just updating the documentation. Or perhaps it could be restricted to be a bare dollar within delimiters like ($) or [$] or {$} which is what almost all of the usages are (I didn't do an exhaustive check for the last$ style).

Initially the parts that could be updated are:

Generally I think the mbe documentation could use a lot of expansion to describe its behavior more precisely and completely, this is just a small part that could be improved.

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 src/macros-by-example.md, especially the MacroMatch rule and the sentence stating that $ cannot be matched or transcribed literally. Review the linked compiler behavior in rustc_expand/src/mbe/quoted.rs and the cited historical change. Done means the Reference accurately documents literal $ matching and transcription, including the supported token-tree cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.