Skip to content

Commit ce90cd6

Browse files
committed
clarify NLL error description
1 parent fe24db7 commit ce90cd6

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

posts/inside-rust/2023-09-25-polonius-update.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ fn main() {
5252
}
5353
```
5454

55-
Here, the loan created on iteration N _may be used_ in the `None` path on iteration N+1, however the borrowed variable at that iteration is not the same.
55+
The current borrow checker rejects this code. It does this because it sees that there is a loan of `temp` to call `temp.maybe_next()`. It also sees that this loan can flow around the loop — in particular, the loan is referenced by `v`, which is then stored into `temp`. Therefore, it reports an error when we attempt to mutably borrow `temp` at the top of the loop, since there may be a loan still existing from a prior iteration.
56+
57+
*However*, a more careful read shows that, while the borrow may flow around the loop, it only does so on the `Some` path, and on that path `temp` is overwritten. This means that the `temp` we will be borrowing on the next iteration is in fact a different location than the one we borrowed on the previous iteration. On the `None` path, meanwhile, the loan ends.
58+
59+
This kind of "case by case" distinguishing — seeing that the flow only occurs on one path, and that on that path there is a reassignment, requires more precision than the current borrow checker can achieve.
5660

5761
Issues like "NLL problem case #3", issue #47680 and others, were therefore deferred from NLLs, and left as future work, [Polonius](http://smallcultfollowing.com/babysteps/blog/2018/04/27/an-alias-based-formulation-of-the-borrow-checker/).
5862

0 commit comments

Comments
 (0)