Skip to content
Permalink
Browse files

Add test checking our behavior for assigning over a `ConstIndex` proj…

…ection.
  • Loading branch information...
pnkfelix committed Jun 21, 2019
1 parent 3b8ef01 commit f483269625f4f8f0f73bb3dc35986894fc51248a
@@ -0,0 +1,32 @@
// Issue #62007: assigning over a const-index projection of an array
// (in this case, `list[I] = n;`) should in theory be able to kill all borrows
// of `list[0]`, so that `list[0]` could be borrowed on the next
// iteration through the loop.
//
// Currently the compiler does not allow this. We may want to consider
// loosening that restriction in the future. (However, doing so would
// at *least* require T-lang team approval, and probably an RFC; e.g.
// such loosening might make complicate the user's mental mode; it
// also would make code more brittle in the face of refactorings that
// replace constants with variables.

#![allow(dead_code)]

struct List<T> {
value: T,
next: Option<Box<List<T>>>,
}

fn to_refs<T>(mut list: [&mut List<T>; 2]) -> Vec<&mut T> {
let mut result = vec![];
loop {
result.push(&mut list[0].value); //~ ERROR cannot borrow `list[_].value` as mutable
if let Some(n) = list[0].next.as_mut() { //~ ERROR cannot borrow `list[_].next` as mutable
list[0] = n;
} else {
return result;
}
}
}

fn main() {}
@@ -0,0 +1,27 @@
error[E0499]: cannot borrow `list[_].value` as mutable more than once at a time
--> $DIR/issue-62007-assign-const-index.rs:23:21
|
LL | fn to_refs<T>(mut list: [&mut List<T>; 2]) -> Vec<&mut T> {
| - let's call the lifetime of this reference `'1`
...
LL | result.push(&mut list[0].value);
| ^^^^^^^^^^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
LL | return result;
| ------ returning this value requires that `list[_].value` is borrowed for `'1`

error[E0499]: cannot borrow `list[_].next` as mutable more than once at a time
--> $DIR/issue-62007-assign-const-index.rs:24:26
|
LL | fn to_refs<T>(mut list: [&mut List<T>; 2]) -> Vec<&mut T> {
| - let's call the lifetime of this reference `'1`
...
LL | if let Some(n) = list[0].next.as_mut() {
| ^^^^^^^^^^^^---------
| |
| mutable borrow starts here in previous iteration of loop
| argument requires that `list[_].next` is borrowed for `'1`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0499`.

0 comments on commit f483269

Please sign in to comment.
You can’t perform that action at this time.