File tree 2 files changed +59
-0
lines changed
2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
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 ( ) { }
Original file line number Diff line number Diff line change
1
+ error[E0499]: cannot borrow `list[_].value` as mutable more than once at a time
2
+ --> $DIR/issue-62007-assign-const-index.rs:23:21
3
+ |
4
+ LL | fn to_refs<T>(mut list: [&mut List<T>; 2]) -> Vec<&mut T> {
5
+ | - let's call the lifetime of this reference `'1`
6
+ ...
7
+ LL | result.push(&mut list[0].value);
8
+ | ^^^^^^^^^^^^^^^^^^ mutable borrow starts here in previous iteration of loop
9
+ ...
10
+ LL | return result;
11
+ | ------ returning this value requires that `list[_].value` is borrowed for `'1`
12
+
13
+ error[E0499]: cannot borrow `list[_].next` as mutable more than once at a time
14
+ --> $DIR/issue-62007-assign-const-index.rs:24:26
15
+ |
16
+ LL | fn to_refs<T>(mut list: [&mut List<T>; 2]) -> Vec<&mut T> {
17
+ | - let's call the lifetime of this reference `'1`
18
+ ...
19
+ LL | if let Some(n) = list[0].next.as_mut() {
20
+ | ^^^^^^^^^^^^---------
21
+ | |
22
+ | mutable borrow starts here in previous iteration of loop
23
+ | argument requires that `list[_].next` is borrowed for `'1`
24
+
25
+ error: aborting due to 2 previous errors
26
+
27
+ For more information about this error, try `rustc --explain E0499`.
You can’t perform that action at this time.
0 commit comments