-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Proposal: stabilize if_let_rescope
for Edition 2024
#131154
Comments
@rustbot labels +T-lang +A-edition-2024 |
Note that I'm not very good at analysing crater run results (not just what broke, but also what happened to the crates that don't build for other reasons), but I think it would be important to have a crater analysis that includes the exact number of breakages and crates that didn't build (or otherwise what the analysis did not cover), and if feasible classification of the nature of breakages. Regarding the lint, as mentioned it's not machine-applicable due to overlapping suggestions not possible in the current fix application infra, but also as is usual with proc-macro detection, any crates that forwards user spans with no distinguishing syntax context will cause |
FCP for stabilization: @rfcbot merge |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
@rfcbot reviewed |
@rfcbot reviewed Copying my reasoning from the meeting notes for posterity: This introduces a disparity between match and if-let, which I don't like, but it moves towards earlier drop, which I do like, and it also means that struct DropOrder;
impl DropOrder {
fn test(&self) -> bool {
false
}
}
impl Drop for DropOrder {
fn drop(&mut self) {
eprintln!("drop");
}
}
fn main() {
if DropOrder.test() {
// ^^^^^^^^^ dropped before entering else
eprintln!("this body");
} else {
eprintln!("else body");
}
} You can see the behavior here, it prints "drop" first. In short, this is moving us closer to the world I want, which is one in which match is able to drop more eagerly. |
@rfcbot reviewed I'm confident in the semantics proposed, given the above reasoning, and that we can make the edition experience good enough for our users, though it might require some dialing in before we ship. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Also, when we document this, we should point out that while it's possible to have surprises with |
@rustbot labels -I-lang-nominated We discussed this in the meeting today... and it's now in FCP. |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Tracked by #124085
Summary
if_let_rescope
is an implementation of the #124085 through #107251. This change did not come with a RFC. This proposal will aim to describe the change to the language fully.if let
and the current unstabilizedif let
-chain has a caveat to the assignment of lifetime rules regarding temporary values generated from initializer expressions.Instead, after stabilizing this, we have this effect.
This will allow us to be more consistent with
let $pat = $expr else { $diverge; }
where temporaries from$expr
are dropped before entering the$diverge
branch in today's language.Given that this is an Edition breaking change, a lint is developed and tested in the field with crater runs.
What is being proposed for stabilization
In #107251, a new kind of scope data
IfThenRescope
is introduced and gated behind the edition and this feature gate. Upon stabilization, this scope will work like a terminating scope for variables and temporary values generated from thelet
-bindings.What is more significant is the case where
if let
is used as a non-trivial value-producing subexpression in a larger evaluation context, in which it will have semantic impact to lifetimes and object drop orders.Here are some breaking changes that will be introduced by stabilization.
A marginal amount of crates are discovered by previous crater runs in which this change leads to rejection from the borrow checker, because
droppy()
does not live long enough.The migration lint that is now implemented on
master
suggests a rewrite to restore the old semantics perfectly, but not automatically applicable due to the current limitation of machine applicable lints and our internal support for overlapping suggestion spans.A more subtle and silent breakage could be involving use of types with significant drop implementation and synchronization primitives such as
Mutex
es.Although the crater run did not find breakage in runtime behaviour through
cargo test
, we proceed with developing lints to detect those cases and rewrite them intomatch
es. Here the suggestion notes are machine applicable because we can coalesce the overlapping spans in one pass.In the latest crater run we found a marginal population of crates that are truly impacted by this lint, revealing corner cases with the lint which are now properly handled and tested.
Future interactions
Currently there is another possible use of pattern matching in control flows that complements
if let
chain is theis
operator proposed in rust-lang/rfcs#3573. This change both runs along the same principle of no unnecessary binding and long lifetime beyond the applicable scope from a pattern-matching predicate. With this change, the implementation ofis
would be believably less error-prone due to the unexpectedly long lifetimes.This leaves
match
being the only exception to this principle. In fact, our mitigation and migration strategy is based on it. It might remains as an acceptable exception and further be publicized and inadvertently advertised as such.In general, this change is not expected to influence or hinder major language design.
The text was updated successfully, but these errors were encountered: