Skip to content

Parse guard patterns - #133424

Merged
bors merged 8 commits into
rust-lang:masterfrom
Nadrieril:guard-patterns-parsing
Dec 9, 2024
Merged

bors merged 8 commits into
rust-lang:masterfrom
Nadrieril:guard-patterns-parsing

Conversation

@Nadrieril

@Nadrieril Nadrieril commented Nov 24, 2024 •

Copy link
Copy Markdown
Member

This implements the parsing of RFC3637 Guard Patterns (see also tracking issue). This PR is extracted from #129996 with minor modifications.

cc @max-niederman

@rustbot

rustbot commented Nov 24, 2024

Copy link
Copy Markdown
Collaborator

r? @fee1-dead

rustbot has assigned @fee1-dead.
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

@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 Nov 24, 2024
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Nadrieril
Nadrieril force-pushed the guard-patterns-parsing branch from 6f2c8e0 to 962c014 Compare November 24, 2024 18:42
@Nadrieril
Nadrieril marked this pull request as ready for review November 24, 2024 20:21
@rustbot

rustbot commented Nov 24, 2024

Copy link
Copy Markdown
Collaborator

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@Nadrieril

Copy link
Copy Markdown
Member Author

@rustbot ready

Comment thread src/tools/rustfmt/src/patterns.rs Resolved
Comment thread compiler/rustc_parse/src/parser/expr.rs Resolved
Comment thread compiler/rustc_expand/src/expand.rs Resolved
@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 28, 2024
@Nadrieril

Copy link
Copy Markdown
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 7, 2024

@fee1-dead fee1-dead 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.

r=me

@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 Dec 8, 2024
@Nadrieril

Copy link
Copy Markdown
Member Author

@bors r=@fee1-dead

@bors

bors commented Dec 8, 2024

Copy link
Copy Markdown
Contributor

📌 Commit 2459dbb has been approved by fee1-dead

It is now in the queue for this repository.

@bors bors 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 Dec 8, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 8, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#133424 (Parse guard patterns)
 - rust-lang#133733 ( compiletest: show the difference between the normalized output and the actual output for lines which didn't match)
 - rust-lang#133993 (Fix: typo in E0751 error explanation)
 - rust-lang#134013 (Adds new intrinsic declaration)
 - rust-lang#134020 (Remove unnecessary `int_type_width_signed` function)
 - rust-lang#134024 (Advent of `tests/ui` (misc cleanups and improvements) [2/N])
 - rust-lang#134038 (deps: Update psm)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 8, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#133424 (Parse guard patterns)
 - rust-lang#133733 ( compiletest: show the difference between the normalized output and the actual output for lines which didn't match)
 - rust-lang#133993 (Fix: typo in E0751 error explanation)
 - rust-lang#134013 (Adds new intrinsic declaration)
 - rust-lang#134020 (Remove unnecessary `int_type_width_signed` function)
 - rust-lang#134024 (Advent of `tests/ui` (misc cleanups and improvements) [2/N])
 - rust-lang#134038 (deps: Update psm)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 1868c8f into rust-lang:master Dec 9, 2024
@rustbot rustbot added this to the 1.85.0 milestone Dec 9, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Dec 9, 2024
Rollup merge of rust-lang#133424 - Nadrieril:guard-patterns-parsing, r=fee1-dead

Parse guard patterns

This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](rust-lang#129967)). This PR is extracted from rust-lang#129996 with minor modifications.

cc `@max-niederman`
@Nadrieril
Nadrieril deleted the guard-patterns-parsing branch December 9, 2024 09:15
flip1995 pushed a commit to flip1995/rust that referenced this pull request Dec 15, 2024
…r=fee1-dead

Parse guard patterns

This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](rust-lang#129967)). This PR is extracted from rust-lang#129996 with minor modifications.

cc `@max-niederman`
rust-bors Bot pushed a commit that referenced this pull request Feb 27, 2026
Parse guard patterns

This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](#129967)). This PR is extracted from #129996 with minor modifications.

cc `@max-niederman`
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>
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Nov 15, 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
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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