Skip to content

Under guard_patterns allow parenthesized guarded patterns in match arms - #164465

Open
fmease wants to merge 1 commit into
rust-lang:mainfrom
fmease:allow-paren-guarded-pat-in-match-arm
Open

fmease wants to merge 1 commit into
rust-lang:mainfrom
fmease:allow-paren-guarded-pat-in-match-arm

Conversation

@fmease

@fmease fmease commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

This PR renders match $expr { ($pat if $expr) => $expr } syntactically legal under unstable feature guard_patterns. RFC 3637 does not forbid it, meaning the current implementation doesn't follow the specification.

The only reason why the current implementation rejects code like this is the fact that prior to the introduction of guard patterns in PR #133424 we already had parse error recovery & a custom diagnostic in place for, well, "accidentally parenthesized match arm patterns with guard". I guess the authors of #133424 did not want to regress the diagnostics here & so opted to leave that case alone (leaving a FIXME).

Furthermore, the error recovery code is hairy & verbose, so I'd like to see it simplified or gone. Fortunately, we can have the cake & eat it, too: We can allow match arm pat ($pat if $expr) if guard_patterns is enabled and continue to provide that custom-tailored diagnostic if it's not enabled (unless the code is cfg'ed out / dropped during expansion which is absolutely acceptable).

Note that we can't just query "is feature guard_patterns enabled" from inside the parser to know when not to emit the custom diagnostic because the answer depends on macro expansion. So instead, we entirely remove this logic from the parser and do it in compiler/rustc_ast_passes/src/feature_gate.rs where we have access to the list of all enabled features (& to Session for querying is_nightly_build).

Part of #129967.

(No LLM was or will be used by me during the entire creation process of this PR)

@fmease fmease added the F-guard_patterns `#![feature(guard_patterns)]` label Oct 17, 2026
@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 Oct 17, 2026
@rustbot

rustbot commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
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, parser
  • compiler, parser expanded to 77 candidates
  • Random selection from 20 candidates

@fmease

This comment was marked as resolved.

@fmease
fmease force-pushed the allow-paren-guarded-pat-in-match-arm branch from bcae0e4 to 784c75e Compare October 17, 2026 14:58

This branch has not been deployed

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

Labels

F-guard_patterns `#![feature(guard_patterns)]` 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants