Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match Ergonomics 2024: document and reorganize the currently-implemented feature gates #135237

Merged
merged 11 commits into from
Jan 20, 2025

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Jan 8, 2025

The hope here is to make it easier to adjust, understand, and test the experimental pattern typing rules implemented in the compiler. This PR doesn't (or at isn't intended to) change any behavior or add any new tests; I'll be handling that later. I've also included some reasoning/commentary on the more involved changes in the commit messages.

Relevant tracking issue: #123076

r? @Nadrieril

dianne added 6 commits January 7, 2025 23:15
This aims to reduce the complexity needed in the boolean logic for telling which
rules we're using to type patterns. If we still want the functionality this
removes, we can re-add it later, after some cleanup to pattern typing.
As far as I can tell, the assignment removed here will never do
anything. `pat_info.max_ref_mutbl` starts at `MutblCap::Mut` for the
top-level pattern and is only changed if feature gates are enabled that
would result in the statement not being executed. Regardless of what new
pattern typing rules are adopted, I don't imagine we want to
conditionally reset `max_ref_mutbl` based on edition either, since it'd
have consequences for subpatterns in other editions.
If Rules 3 or 5 are adopted in any edition, we'll need to track the
`MutblCap` in all editions for macro hygiene purposes. Previously, the
check for whether to track it was conflated with the checks for whether
to apply Rules 3 and 5, so to make it a bit clearer, this always tracks
the `MutblCap`. If needed, we could check if Rules 3 or 5 are present in
any edition before tracking the `MutblCap`, but since it's not that much
more expensive to always track it, I've figured that's simplest.

My main concern with removing the checks is that it may not be clear
that the `MutblCap` is tracked for those specific purposes. To try and
mitigate this, I've made its doc comment a bit more precise regarding
the extent of how and why it's used.

This leaves the condition untouched on the `cap_to_weakly_not` call
needed for Rule 5, since it's only needed for that and it can affect
diagnostics.
The goal of this cleanup is to make it more apparent which feature gates
correspond to which typing rules, and which typing rules correspond to
what code. My intent is for calls to the "which typing rules do we
have?" functions to be replaced by comments (and edition checks, as
appropriate), but as long as we're experimenting with multiple rulesets,
this seemed to me to be the easiest to document and read. There's still
some nontrivial control flow, but I've added comments to try and make it
clearer.

There's some logic that looks like it could be de-duplicated across
different ways of matching against inherited references; however, the
duplication is intentional. Once we choose which rulesets we want, we
can make this more clever, but until then, my priorities are clarity and
ease of modification/extension. That said, I think the diagnostics could
use some work; factoring out commonalities there (and separating them
from the typing logic) would be ideal. I've opted not to include that
here both since it'd make this refactor less obvious and since it
affects test output.

Also, this doesn't get quite as fine-grained as Typing Rust Patterns, so
there's some instances where certain rules are conflated. I'd prefer to
minimize dead/untested codepaths for rulesets we're not interested in,
so as a compromise I've added comments wherever some aspect of the
typing rules is assumed from another. I'm not totally happy with it, but
I think it's at least better than plain checks against the feature gates
and edition.
This only includes previously existing tests (with a couple duplicates
removed). I plan on adding more comprarisons where the rules differ
once I've updated the pattern typing rules. I also haven't touched the
tests for new rules in old editions; I'll see how best to handle that
once those rules are updated as well.
@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 Jan 8, 2025
This also makes it test the "structural" ruleset, in preparation for
additional tests where the rulesets disagree.
This was a test for `ref_pat_eat_one_layer_2024` downgrading a `mut ref`
default binding mode to `ref` within a shared reference pattern (i.e.
Rule 3) on edition 2021 specifically. Since it's near-identical to
another existing test (currently in `ref_pat_eat_one_layer_2021.rs` in
the "experimental" rfc 3627 subdirectory) and that particular feature
gate's typing rulesets are planned to no longer have Rule 3, I'm opting
to remove it rather than continue maintaining it separately.
@dianne
Copy link
Contributor Author

dianne commented Jan 13, 2025

I've added bit more test maintenance. The remaining tests I haven't touched at this point are:

  • feature-gate-ref_pat_eat_one_layer_2024.rs: this seems to follow the pattern of other gate-tests.
  • ref_pat_eat_one_layer_2021.rs and ref_pat_eat_one_layer_2021_fail.rs: what I do with these depends on how easy it is to test edition 2021 and 2024 behavior together. Until I figure out exactly what's needed, I'm leaving them as-is.
  • tests/ui/pattern/mut_preserve_binding_mode_2021.rs and tests/ui/pattern/mut_preserve_binding_mode_2024.rs: these are for the mut_ref feature and mirror the mut-ref-mut.rs failure case. I'm not sure what the status of mut_ref is, but as long as I don't break them, I don't mind leaving them for whoever's maintaining that feature.
  • tests/ui/pattern/skipped-ref-pats-issue-125058.rs: this is a regression test for an ICE. I'm not sure whether it belongs with the tests that the feature gates behave according to spec, so I've left it for now, though it may want a new home (or at least a comment).

@Nadrieril
Copy link
Member

I'm not sure what the status of mut_ref is

It was made as part of the match ergonomics 2024 work, it's not really maintained nor pushed forward by anyone. Let's keep it around until we're sure we won't need it.

Copy link
Member

@Nadrieril Nadrieril left a comment

Choose a reason for hiding this comment

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

Thanks for this! I'm a fan of making the rules more explicit and separate from the feature gates, this is a great legibility improvement. Much appreciate the small commits also.

Very good point on needing to track MutblCap in all editions! Could you add a test for that? We already have a cross-edition test in rfc-3627-match-ergonomics-2024/migration-lint.rs, it would make sense to add a mixed_editions.rs file with a test or two. Feel free to leave that for a later PR.

Code looks good, I only have suggestions regarding comments.

compiler/rustc_hir_typeck/src/pat.rs Show resolved Hide resolved
compiler/rustc_hir_typeck/src/pat.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/pat.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/pat.rs Show resolved Hide resolved
compiler/rustc_hir_typeck/src/pat.rs Show resolved Hide resolved
compiler/rustc_hir_typeck/src/pat.rs Show resolved Hide resolved
compiler/rustc_hir_typeck/src/pat.rs Show resolved Hide resolved
@Nadrieril
Copy link
Member

@rustbot author

@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 Jan 19, 2025
@dianne
Copy link
Contributor Author

dianne commented Jan 20, 2025

I've added your suggested comments and debug asserts; ty! I'll make a note to write some tests for mixed-edition patterns too.

@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 Jan 20, 2025
@Nadrieril
Copy link
Member

Let's goo

bors-r-plus

@bors
Copy link
Contributor

bors commented Jan 20, 2025

📌 Commit 0263772 has been approved by Nadrieril

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-review Status: Awaiting review from the assignee but also interested parties. labels Jan 20, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 20, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang#134276 (fully de-stabilize all custom inner attributes)
 - rust-lang#135237 (Match Ergonomics 2024: document and reorganize the currently-implemented feature gates)
 - rust-lang#135310 (Always force non-trimming of path in `unreachable_patterns` lint)
 - rust-lang#135446 (further improve panic_immediate_abort by removing rtprintpanic! messages)
 - rust-lang#135491 (Remove dead rustc_allowed_through_unstable_modules for std::os::fd contents)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1419f79 into rust-lang:master Jan 20, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 20, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 20, 2025
Rollup merge of rust-lang#135237 - dianne:match-2024-cleanup, r=Nadrieril

Match Ergonomics 2024: document and reorganize the currently-implemented feature gates

The hope here is to make it easier to adjust, understand, and test the experimental pattern typing rules implemented in the compiler. This PR doesn't (or at isn't intended to) change any behavior or add any new tests; I'll be handling that later. I've also included some reasoning/commentary on the more involved changes in the commit messages.

Relevant tracking issue: rust-lang#123076

r? `@Nadrieril`
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.

4 participants