Skip to content

Add macro matcher for guard fragment specifier#153702

Open
SpriteOvO wants to merge 1 commit intorust-lang:mainfrom
SpriteOvO:guard-matcher
Open

Add macro matcher for guard fragment specifier#153702
SpriteOvO wants to merge 1 commit intorust-lang:mainfrom
SpriteOvO:guard-matcher

Conversation

@SpriteOvO
Copy link
Member

Tracking issue #153104

This PR implements a new guard macro matcher to match if-let guards (specifically MatchArmGuard). In the upcoming PR, we can use this new matcher in the matches! and assert_matches! macros to support their use with if-let guards. (see #152313)

The original Expr used to represent a guard has been wrapped in a new Guard type, allowing us to carry the span information of the leading if keyword. However, it might be even better to include the if keyword in the Guard type as well? I've left a FIXME comment in the code.

@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 2026

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 11, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 2026

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

@SpriteOvO SpriteOvO added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-lang Relevant to the language team labels Mar 11, 2026
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 2026

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Mar 11, 2026
@rust-log-analyzer
Copy link
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
    |
301 |     let guard_str = rewrite_guard(
    |                     ------------- arguments to this function are incorrect
302 |         context,
303 |         &arm.guard,
    |         ^^^^^^^^^^ expected `&Option<Box<Expr>>`, found `&Option<Box<Guard>>`
    |
    = note: expected reference `&std::option::Option<std::boxed::Box<rustc_ast::Expr>>`
               found reference `&std::option::Option<std::boxed::Box<rustc_ast::Guard>>`
note: function defined here
   --> src/tools/rustfmt/src/matches.rs:572:4
    |
572 | fn rewrite_guard(
    |    ^^^^^^^^^^^^^
573 |     context: &RewriteContext<'_>,
574 |     guard: &Option<Box<ast::Expr>>,
    |     ------------------------------

For more information about this error, try `rustc --explain E0308`.
[RUSTC-TIMING] rustfmt_nightly test:false 2.282
error: could not compile `rustfmt-nightly` (lib) due to 1 previous error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants