-
Notifications
You must be signed in to change notification settings - Fork 13.3k
if let
pre-stabilization preview
#140908
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
if let
pre-stabilization preview
#140908
Conversation
This comment has been minimized.
This comment has been minimized.
src/doc/if-let-guard-review.md
Outdated
Here `v` is `&Vec`, and the guard borrows parts of it; those references are valid in the arm body. If a guard binds by value | ||
(e.g. `if let x = some_moveable`), the usual move/borrow rules apply (see below), but in all cases the scopes follow the match-arm rules. | ||
|
||
Moreover, an `if let` guard cannot break exhaustiveness: each arm is either taken or skipped in the usual way. |
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'd expect exhaustiveness checks to work like with normal guards: they don't play into exhaustiveness checks at all.
src/doc/if-let-guard-review.md
Outdated
In effect, the drop semantics are the same as for an ordinary match guard or an `if let` in an `if` expression: no unexpected | ||
extension of lifetimes. (In Rust 2024 edition, there is a finer rule that even in `if let` | ||
expressions temporaries drop before the `else` block; but for match guards the effect is that temporaries from the | ||
guard are dropped before the arm body.) |
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 think we should take the approach that we adopt 2024 drop order behaviour on all editions. As it's a green field new feature, we can do that. Because I see that there is edition differences on the example from #104843 adapted to if let guards: there is a broken MIR ICE on 2021, but no ICE on 2024. Edit: had a look again, now it seems to not reproduce any more???
When it comes to #103476 (comment) , I can't reproduce it on the 2021 edition.
I'd also check if the lints for bindings properly visit if let guards: they are a new concept, so maybe not all lints are adapted to them. E.g. do we check for irrefutable tails via the |
Also, from experience from let chains and let else we should really ensure that we get the drop order right. I think if let guards are a bit more rarely to be used than former two features, so users won't be doing as much experimentation with it. On the other hand, having done both let chains and let else gave us some experience around drop order which we can apply to if let guards as well (tests in the testsuite as a minimum). As a procedural note, even though I have responded, PRs adding documents are generally not the place the Rust community has discussions. Either one can discuss it in https://internals.rust-lang.org/ or on zulip. But it's also fine to file a stabilization PR right away, at the cost that it might be open for years :). |
@est31 Thanks! I actually have quite a bit of free time right now, so I’ll spend it digging into this further. Also, I completely forgot about the internals forum — you’re right, it would probably be a better place to post my write-up, maybe with a Gist link to the full review My original thinking was to open a PR just to get some feedback on the general state of the feature, and then either close it or follow it up with a proper stabilization PR I’ll check whether the lints behave correctly later today One thing I’m wondering about: how can I enable the 2024 edition drop order behavior in older editions? Maybe it would make sense to rename the current PR to something like “pre-stabilization review” and add a checklist of steps toward stabilization? Would that be a reasonable approach? |
I personally love checklists, they allow organizing things really well. With github checklists though, especially in PRs or issues, people have an expectation that you keep them up to date. So I'd suggest making one but making it clear that it's not the "real" one, just a draft for the actual stabilization report.
I suppose you can take a look how the 2024 drop order was introduced. It should be possible to adjust that code to support special treatment for if-let guards. The most relevant reading in that regard is these two PRs: |
@est31 I'm wondering if we should enable I guess if we can start from stabilizing it in 2024 edition only, then I think it's a good point to open stabilization PR from |
|
if let
pre-stabilization preview
This comment has been minimized.
This comment has been minimized.
It might be that the errors you mentionhave an easy fix. In any case, as I've said before, let's discuss this in a github issue, an internals.rust-lang.org thread, or a zulip thread. Oh and before I forget it, rustfmt support is quite important too. It needs to be tracked for stabilization. |
The job Click to see the possible cause of the failure (guessed by this bot)
|
https://internals.rust-lang.org/t/pre-stabilization-if-let-guards/22901 |
This was moved to https://internals.rust-lang.org/t/pre-stabilization-if-let-guards/22901
r? @est31