Closed
Description
I expected this example from the NLL RFC to pass the MIR borrow checker:
struct List<T> {
value: T,
next: Option<Box<List<T>>>,
}
fn to_refs<T>(mut list: &mut List<T>) -> Vec<&mut T> {
let mut result = vec![];
loop {
result.push(&mut list.value);
if let Some(n) = list.next.as_mut() {
list = n;
} else {
return result;
}
}
}
fn main() {
}
The idea here is that assigning to list
is supposed to clear the existing borrows of list.{value,next}
. However, it still gets errors for me:
lunch-box. rustc --stage1 iterate-mut-ref.rs -Zborrowck=mir
error[E0499]: cannot borrow `list.value` as mutable more than once at a time
--> iterate-mut-ref.rs:9:21
|
9 | result.push(&mut list.value);
| ^^^^^^^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
16 | }
| - mutable borrow ends here
error[E0499]: cannot borrow `list.next` as mutable more than once at a time
--> iterate-mut-ref.rs:10:26
|
10 | if let Some(n) = list.next.as_mut() {
| ^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
16 | }
| - mutable borrow ends here
error[E0506]: cannot assign to `list` because it is borrowed
--> iterate-mut-ref.rs:11:13
|
9 | result.push(&mut list.value);
| --------------- borrow of `list` occurs here
10 | if let Some(n) = list.next.as_mut() {
11 | list = n;
| ^^^^^^^^ assignment to borrowed `list` occurs here
error: aborting due to 3 previous errors
Metadata
Metadata
Assignees
Labels
Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlArea: Non-lexical lifetimes (NLL)Area: Messages for errors, warnings, and lintsCategory: This is a bug.Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️Working towards the "valid code works" goalMedium priorityRelevant to the compiler team, which will review and decide on the PR/issue.