-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 #3627
base: master
Are you sure you want to change the base?
Match ergonomics 2024 #3627
Conversation
(including "inherited" references). | ||
|
||
```rust | ||
let &foo = &mut 42; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foo
is still immutable right?
let &foo = &mut 42;
foo = 53; // error[E0384]: cannot assign twice to immutable variable `foo`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. (It would be immutable with let &mut foo = &mut 42;
also.)
Co-authored-by: kennytm <kennytm@gmail.com>
The " |
It could be split off, yes. I mention this in the rationale section, but one of the motivating factors for including it is the "no inherited |
The "no inherited |
It is not. On edition 2024, this example would work without the rule, but errors with it: (I'll edit the RFC to make that more clear) |
I'm not sure why (a version of) "no inherited Really I'm not sure why we would even want to reject it in the first place. There is still a This is exactly the sort of thing that would be good to work out as part of deref patterns. Going back to the comparison with places, the reason you can get a Accepting this example is a different sort of consistency than the "immutability takes precedence" one. Instead, it's "inherited reference type matches skipped reference type." This also brings back the locality that " If we want to defer any decision at all here, I believe we could instead forbid matching |
If there are several skipped reference types with different mutabilities, you have to choose: eg in |
let _: &mut u8 = foo; | ||
``` | ||
|
||
## Edition 2024: `&` and `&mut` can match against inherited references |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! With this, is there even a reason to keep supporting &mut
in patterns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, without it we can't get desugar match ergonomics that get mutable references into places: let &mut Struct { ref mut field } = ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, okay. Sounds worthy of a clippy lint for if it's not combined with ref mut
then :)
The innermost one, of course! Why would it be anything else? That's how default binding modes already work, and we're merely exposing that to the user so they can exit the current mode, not trying to reinvent how the mode is entered. |
No it's not! Outer shared + inner mutable results in |
What I'm saying is that it is how initially entering a default binding mode already works. That's why the example The inconsistency we're both talking about is the difference in behavior between "behind an explicit You're proposing we resolve it in favor of the binding mode (essentially letting explicit |
Here's another way of looking at it: if Simple case: let &mut x = &mut 42; // x: i32
let x = &mut 42; // x: &mut i32 Roundabout case: let &[&mut x] = &[&mut 42]; // x: i32
//let &[x] = &[&mut 42]; // ERROR, but…
let &[ref x] = &[&mut 42]; // x: &&mut i32 -> we can get an &mut in the end However: let &[[&mut x]] = &[&mut [42]]; // If we allow this, with x: i32
//let &[[x]] = &[&mut [42]]; // and then remove the &mut… -> ERROR move check, if the default binding mode is to be `ref mut`
// nothing we do will get us &mut i32 in any form |
Indeed, that is a demonstration of the fact that we are limited to a single layer of inherited reference/a single binding mode. If we relaxed that (not that I think this would be useful in practice), you could get a let &[[&mut x]] = &[&mut [42]]; // x: i32
let &[[x]] = &[&mut [42]]; // ERROR move check, unless the pattern equivalent of auto(de)ref coerces &mut i32 -> x: &i32
let &[[ref x]] = &[&mut [42]]; // HYPOTHETICAL: A `ref` binding + an inherited `&mut` -> x: &&mut i32 My proposal is not so much breaking the user's ability to remove a If I understand what you're getting at, your proposal would avoid this edge case by preemptively replacing |
It is semantically impossible to relax that. E.g. let var = Some(0);
if let Some(x) = &&var {
//...
} Here |
Well, that was only a hypothetical to demonstrate a different way of thinking about the limitation Jules pointed out. But I don't think it's entirely impossible to materialize those extra reference layers- we could introduce temporaries for them around the match, it would just be a bit silly, and they would have shortened lifetimes. (Not to mention confusing to be able to layer binding modes like my example.) |
AIUI, what you're proposing would be backward compatible. There is going to be a follow-up to this RFC at some point (to choose a syntax for mutable by-reference bindings, and possibly also to provide a solution for matching |
The reason I'm making this proposal in the first place is because it enables us to defer " |
@rfcbot reviewed Having mulled it over, I think this RFC strikes the right balance on all the questions we need to decide now. I would also like to see us move forward with an implementation in nightly quickly to give enough time for experimentation with the new rules and with the edition migration.
I would rather have the first option work than the second, especially when considering the alternatives (to binding
While I can see that we might want to allow this in the future, it's not entirely clear to me that this is the case. As the RFC states, "there is not much use for mutable by-reference bindings". They seem rare enough that I would guess a significant percentage of the time, they are not actually what the user wants when written, and when used correctly are unclear to the reader. |
ICYMI: rust-lang/rust#123076 Everything in this RFC, including the migration lint, is either already in nightly under an experimental feature gate, or waiting on PR review. |
d509ffa
to
04501bc
Compare
Reviewing this section again, I've realized that I grossly overstated my case. For the |
As Jules noted, it should be backwards compatible with the RFC to accept the Rather, given that we want to make
|
@rfcbot resolve leaving-us-in-an-inconsitent-state |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Fantastic, thanks for your efforts here. Ideally I would like to see this go in as part of the unstable 2024 edition once this RFC lands, i.e. not blocking on a stabilization FCP before that happens. I'm not sure if that's consistent or not with what we've done in the past.
Thanks for the correction! The new examples feel more like a wash to me, to the point where I don't know which one to prefer. The RFC lists arguments in favor of this rule, but what are the drawbacks? I guess this one? (It sounds like it applies, even though the surrounding conversation seems to be about the
Are there others? @rfcbot concern what tradeoff are we making with the " If we have a clear idea of the tradeoff we're making I would think we can resolve this in a triage meeting, so nominating for discussion now. @rustbot label I-lang-nominated |
That's basically it. //! Edition ≥ 2024
let [[&mut x]] = [&mut [42]]; // This works
let &[[&mut x]] = &[&mut [42]]; // This is broken by the proposed rule (example given in RFC)
let [[&mut x]] = &[&mut [42]]; // This is also broken by the rule Note that with //! Edition ≥ 2024
let [[&x]] = [&mut [42]]; // This works, with same meaning as example above
let &[[&x]] = &[&mut [42]]; // This also works
let [[&x]] = &[&mut [42]]; // This as well (same meaning) That's why |
As I understand it, the main drawback of this alternative would be that the compiler would no longer "do the right thing" for you when matching by reference to x in So we are not converging on the same place: fundamentally we have to choose, or (perhaps) go for an even more conservative "non-choice" option. @rpjohnst Are you making an argument in favor of going with the alternative option? If so, is there a chance you could write that up in a more self-contained hackmd? Even if we accept the current RFC as-is, it would be good to include as part of the RFC alternatives section. |
My understanding of @rpjohnst's position is that he thinks we should accept the union of what the two alternatives accept (is, be maximally permissive). |
I'm not proposing that we keep the old behavior of an unusable I'm also not proposing that we be maximally permissive, @Jules-Bertholet. My primary reason for jumping into this thread has always been to make the argument that we not make the " However, as currently specified, the "no This is why the RFC accepts I look at this consequence of "no So in the spirit of describing this in a self-contained way, I am proposing these changes to parts 2-4 of the RFC's reference section:
This last point should behave the same way as the RFC (e.g. |
Many thanks for the clarifications, @rpjohnst and @Jules-Bertholet. I think I have a good grasp of your positions now.
Leaving more design space open for deref patterns is desirable. At the same time, I struggle somewhat to come up with a scenario in which adopting " While I'd like to avoid going too deep into hypotheticals, is there any way you could sketch out the kind of question this might restrict our choices on in the future, @rpjohnst? cc @WaffleLapkin, who was also worried about interactions here.
For my own understanding, is there a "nuclear option" that rejects both of these and allows us to choose one later? Does that mean delaying " |
It's possible to do, we would have to forbid matching against inherited references in the specific situation that the rule affects. It would be confusing and irritating for users, though. Unless there a compelling reason to think we might gain new information in the future to better inform this choice (and I don't see us), delay will not do any good IMO. |
It's looking like deref patterns will introduce some syntax for "deref a scrutinee of any pointer type, and then match on the result." That syntax may or may not be Similarly, if we introduce " Regarding the "nuclear option," we would only need to reject matching on inherited references which are both A) inherited from a
Personally, I don't believe this is important. The language is full of ways to "talk about" something without being able to "get your hands on it," but these are enforced by the borrow checker, rather than by adjusting types ahead of time. Consider that, given When you rely on auto(de)ref to get a To me it seems more in line with the rest of the language to accepting |
The deciding factor is ultimately whether the lang team has considered all of the information it thinks is relevant, or will have time to do so, without either delaying the edition or risking this RFC as part of it.
What about the second part of my question — delaying " We all agree this feature is desirable, and I would like to ship it as part of the story we tell for the edition. But we also have to prioritize decisions that must be made for the edition (and we are whittling those down quickly, knock on wood). Is this feature inherently edition sensitive on its own? |
Yes, at least if we adopt the "eat-one-layer" model as proposed (and alternative models have their own issues.) |
Rendered
Changes to match ergonomics for the 2024 edition.
@rustbot label T-lang A-patterns A-edition-2024