Repository navigation
Under guard_patterns allow parenthesized guarded patterns in match arms - #164465
Conversation
|
r? @jackh726 rustbot has assigned @jackh726. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment was marked as resolved.
This comment was marked as resolved.
bcae0e4 to
784c75e
Compare
|
r? me |
| "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, |
There was a problem hiding this comment.
Could we emit this note on span_with_leading_if, like normal feature errors?
784c75e to
4bde2a7
Compare
|
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. |
|
@bors r=mu001999 rollup |
|
📌 Commit 4bde2a7 has been tentatively approved by 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: |
…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>
…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>
…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`)
…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`)
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>
|
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. Next Steps:
@rustbot label: +perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)This perf run didn't have relevant results for this metric. CyclesThis perf run didn't have relevant results for this metric. Binary sizeThis perf run didn't have relevant results for this metric. Artifact size: 406.16 MiB -> 406.19 MiB (0.01%) |
|
Just noting that here as well, the "regression" looks like include-blob noise for many of the members of the rollup. |
|
@rustbot label: +perf-regression-triaged |
…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`)
This PR renders
match $expr { ($pat if $expr) => $expr }syntactically legal under unstable featureguard_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)ifguard_patternsis 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_patternsenabled" 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 incompiler/rustc_ast_passes/src/feature_gate.rswhere we have access to the list of all enabled features (& toSessionfor queryingis_nightly_build).Part of #129967.
(No LLM was or will be used by me during the entire creation process of this PR)