Inconsistent rejection of early return of borrow in loops #124254
Labels
A-borrow-checker
Area: The borrow checker
C-bug
Category: This is a bug.
fixed-by-polonius
Compiling with `-Zpolonius` fixes this issue.
T-types
Relevant to the types team, which will review and decide on the PR/issue.
I tried this code:
I expected to see this happen: Code works, prints "pass"
Instead, this happened: Compiler error:
In general, it feels like the borrow checker SHOULD be able to tell that the
return
is divergent and allow the return ofi
which comes from theiter_mut
borrow. The compiler CAN tell that the branch is divergent, as it allows the use of IndexMut to invalidate the old borrow (the first commented solution) for early return.This might be a known limitation of the current borrow checker, however it surprised me and the diagnostics were not particularly helpful, especially as the first commented option DOES work.
Meta
Repros on 1.77.2 stable, as well as 2024-04-21 nightly
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=01c4d55ee75c67771912532d87a69a6d
The text was updated successfully, but these errors were encountered: