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: Implement eat-one-layer #123512

Merged
merged 6 commits into from
Apr 16, 2024

Conversation

Jules-Bertholet
Copy link
Contributor

r? @Nadrieril

cc #123076

@rustbot label A-edition-2024 A-patterns

@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. A-edition-2024 Area: The 2024 edition A-patterns Relating to patterns and pattern matching labels Apr 5, 2024
@Jules-Bertholet Jules-Bertholet force-pushed the ref-pat-eat-one-layer-2024 branch 2 times, most recently from 0c430f7 to 2f730c1 Compare April 5, 2024 20:38
Comment on lines 26 to 31
if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
let _: u32 = x;
}
if let Some(&Some(&mut x)) = &mut Some(& Some(0)) {
let _: u32 = x;
}
Copy link
Member

@Nadrieril Nadrieril Apr 5, 2024

Choose a reason for hiding this comment

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

These two are weird, shouldn't we be eating references in the same order as in the type?

Copy link
Member

Choose a reason for hiding this comment

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

Does this typecheck?

if let Some(&Some(x)) = &mut Some(&Some(0)) {
    let _: &mut u32 = x;
}

Copy link
Contributor Author

@Jules-Bertholet Jules-Bertholet Apr 6, 2024

Choose a reason for hiding this comment

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

Does this typecheck?

Not any longer, pushed a fix so we now properly downgrade the mutability of the inherited reference.

Shouldn't we be eating references in the same order as in the type?

No, that would be unnecessarily restrictive, as demonstrated by this newly added test:

if let Some(Some(&mut x)) = &Some(Some(&mut 0)) {
    let _: &u32 = x;
}

(let &mut x = &&mut 0 does not work currently, but I think it should, will try to fix edit: also works now)

Copy link
Member

@Nadrieril Nadrieril Apr 6, 2024

Choose a reason for hiding this comment

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

Hm, I would not expect these tests to work personally.

I would expect we want a rule like: let &mut x = <expr>; x: T iff let x = <expr>; x: &mut T, no?

Copy link
Contributor Author

@Jules-Bertholet Jules-Bertholet Apr 6, 2024

Choose a reason for hiding this comment

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

I don't see any good reason we should restrict it to that, when the currently implemented rule (if the pattern can match against the inherited reference, do so; otherwise match against a reference in the scrutinee type) is strictly more useful. let Some(&mut x) = &Some(&mut 0) is more convenient than let &Some(&mut (ref x)) = &Some(&mut 0), why should we force people to use the latter?

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't let Some(&x) = &Some(&mut 0) work just as well?

Copy link
Contributor Author

@Jules-Bertholet Jules-Bertholet Apr 6, 2024

Choose a reason for hiding this comment

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

No, that would make x an &mut {integer} instead of an &{integer}, and then the compiler yells at you that you can't move out of a shared reference

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see. Well, I'm happy to merge your design, this is an implementation review not a design review. I'll just need some time to wrap my head around the new commits

@rustbot
Copy link
Collaborator

rustbot commented Apr 6, 2024

Some changes occurred in match checking

cc @Nadrieril

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.

Alright, that looks quite good. I'm ok with most of the implementation, I'd just like to try and simplify calc_default_binding_mode further because it's becoming thorny.

compiler/rustc_ast/src/ast.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/pat.rs Outdated Show resolved Hide resolved
AdjustMode::ResetAndConsumeRef(ref_pat_mutbl) => {
let mutbls_match = def_bm.0 == ByRef::Yes(ref_pat_mutbl);
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
let max_ref_mutbl = cmp::min(max_ref_mutbl, ref_pat_mutbl);
Copy link
Member

Choose a reason for hiding this comment

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

Intuitively I'd expect we can just cap def_bm by max_ref_mutbl just before returning from the function, and cap it by ref_pat_mutbl at the end of this if. Does that work? The peel_off_references change I just suggested could help.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made a slight tweak to this function, but didn't go as far as that. The capping by max_ref_mutbl has to be gated by edition/feature gate, so needs to stay inside the if.

@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 Apr 13, 2024
@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 Apr 14, 2024
@bors
Copy link
Contributor

bors commented Apr 16, 2024

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

@Nadrieril
Copy link
Member

Looks good, ty!

@bors r+

@bors
Copy link
Contributor

bors commented Apr 16, 2024

📌 Commit 3efbe3e 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 Apr 16, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 16, 2024
…llaumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#122811 (Move `SourceMap` initialization)
 - rust-lang#123512 (Match ergonomics 2024: Implement eat-one-layer)
 - rust-lang#123811 (Use queue-based `RwLock` on more platforms)
 - rust-lang#123859 (Remove uneeded clones now that TrustedStep implies Copy)
 - rust-lang#123979 (Subtype predicates only exist on inference types, so we can allow them to register opaque types within them.)
 - rust-lang#124016 (Outline default query and hook provider function implementations)
 - rust-lang#124023 (Allow workproducts without object files.)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 239b372 into rust-lang:master Apr 16, 2024
12 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 16, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 16, 2024
Rollup merge of rust-lang#123512 - Jules-Bertholet:ref-pat-eat-one-layer-2024, r=Nadrieril

Match ergonomics 2024: Implement eat-one-layer

r? `@Nadrieril`

cc rust-lang#123076

`@rustbot` label A-edition-2024 A-patterns
@Jules-Bertholet Jules-Bertholet deleted the ref-pat-eat-one-layer-2024 branch April 16, 2024 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2024 Area: The 2024 edition A-patterns Relating to patterns and pattern matching 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