Code change:
This bug has already been identified but I wanted to open an issue for it. The following code should print first. () I believe there is an unanswered question: Should temporaries be dropped before the block? said this issue is being investigated at <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --This issue has been assigned to via . <!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
assign (I'm pretty sure you are working on this at )
I think they should, since this would be consistent with if/else. ()
Originally reported in: There are also some variations of it: I've made a little test case for it that doesn't rely on printf but instead can be ran as a run-pass test:
I believe the two tests in shall be comparing to . The reason is that actually extends the temporary to the end of the block at the moment, which is the desired outcome. cc
good point. What's important is that both the let-else and let-else free version are doing the same thing. Maybe one can add an additional block around the and then add a second to ensure it dropped after the block terminated? Edit: nvm, it's not possible as the .stdout files are for compiler output, not runtime output of the tests.
<details<summaryHere's another way to test, using Rc</summary</details
It might also be an idea to add a test for the inconsistency between if and if let: (no matter which side of the divide let else is on, make sure that there is no breakage of it in the future)
The above test for "drop before else block" failed on master. Reopening.
Allow me to notify that I am currently looking at how to drop values earlier inside the block.
So I was rebasing onto latest master and I wanted to share this as part of the diff: ! There has been great improvement by but it was no full fix, especially this still post-drops: prints: If you remove the it works now thanks to .
I think this is because the borrowed is assigned with extended lifetime. only breaks out towards the remainder scope. It turns out this is not sufficient. I will try to break out to further one scope up, say the parent remainder scope of the current one. I feel that this should suffice.
This is correct behavior I think. Anything accessible in the pattern needs to be in scope till the end of the block. Consider you can write . It shouldn't depend on whether the pattern matches since that is not known at compile time generally.
yeah it may not be dropped before the code after the construct terminates, but it can be dropped before the block.
I see. I think this is the non-temporary case, when the pattern doesn't match.

Related discussion:
text: <ins> // run-pass // // from issue #93951, where borrowck complained the temporary that `foo(&x)` was stored in was to // be dropped sometime after `x` was. It then suggested adding a semicolon that was already there. #![feature(let_else)] use std::fmt::Debug; fn foo<'a>(x: &'a str) -> Result<impl Debug + 'a, ()> { Ok(x) } fn let_else() { let x = String::from("Hey"); let Ok(_) = foo(&x) else { return }; } fn if_let() { let x = String::from("Hey"); let _ = if let Ok(s) = foo(&x) { s } else { return }; } fn main() { let_else(); if_let(); } </ins>