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: update old-edition behavior of feature gates #136093

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Jan 26, 2025

This is based on #135434; only the latest 6 commits are new. It should be fully implemented, but I'll leave this as a draft until that's merged in case it needs updates.

This updates the behavior of the feature gates ref_pat_eat_one_layer_2024_structural and ref_pat_eat_one_layer_2024 in Editions 2021 and earlier to correspond to the left and right typing rules compared here, respectively. Compared to the stable_rust rules:

  • they both allow reference patterns to match a lone inherited ref,
  • they both allow & patterns to eat &mut reference types (and lone &mut inherited refs) as if they're shared,
  • the left ruleset allows &mut patterns to eat & reference types when there's a &mut inherited reference to also eat,
  • and the left ruleset has RFC 3627's Rule 3: after encountering a shared reference type in the scrutinee, the default binding mode will be treated as by-shared-ref when it would otherwise be by-mutable-ref.

I think there's already tests for all of those typing rules, so I've added revisions to use the existing tests with the new rulesets. Additionally, I've added a few tests to make sure we handle mixed-edition patterns appropriately, and I've added references to the unstable book.

Relevant tracking issue: #123076

r? @ghost

dianne added 11 commits January 20, 2025 16:03
The debug assertion ensuring that the pattern mutability cap holds
assumes the presence of Rule 3, so it now checks for that. I
considered going back to only tracking the mutability cap when Rule 3
is present, but since the mutability cap is used in Rule 5's
implementation too, the debug assertion would still need to check
which typing rules are present.

This also required some changes to tests:
- `ref_pat_eat_one_layer_2021.rs` had a test for Rule 3; I'll be
handling tests for earlier editions in a later commit, so as a stopgap
I've #[cfg]ed it out.
- One test case had to be moved from `well-typed-edition-2024.rs` to
`borrowck-errors.rs` in order to get borrowck to run on it and emit an
error.
These come directly from the "Compare" tab of Typing Rust Patterns,
though they had to be split across multiple files. They're not
comprehensive, but they do provide some previously-missing coverage and
are easier to check against the spec. Possibly they should be split up
some more, since `pattern-errors.rs` is getting a bit unwieldy, but I'm
not sure how best to go about that.
…back for inherited ref mutability mismatches

I think the diagnostic could use some work, but it's more helpful than
the alternative. The previous error was misleading, since it ignored the
inherited reference altogether.
…e pattern doesn't match the mutability of an inner reference

This is the `Deref(EatInner, FallbackToOuter)` rule in Typing Rust Patterns.
- Removes some excess parens

- Removes 3 duplicated tests
This serves two purposes.

First, they're additional tests that stable Rust behavior hasn't been
messed with. There's plenty of other pattern tests, so this is less
important, but these at least are targeted at what's being changed.

Second, this helps document exactly where the new rulesets agree and
disagree with stable pattern typing. This will be especially important
after the new rules for old editions are updated, since they need to be
strictly more permissive; any patterns well-typed on stable should also
be well-typed with the same resultant bindings on the (upcoming) new new
old-edition rules.

The unusual test ordering on `borrowck-errors.rs` and
`ref-binding-on-inh-ref-errors.rs` are to hopefully reduce how much
adding new tests will mess with line numbers in their stderr.
This doesn't (or at least shouldn't!) add, remove, or change any test
cases. I've grouped them by which rule variants they test.
…rules)

Since there are so many ways to write these, I've opted to only include
two sorts of test: simple tests that directly target the rules differing
between rulesets and nuanced tests that produce different errors under
different rulesets. I've also tried not to add any duplicate tests.

`well-typed-edition-2024.rs` already has tests disagreeing with stable,
so I've opted not to include any in this commit that are well-typed
under the new rules.
@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 26, 2025
@dianne dianne force-pushed the match-2024-for-edition-2021 branch from fe072ca to 737d409 Compare January 27, 2025 06:16
These are very bare-bones, only intended to provide some documentation
of what these feature gates are and aren't yet implementing.
This also adds `#[cfg]` attributes to tests for bindings' types,
to make it visually clearer which revisions type successfully.
These are superseded by the old-edition revisions on the shared tests.
@dianne dianne force-pushed the match-2024-for-edition-2021 branch from 737d409 to d37a4d4 Compare January 28, 2025 05:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

2 participants