You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by Andrew Ares, wu aoxiang, Takayuki Maeda, Kyle L. Davis, Matthias Beyer, Rageking8, Kazurin Nanako, zohnannor, Nahua, JSDurand and 10 more
Reacted by Michael Goulet, Téo Orthlieb, Constantin GIERCZAK--GALLE, Pâris DOUADY, waffle, erwanvivien, ilya sheprut, Grégoire Geis, SeniorMars, PersonBelowRocks and 13 more
Reacted by Daniel Henry-Mantilla, André Vennberg, asquared31415, Jakub Beránek, Juxhin, amy null, bb010g, William, Téo Orthlieb, Yi-Jyun Pan and 24 more
Yeah, that definitely looks wrong; we will have to roll this back and try again for 1.65.
Reacted by Jan Riemer, Erich Gubler, seanjensengrey, Nico Mürdter, Pâris DOUADY, undersquire, Filip T, Luca Barbato, PersonBelowRocks, Scott Steele and 23 more
Reacted by kkocdko, İrem, Sycration, flamion, Rageking8, zohnannor, TonalidadeHidrica, Y0GAAAA and Eric Yu
@joshtriplett given rust-lang/rustfmt#5203 (comment) , can we use this opportunity to also ensure that there is a style decision put forth by the lang team in place before restabilizing so that rustfmt is able to merge formatting for let_chains?
Now that the first nightly with #102998 is out, I've tried running again the snippet from above. The snippet now prints:
1
Dropped! 1.5
Dropped! first
Dropped! last
2
3
This is not 100% what the desugaring from the RFC prints (should be "first", "1.5", "last"). It's closer than what is seen at the top of the issue's thread, but not quite it. Over at #102998 , @cjgillot has brought up similar concerns.
Although actually I have to say that my desugaring might be be fully correct. I split up a normal && into two nested ifs which is probably wrong. If it is let together, we should the current output.
Yeah it doesn't have to match it 100%, one always deviate in the end from the RFC. What is important is that the details are well understood and approved. #102998 has greatly improved the state but we aren't there yet I think.
What is currently implemented
So in order to figure out what the current desugaring is doing, I've extended the drop_order.rs test that you added:
So nested if let's drop in reverse definition order after their body. On the other hand, let-free ifs with && drop in reading order, except for the first condition, which gets droped last (and all conditions drop before their body). Let's call this the "twist".
For let chains, the current implementation seems to:
Drop all non-let conditions before the then/else body
Drop let conditions in reverse definition order
Drop non-let conditions mostly in definition order, but with a special version of the twist: if there is a block of non-let conditions right at the start of the if chain, then the first non-let condition is dropped after all other non-let conditions that are part of that leading block, but still before any possible non-let conditions that come after the first let condition.
What behaviour should look like
Personally, I agree a lot with the current implementation, except for that weird exception in rule number 4.
Rules 1 and 3 make sense because you need to keep the bindings around for the stuff that possibly uses them, which can be either the subsequent let conditions or the body. Ideally however we would deviate from if let in that we drop before the else, like if is doing it.
Rule 2 makes sense because ideally you drop stuff as quickly as possible. Same goes for rule 4, except the twist.
My proposal would be to:
Drop all let conditions after the then body, but before the else body
Drop all non-let conditions before the then/else body
Drop let conditions in reverse definition order
Drop non-let conditions in definition order
Changes required from the current implementation on master (as of 2022-10-20):
An alternative proposal would drop everything in reverse order, after the body. This rule would be simpler, but you would keep around the temporaries for way too long.
(Side note: Ideally the twist would also be eliminated from normal if's but I'm not sure if we can do this due to backwards compatibly. This step would make bool-only && chains them associative wrt drop order though, and also if let chains associative, at least when we consider a hypothetical extension that allows ()s in let chains. I'm not sure if there are issues about this, and if not, maybe I should file one Edit: issue filed #103107)
Drop all let conditions after the then body, but before the else body
This bit isn't quite accurate. In the current implementation it's inconsistent -- if the let is leading then it will drop after the else body, but if it is trailing it will drop before the else body. (example, which at time of writing prints 1,2, then 2,1,).
This is the result of the same bug that causes #100276. After #103034, let conditions will always drop after the else body, just like with normal if let (so the aforementioned example would print 1,2, then 1,2,).
Regarding #103034, I wonder if it's possible to change it so that it always drops before else? Keeping around the references for the then block makes sense, but else doesn't need any of them, and it would be nice if you could re-access stuff in the else block that if let was accessing. The same holds for if let too, see #103108 which also contains an example of code that is currently not compiling due to the behaviour of dropping before the else drop order. It might be possible that if let can't be changed any more, but it would be great IMO if let chains would start with the better behaviour.
Activity
Noratrieb commentedon Aug 13, 2022
This also causes issues like #99852 and #100276
joshtriplett commentedon Aug 14, 2022
Yeah, that definitely looks wrong; we will have to roll this back and try again for 1.65.
Rollup merge of rust-lang#100526 - Nilstrieb:tests!, r=Mark-Simulacrum
Noratrieb commentedon Aug 14, 2022
I've opened a zulip thread about fixing them. cc @c410-f3r, I wasn't able to find you on zulip to ping you there
Noratrieb commentedon Aug 14, 2022
That was a misclick 😆
Manishearth commentedon Aug 15, 2022
@joshtriplett given rust-lang/rustfmt#5203 (comment) , can we use this opportunity to also ensure that there is a style decision put forth by the lang team in place before restabilizing so that rustfmt is able to merge formatting for let_chains?
7 remaining items
nikomatsakis commentedon Sep 13, 2022
Discussed in lang-team meeting. We are removing the nomination. The current behavior seems clearly surprising and also not in accordance with the RFC.
nathanwhit commentedon Oct 13, 2022
@rustbot claim
Rollup merge of rust-lang#102998 - nathanwhit:let-chains-drop-order, …
est31 commentedon Oct 16, 2022
Now that the first nightly with #102998 is out, I've tried running again the snippet from above. The snippet now prints:
This is not 100% what the desugaring from the RFC prints (should be "first", "1.5", "last"). It's closer than what is seen at the top of the issue's thread, but not quite it. Over at #102998 , @cjgillot has brought up similar concerns.
So maybe this issue should be reopened?
Noratrieb commentedon Oct 16, 2022
I agree, I think this issue should only be closed once T-lang decided on a desired drop order and the compiler implements it.
Noratrieb commentedon Oct 16, 2022
Although actually I have to say that my desugaring might be be fully correct. I split up a normal
&&into two nested ifs which is probably wrong. If it is let together, we should the current output.est31 commentedon Oct 16, 2022
Yeah it doesn't have to match it 100%, one always deviate in the end from the RFC. What is important is that the details are well understood and approved. #102998 has greatly improved the state but we aren't there yet I think.
What is currently implemented
So in order to figure out what the current desugaring is doing, I've extended the drop_order.rs test that you added:
Details
And:
Compare this with normal if chains:
And nested ifs:
So nested if let's drop in reverse definition order after their body. On the other hand, let-free ifs with && drop in reading order, except for the first condition, which gets droped last (and all conditions drop before their body). Let's call this the "twist".
For let chains, the current implementation seems to:
but before the else body (edit: currently leading let conditions will always drop after the else as @nathanwhit points out below), and also after the else body (as of Let expressions on RHS shouldn't be terminating scopes #103034)What behaviour should look like
Personally, I agree a lot with the current implementation, except for that weird exception in rule number 4.
Rules 1 and 3 make sense because you need to keep the bindings around for the stuff that possibly uses them, which can be either the subsequent let conditions or the body. Ideally however we would deviate from if let in that we drop before the
else, likeifis doing it.Rule 2 makes sense because ideally you drop stuff as quickly as possible. Same goes for rule 4, except the twist.
My proposal would be to:
Changes required from the current implementation on master (as of 2022-10-20):
What do you think?
An alternative proposal would drop everything in reverse order, after the body. This rule would be simpler, but you would keep around the temporaries for way too long.
(Side note: Ideally the twist would also be eliminated from normal if's but I'm not sure if we can do this due to backwards compatibly. This step would make bool-only && chains them associative wrt drop order though, and also if let chains associative, at least when we consider a hypothetical extension that allows ()s in let chains.
I'm not sure if there are issues about this, and if not, maybe I should file oneEdit: issue filed #103107)nathanwhit commentedon Oct 16, 2022
One note:
This bit isn't quite accurate. In the current implementation it's inconsistent -- if the
letis leading then it will drop after theelsebody, but if it is trailing it will drop before theelsebody. (example, which at time of writing prints1,2,then2,1,).This is the result of the same bug that causes #100276. After #103034,
letconditions will always drop after theelsebody, just like with normalif let(so the aforementioned example would print1,2,then1,2,).est31 commentedon Oct 16, 2022
@nathanwhit thanks! I've edited my comment.
Regarding #103034, I wonder if it's possible to change it so that it always drops before
else? Keeping around the references for thethenblock makes sense, butelsedoesn't need any of them, and it would be nice if you could re-access stuff in theelseblock thatif letwas accessing. The same holds forif lettoo, see #103108 which also contains an example of code that is currently not compiling due to the behaviour of dropping before theelsedrop order. It might be possible thatif letcan't be changed any more, but it would be great IMO if let chains would start with the better behaviour.est31 commentedon Oct 19, 2022
I've filed an issue for let chains else dropping behaviour: #103248
est31 commentedon Oct 20, 2022
PR filed to get rid of the && drop order twist in all Rust: #103293