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 edition 2024 behavior of feature gates #135434

Merged
merged 12 commits into from
Jan 30, 2025

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Jan 13, 2025

This updates the edition 2024 behavior of the feature gates ref_pat_eat_one_layer_2024_structural and ref_pat_eat_one_layer_2024 to correspond to the left and right typing rules compared here, respectively. I'll implement the proposed new behavior for editions ≤ 2021 in another PR.

The tests are split up a bit awkwardly for practical reasons, but I've added new tests from 3 places:

  • I got tests for where the typing rules differ from the "Compare" tab of the page linked above. These had to be split up based on where the errors are emitted and how rustfixable they are, so they've ended up in different files to keep tidy. Within each file, though, the order of the tests matches the order the typing differences appear in that comparison (as of when this was written).
  • I used this other comparison to test the Deref(EatInner, FallbackToOuter) rule of the left/"structural"/eat-inner ruleset. These are all in well-typed-edition-2024.rs.
  • I added some select tests for cases where the new typing rules differ from current stable Rust. I had to be pickier about what I included here, but I tried to make sure each typing rule got some coverage. That said, my approach for these tests was a bit ad-hoc, so I may have missed something.

Relevant tracking issue: #123076

r? @ghost

@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 13, 2025
@rust-log-analyzer

This comment has been minimized.

@dianne dianne force-pushed the match-2024-for-edition-2024 branch from 2fd80ef to ddf74a4 Compare January 13, 2025 23:18
@dianne
Copy link
Contributor Author

dianne commented Jan 13, 2025

Since the debug assertion that was failing there seems specific to making sure Rule 3 works, I've turned it off when Rule 3 isn't present. There might be some better way of handling the mutability cap if Rule 3 is only present in some editions or under some conditions, but

  • it doesn't sound like there are any plans to make Rule 3 conditional
  • I personally think Rule 3's presence or absence should be consistent across editions, to avoid complicating mixed-edition patterns

@rust-log-analyzer

This comment has been minimized.

@dianne
Copy link
Contributor Author

dianne commented Jan 14, 2025

That failure looks like a github hiccup. I'll hold off on retrying for now, though. I'm not too worried about this passing CI until after #135237 is merged.

@@ -2297,15 +2297,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return expected;
}
InheritedRefMatchRule::EatInner => {
if let ty::Ref(_, _, r_mutbl) = *expected.kind() {
if let ty::Ref(_, _, r_mutbl) = *expected.kind()
&& pat_mutbl <= r_mutbl
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A diagnostics note: currently this means if falling back to matching on the inherited ref fails, the error will be about failing to match an inherited & with a &mut pattern. I think it could use some rewording/explaining in general, but in particular if we adopt fallback-to-outer I think the message should also mention in some way that we first failed to match the inner & with the &mut pattern.

This is also the case for the fallback-to-outer variant of the edition 2021 rules (not in this PR).

Copy link
Member

Choose a reason for hiding this comment

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

Could you leave a // FIXME(ref_pat_eat_one_layer_2024_structural): explain that there was fallback in the error message?

@Nadrieril
Copy link
Member

While you're using the "Compare" tab to generate interesting test cases, could you also add tests for cases that differ between stable_rust and either of the feature gates?

@bors
Copy link
Contributor

bors commented Jan 20, 2025

☔ The latest upstream changes (presumably #135755) made this pull request unmergeable. Please resolve the merge conflicts.

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.
@dianne dianne force-pushed the match-2024-for-edition-2024 branch from ddf74a4 to e288cff Compare January 21, 2025 14:40
@dianne dianne marked this pull request as ready for review January 21, 2025 15:09
@dianne
Copy link
Contributor Author

dianne commented Jan 21, 2025

I've rebased, tidied the tests some more, and added a few tests for cases that lacked coverage. This should be ready for review now. As before, I tried to break the changes up some and added more commentary in the commit descriptions. The added lines are almost entirely tests, largely from adding "stable, edition 2021" revisions, in order to document/verify the comparison between the old and new typing rulesets (and particularly the upcoming new new old-edition rules).

Unfortunately, since there's so many patterns where the new rulesets differ from stable_rust, the amount I could get out of automated comparison was limited. I made sure each way the results differed was represented somehow, but since my approach was ad-hoc, I may have missed some cases. I haven't yet tried automating test generation solely based on the typing judgments Typing Rust Patterns generates, but that (plus hand-written comments and some automated diffing to share tests) might be the way to go if we want more certainty.

r? @Nadrieril

Edit: This actually isn't wasn't totally 100% complete. I'll be adding I added a tiny update to the unstable book too, to at least point to where the new edition 2024 rules are specified (with a note that the currently-implemented behavior in edition 2021 is unspecified).

These are very bare-bones, only intended to provide some documentation
of what these feature gates are and aren't yet implementing.
@dianne dianne force-pushed the match-2024-for-edition-2024 branch from 27ae979 to be7d6e3 Compare January 28, 2025 05:30
@dianne
Copy link
Contributor Author

dianne commented Jan 28, 2025

Some slight changes to the unstable book chapters:

  • I updated the wording/formatting to be a bit more in line with how I ended up writing it for the next PR.
  • I changed the Typing Rust Patterns link on the ref_pat_eat_one_layer_2024 chapter so that it shows as being the stateless_no_binding_modifiers ruleset. The judgments were identical, but the fallback-to-outer option was set differently, so it didn't get the name.

@Nadrieril
Copy link
Member

Looks good! Grateful for the precise commits again.

@bors r+

@bors
Copy link
Contributor

bors commented Jan 29, 2025

📌 Commit be7d6e3 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 29, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 30, 2025
…, r=Nadrieril

Match Ergonomics 2024: update edition 2024 behavior of feature gates

This updates the edition 2024 behavior of the feature gates `ref_pat_eat_one_layer_2024_structural` and `ref_pat_eat_one_layer_2024` to correspond to the left and right typing rules compared [here](https://nadrieril.github.io/typing-rust-patterns/?compare=true&opts2=AQEBAAABAQABAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=true&ty_d=3&style=SequentBindingMode), respectively. I'll implement the proposed new behavior for editions ≤ 2021 in another PR.

The tests are split up a bit awkwardly for practical reasons, but I've added new tests from 3 places:
- I got tests for where the typing rules differ from the "Compare" tab of the page linked above. These had to be split up based on where the errors are emitted and how rustfixable they are, so they've ended up in different files to keep tidy. Within each file, though, the order of the tests matches the order the typing differences appear in that comparison (as of when this was written).
- I used [this other comparison](https://nadrieril.github.io/typing-rust-patterns/?q=%5B%26mut+%26%28mut+x%29%5D%3A+%26mut+%5B%26CT%5D&compare=true&opts2=AQEBAgABAQEBAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=compare&do_cmp=true&ty_d=3&style=SequentBindingMode) to test the `Deref(EatInner, FallbackToOuter)` rule of the left/"structural"/eat-inner ruleset. These are all in `well-typed-edition-2024.rs`.
- I added some select tests for cases where the new typing rules differ from current stable Rust. I had to be pickier about what I included here, but I tried to make sure each typing rule got some coverage. That said, my approach for these tests was a bit ad-hoc, so I may have missed something.

Relevant tracking issue: rust-lang#123076

r? `@ghost`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 30, 2025
…, r=Nadrieril

Match Ergonomics 2024: update edition 2024 behavior of feature gates

This updates the edition 2024 behavior of the feature gates `ref_pat_eat_one_layer_2024_structural` and `ref_pat_eat_one_layer_2024` to correspond to the left and right typing rules compared [here](https://nadrieril.github.io/typing-rust-patterns/?compare=true&opts2=AQEBAAABAQABAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=true&ty_d=3&style=SequentBindingMode), respectively. I'll implement the proposed new behavior for editions ≤ 2021 in another PR.

The tests are split up a bit awkwardly for practical reasons, but I've added new tests from 3 places:
- I got tests for where the typing rules differ from the "Compare" tab of the page linked above. These had to be split up based on where the errors are emitted and how rustfixable they are, so they've ended up in different files to keep tidy. Within each file, though, the order of the tests matches the order the typing differences appear in that comparison (as of when this was written).
- I used [this other comparison](https://nadrieril.github.io/typing-rust-patterns/?q=%5B%26mut+%26%28mut+x%29%5D%3A+%26mut+%5B%26CT%5D&compare=true&opts2=AQEBAgABAQEBAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=compare&do_cmp=true&ty_d=3&style=SequentBindingMode) to test the `Deref(EatInner, FallbackToOuter)` rule of the left/"structural"/eat-inner ruleset. These are all in `well-typed-edition-2024.rs`.
- I added some select tests for cases where the new typing rules differ from current stable Rust. I had to be pickier about what I included here, but I tried to make sure each typing rule got some coverage. That said, my approach for these tests was a bit ad-hoc, so I may have missed something.

Relevant tracking issue: rust-lang#123076

r? ``@ghost``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 30, 2025
…, r=Nadrieril

Match Ergonomics 2024: update edition 2024 behavior of feature gates

This updates the edition 2024 behavior of the feature gates `ref_pat_eat_one_layer_2024_structural` and `ref_pat_eat_one_layer_2024` to correspond to the left and right typing rules compared [here](https://nadrieril.github.io/typing-rust-patterns/?compare=true&opts2=AQEBAAABAQABAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=true&ty_d=3&style=SequentBindingMode), respectively. I'll implement the proposed new behavior for editions ≤ 2021 in another PR.

The tests are split up a bit awkwardly for practical reasons, but I've added new tests from 3 places:
- I got tests for where the typing rules differ from the "Compare" tab of the page linked above. These had to be split up based on where the errors are emitted and how rustfixable they are, so they've ended up in different files to keep tidy. Within each file, though, the order of the tests matches the order the typing differences appear in that comparison (as of when this was written).
- I used [this other comparison](https://nadrieril.github.io/typing-rust-patterns/?q=%5B%26mut+%26%28mut+x%29%5D%3A+%26mut+%5B%26CT%5D&compare=true&opts2=AQEBAgABAQEBAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=compare&do_cmp=true&ty_d=3&style=SequentBindingMode) to test the `Deref(EatInner, FallbackToOuter)` rule of the left/"structural"/eat-inner ruleset. These are all in `well-typed-edition-2024.rs`.
- I added some select tests for cases where the new typing rules differ from current stable Rust. I had to be pickier about what I included here, but I tried to make sure each typing rule got some coverage. That said, my approach for these tests was a bit ad-hoc, so I may have missed something.

Relevant tracking issue: rust-lang#123076

r? ```@ghost```
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 30, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#133636 ([rustdoc] Add sans-serif font setting)
 - rust-lang#135434 (Match Ergonomics 2024: update edition 2024 behavior of feature gates)
 - rust-lang#135739 (Clean up uses of the unstable `dwarf_version` option)
 - rust-lang#135882 (simplify `similar_tokens` from `Option<Vec<_>>` to `&[_]`)
 - rust-lang#136179 (Allow transmuting generic pattern types to and from their base)
 - rust-lang#136199 (Fix a couple Emscripten tests)
 - rust-lang#136238 (ci: refactor how directories are removed in free-disk-space disk)
 - rust-lang#136251 (use impl Into<String> instead of explicit type args with bounds)

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

Rollup of 7 pull requests

Successful merges:

 - rust-lang#133636 ([rustdoc] Add sans-serif font setting)
 - rust-lang#135434 (Match Ergonomics 2024: update edition 2024 behavior of feature gates)
 - rust-lang#135739 (Clean up uses of the unstable `dwarf_version` option)
 - rust-lang#135882 (simplify `similar_tokens` from `Option<Vec<_>>` to `&[_]`)
 - rust-lang#136179 (Allow transmuting generic pattern types to and from their base)
 - rust-lang#136199 (Fix a couple Emscripten tests)
 - rust-lang#136251 (use impl Into<String> instead of explicit type args with bounds)

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

Match Ergonomics 2024: update edition 2024 behavior of feature gates

This updates the edition 2024 behavior of the feature gates `ref_pat_eat_one_layer_2024_structural` and `ref_pat_eat_one_layer_2024` to correspond to the left and right typing rules compared [here](https://nadrieril.github.io/typing-rust-patterns/?compare=true&opts2=AQEBAAABAQABAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=true&ty_d=3&style=SequentBindingMode), respectively. I'll implement the proposed new behavior for editions ≤ 2021 in another PR.

The tests are split up a bit awkwardly for practical reasons, but I've added new tests from 3 places:
- I got tests for where the typing rules differ from the "Compare" tab of the page linked above. These had to be split up based on where the errors are emitted and how rustfixable they are, so they've ended up in different files to keep tidy. Within each file, though, the order of the tests matches the order the typing differences appear in that comparison (as of when this was written).
- I used [this other comparison](https://nadrieril.github.io/typing-rust-patterns/?q=%5B%26mut+%26%28mut+x%29%5D%3A+%26mut+%5B%26CT%5D&compare=true&opts2=AQEBAgABAQEBAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=compare&do_cmp=true&ty_d=3&style=SequentBindingMode) to test the `Deref(EatInner, FallbackToOuter)` rule of the left/"structural"/eat-inner ruleset. These are all in `well-typed-edition-2024.rs`.
- I added some select tests for cases where the new typing rules differ from current stable Rust. I had to be pickier about what I included here, but I tried to make sure each typing rule got some coverage. That said, my approach for these tests was a bit ad-hoc, so I may have missed something.

Relevant tracking issue: rust-lang#123076

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

5 participants