]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/issue-62007-assign-const-index.rs
Rollup merge of #105216 - GuillaumeGomez:rm-unused-gui-test, r=notriddle
[rust.git] / src / test / ui / nll / issue-62007-assign-const-index.rs
1 // Issue #62007: assigning over a const-index projection of an array
2 // (in this case, `list[I] = n;`) should in theory be able to kill all borrows
3 // of `list[0]`, so that `list[0]` could be borrowed on the next
4 // iteration through the loop.
5 //
6 // Currently the compiler does not allow this. We may want to consider
7 // loosening that restriction in the future. (However, doing so would
8 // at *least* require T-lang team approval, and probably an RFC; e.g.
9 // such loosening might make complicate the user's mental mode; it
10 // also would make code more brittle in the face of refactorings that
11 // replace constants with variables.
12
13 #![allow(dead_code)]
14
15 struct List<T> {
16     value: T,
17     next: Option<Box<List<T>>>,
18 }
19
20 fn to_refs<T>(mut list: [&mut List<T>; 2]) -> Vec<&mut T> {
21     let mut result = vec![];
22     loop {
23         result.push(&mut list[0].value); //~ ERROR cannot borrow `list[_].value` as mutable
24         if let Some(n) = list[0].next.as_mut() { //~ ERROR cannot borrow `list[_].next` as mutable
25             list[0] = n;
26         } else {
27             return result;
28         }
29     }
30 }
31
32 fn main() {}