I created this topic once I was not able to find either note in documentation or reported bug to this behavior and because it affects both borrow checkers.
It looks like the match guard is extending the lifetime of the children match. Consider this similar code (where I added an extra bool to the Tree variant):
This has a borrow-check error, but if you remove the if true, it compiles.
As such, I suspect that the match guard is extending the lifetime of children to the end of the match block, and also suspect that changing this behavior would be likely to break something. Intuitively it seems like the lifetime of things matched by the pattern should end at the end of the match guard if the match guard is false, but it wouldn't surprise me if that were impossible to implement or contradicted other parts of the language.
An example for the sort of thing I have in mind (this works in current Rust, but might not be quite impossible to reconcile with allowing the original program to borrow-check):
use core::hint::black_box;
#[derive(Debug)]
struct Holder<T>(T);
pub fn main() {
let a = black_box(Holder(Holder(true)));
let t = loop {
match &a {
Holder(a0) if break a0 => { unreachable!() }
Holder(_) => { unreachable!() }
}
};
dbg!(t);
}
In this case, a0 outlives the match (and indeed the loop), although it is possible to make this work by considering t to be a clone (technically copy) of a0 that has a longer lifetime than a0 does.
Edit: Oh, I see you're talking about the polonius alpha that's automatically enabled on nightly. Sorry I kinda didn't have that on my radar at the moment. Apparently there are still differences between that and full -Zpolonius then?
I was trying to make a more typical polonius example for comparison with
but that one does work on nightly without unstable flags.
That does not surprise me, as it avoids the polonius-needing pattern altogether. (I.e. it works on stable, too; as does the same change applied to the handle_value example.)
Yes, indeed, to be honest, I am surprised that on nightly there are three different behaviors of borrow checker:
without polonius argument
-Zpolonius
-Zpolonius=off
In case of issue related to this topic, there is difference between no polonius argument and -Zpolonius: only explicitly enabled polonius -Zpolonius fixes this issue. It is behavior totally opposite to my previous issue with borrow checker, that on nightly, fails only with -Zpolonius=off and compiles without polonius argument or with -Zpolonius.
Asking rustc about it confirms it’s 3 possibilities indeed:
$ rustc +nightly -Zpolonius=
error: incorrect value `` for unstable option `polonius` - either no value or one of `legacy` (the default), `off`, or `next` was expected
Based on testing what works and what doesn't, it looks like currently -Zpolonius is short for -Zpolonius=legacy, and
on nightly, the implicitly enabled default is actually -Zpolonius=next.
Looking at
seems to confirm that it's meant to be a subset
In 2023, a new formulation of a Polonius-style borrow checker was imagined that required minimal rearchitecture of the existing NLL implementation and could be extended to allow more code to compile. […]
We're nearly there now! At this point, there are no known remaining issues with the subset coined Polonius Alpha that we intend to stabilize.
Thank you very much for explanation. It works as you described and it makes sense that subset which requires less architecture changes has behavior more closer to NLL.
then used an LLM to do more minimization work resulting in something similar to f1. Finally, I hand adjusted argument names and order, changed the syntax to achieve the reborrow. Afterwards, all the exploration into f2, f3 etc.. was manual again. ↩︎
certainly the workarounds aren't easily transferable anymore ↩︎