Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
fmease:allow-paren-guarded-pat-in-match-arm
Nov 16, 2026
Merged

rust-bors[bot] merged 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.

@mu001999

Copy link
Copy Markdown
Member

r? me

@rustbot rustbot assigned mu001999 and unassigned jackh726 Nov 13, 2026

@mu001999 mu001999 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just one thought :)

@rustbot author

View changes since this review

"see issue #129967 <https://github.com/rust-lang/rust/issues/129967> for more information"
)]
#[help("add `#![feature(guard_patterns)]` to the crate attributes to enable")]
pub sugg_feature: bool,

@mu001999 mu001999 Nov 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we emit this note on span_with_leading_if, like normal feature errors?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 13, 2026
@fmease
fmease force-pushed the allow-paren-guarded-pat-in-match-arm branch from 784c75e to 4bde2a7 Compare November 14, 2026 16:38
@rustbot

rustbot commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@fmease

fmease commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

@bors r=mu001999 rollup

@rust-bors

rust-bors Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4bde2a7 has been tentatively approved by mu001999

It will be put into the queue for this repository once PR CI succeeds.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: consistent failures on main

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 14, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 14, 2026
…match-arm, r=mu001999

Under `guard_patterns` allow parenthesized guarded patterns in match arms

This PR renders `match $expr { ($pat if $expr) => $expr }` syntactically legal under unstable feature `guard_patterns`. [RFC 3637](rust-lang/rfcs#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 rust-lang#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 rust-lang#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 rust-lang#129967.

<sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 14, 2026
…match-arm, r=mu001999

Under `guard_patterns` allow parenthesized guarded patterns in match arms

This PR renders `match $expr { ($pat if $expr) => $expr }` syntactically legal under unstable feature `guard_patterns`. [RFC 3637](rust-lang/rfcs#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 rust-lang#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 rust-lang#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 rust-lang#129967.

<sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>
rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - #165928 (Replace most uses of `type_array` with `type_byte_array` in `rustc_codegen_llvm`)
 - #160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - #161950 (Ensure floats are returned losslessly by the C ABI on 32-bit x86)
 - #162267 (Fix an edge within `std::fs::create_dir_all` for certain platforms that views trailing cur dir components as a separate missing directory components)
 - #164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - #164808 (asm const ptr: move name generation to asm-specific logic)
 - #164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - #165568 (rustdoc: Suggest corrections for reference label typos)
 - #165641 (Treat all proc macro dependencies as host dependencies in bootstrap)
 - #165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - #165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - #163696 (Stabilize `int_from_ascii`)
 - #163882 (docs(vec): clarify into_boxed_slice allocation guarantee)
 - #164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - #164465 (Under `guard_patterns` allow parenthesized guarded patterns in match arms)
 - #164997 (Fix as str suggestion in subpattern)
 - #165825 (make `Complex<x87_f80>` ABI-compatible with C)
 - #165868 (Small changes to dyn compatibility)
 - #165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - #165902 (Add allocator() to vec_deque::IntoIter and vec_deque::Drain)
 - #165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - #165914 (Use `trim_prefix` in bootstrap)
 - #165919 (tests: accept better behavior on underaligned accesses)

Failed merges:

 - #165893 (Prune un/under used methods of `ast::Attribute`)
rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - #165928 (Replace most uses of `type_array` with `type_byte_array` in `rustc_codegen_llvm`)
 - #160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - #161950 (Ensure floats are returned losslessly by the C ABI on 32-bit x86)
 - #162267 (Fix an edge within `std::fs::create_dir_all` for certain platforms that views trailing cur dir components as a separate missing directory components)
 - #164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - #164808 (asm const ptr: move name generation to asm-specific logic)
 - #164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - #165568 (rustdoc: Suggest corrections for reference label typos)
 - #165641 (Treat all proc macro dependencies as host dependencies in bootstrap)
 - #165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - #165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - #163696 (Stabilize `int_from_ascii`)
 - #163882 (docs(vec): clarify into_boxed_slice allocation guarantee)
 - #164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - #164465 (Under `guard_patterns` allow parenthesized guarded patterns in match arms)
 - #164997 (Fix as str suggestion in subpattern)
 - #165825 (make `Complex<x87_f80>` ABI-compatible with C)
 - #165868 (Small changes to dyn compatibility)
 - #165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - #165902 (Add allocator() to vec_deque::IntoIter and vec_deque::Drain)
 - #165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - #165914 (Use `trim_prefix` in bootstrap)
 - #165919 (tests: accept better behavior on underaligned accesses)

Failed merges:

 - #165893 (Prune un/under used methods of `ast::Attribute`)
@rust-bors
rust-bors Bot merged commit c697e90 into rust-lang:main Nov 16, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 16, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 16, 2026
Rollup merge of #164465 - fmease:allow-paren-guarded-pat-in-match-arm, r=mu001999

Under `guard_patterns` allow parenthesized guarded patterns in match arms

This PR renders `match $expr { ($pat if $expr) => $expr }` syntactically legal under unstable feature `guard_patterns`. [RFC 3637](rust-lang/rfcs#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.

<sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>
@rust-timer

Copy link
Copy Markdown
Collaborator

Note

This PR was benchmarked as part of triage of its containing rollup: triage URL.

Finished benchmarking commit (37443d9): comparison URL.

Overall result: ❌ regressions - please read:

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.3% [0.0%, 0.5%] 9
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

This perf run didn't have relevant results for this metric.

Cycles

This perf run didn't have relevant results for this metric.

Binary size

This perf run didn't have relevant results for this metric.

Artifact size: 406.16 MiB -> 406.19 MiB (0.01%)

@rustbot rustbot added the perf-regression Performance regression. label Nov 16, 2026
@fmease
fmease deleted the allow-paren-guarded-pat-in-match-arm branch November 16, 2026 08:37
@lqd

lqd commented Nov 16, 2026

Copy link
Copy Markdown
Member

Just noting that here as well, the "regression" looks like include-blob noise for many of the members of the rollup.

@JonathanBrouwer

Copy link
Copy Markdown
Member

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 16, 2026
sushink70 pushed a commit to sushink70/miri that referenced this pull request Nov 17, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - rust-lang/rust#165928 (Replace most uses of `type_array` with `type_byte_array` in `rustc_codegen_llvm`)
 - rust-lang/rust#160618 (require a consistent codegen backend when -Zllvm-target-feature is present)
 - rust-lang/rust#161950 (Ensure floats are returned losslessly by the C ABI on 32-bit x86)
 - rust-lang/rust#162267 (Fix an edge within `std::fs::create_dir_all` for certain platforms that views trailing cur dir components as a separate missing directory components)
 - rust-lang/rust#164043 (alloc: Unstably add `Vec::repeat` and `Vec::repeat_in`)
 - rust-lang/rust#164808 (asm const ptr: move name generation to asm-specific logic)
 - rust-lang/rust#164823 (Use `DerefMut` and `IndexMut` before `&raw mut` autoborrow)
 - rust-lang/rust#165568 (rustdoc: Suggest corrections for reference label typos)
 - rust-lang/rust#165641 (Treat all proc macro dependencies as host dependencies in bootstrap)
 - rust-lang/rust#165924 (rustc_codegen_gcc subtree update 2026-11-13)
 - rust-lang/rust#165927 (Remove `const_struct` from `rustc_codegen_ssa`)
 - rust-lang/rust#163696 (Stabilize `int_from_ascii`)
 - rust-lang/rust#163882 (docs(vec): clarify into_boxed_slice allocation guarantee)
 - rust-lang/rust#164362 (Emit note for `non_exhaustive` within defining crate for `unreachable_patterns`)
 - rust-lang/rust#164465 (Under `guard_patterns` allow parenthesized guarded patterns in match arms)
 - rust-lang/rust#164997 (Fix as str suggestion in subpattern)
 - rust-lang/rust#165825 (make `Complex<x87_f80>` ABI-compatible with C)
 - rust-lang/rust#165868 (Small changes to dyn compatibility)
 - rust-lang/rust#165879 (Add `#[rustc_test_entrypoint_marker]` back !)
 - rust-lang/rust#165902 (Add allocator() to vec_deque::IntoIter and vec_deque::Drain)
 - rust-lang/rust#165903 (Fix borrowck ICE when a borrow outlives a local in a const block)
 - rust-lang/rust#165914 (Use `trim_prefix` in bootstrap)
 - rust-lang/rust#165919 (tests: accept better behavior on underaligned accesses)

Failed merges:

 - rust-lang/rust#165893 (Prune un/under used methods of `ast::Attribute`)
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)]` perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants