rust-lang / rust-lang/rust

Tracking Issue for `guard` macro fragment specifier

Open
#153,104 0 comments 2 reactions 1 assignee View on GitHub

@SpriteOvO is already working on this.

Since Feb 25, 2026.

A-macros B-experimental C-tracking-issue T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This is a tracking issue for the feature "guard macro fragment specifier" accepted in https://github.com/rust-lang/rust/pull/152832#issuecomment-3953476605.
The feature gate for the issue is #![feature(macro_guard_matcher)].

Introduce a new macro fragment specifier guard to match MatchArmGuard, to make it easier for macro authors to match the recently stabilized if_let_guard feature (#51114).

This resolves #152313, because we either introduce a new :guard matcher or must refactor matches! and assert_matches! into built-in macros. Considering that more users will benefit from :guard, we choose to do so.

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Discussion comments will get marked as off-topic or deleted.
Repeated discussions on the tracking issue may lead to the tracking issue getting locked.

Steps
Unresolved Questions
  • (fmease writing) Should { really be in the follow set? I'm not quite sure it should.
    • The condition of match guards can be an "unrestricted" expressions precisely because the guard can only be followed by => contrary to the conditions of if & while expressions and the iterator of for loops which are expressions without struct expressions.
    • So while if S {} successfully parses as an expression (namely as if (S) {}), macro matcher $g:guard {} currently rejects if S {} as input because it parses if (S {}) and looks for extra { } which aren't there of course.
Implementation history
  • #152832
    The initial attempt used :tt to match if-let guards, but :tt greedily captured the trailing comma and the assertion message. So we decide to introduce a new :guard matcher.
  • #153702
  • #155182
  • #155475
  • #156025

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.