about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlqd <remy.rakic+github@gmail.com>2019-07-16 17:33:16 +0200
committerlqd <remy.rakic+github@gmail.com>2019-07-22 12:46:54 +0200
commit770129c28039ab7501cdc0f2f7ca1a38e22208f0 (patch)
tree8bb67e09b92297cabc1f627fab49aa0a64d4a412
parentd41e002111dd61efa08d996cfc1085fc00472288 (diff)
downloadrust-770129c28039ab7501cdc0f2f7ca1a38e22208f0.tar.gz
rust-770129c28039ab7501cdc0f2f7ca1a38e22208f0.zip
Add test to check that assignments to projections do not kill too many loans
-rw-r--r--src/test/ui/nll/polonius/assignment-to-differing-field.rs50
-rw-r--r--src/test/ui/nll/polonius/assignment-to-differing-field.stderr51
2 files changed, 101 insertions, 0 deletions
diff --git a/src/test/ui/nll/polonius/assignment-to-differing-field.rs b/src/test/ui/nll/polonius/assignment-to-differing-field.rs
new file mode 100644
index 00000000000..c0ba1b983fc
--- /dev/null
+++ b/src/test/ui/nll/polonius/assignment-to-differing-field.rs
@@ -0,0 +1,50 @@
+#![allow(dead_code)]
+
+// Compared to `assignment-kills-loans.rs`, we check here
+// that we do not kill too many borrows. Assignments to the `.1`
+// field projections should leave the borrows on `.0` intact.
+
+// compile-flags: -Z borrowck=mir -Z polonius
+// ignore-compare-mode-nll
+
+struct List<T> {
+    value: T,
+    next: Option<Box<List<T>>>,
+}
+
+
+fn assignment_to_field_projection<'a, T>(
+    mut list: (&'a mut List<T>, &'a mut List<T>),
+) -> Vec<&'a mut T> {
+    let mut result = vec![];
+    loop {
+        result.push(&mut (list.0).value);
+        //~^ ERROR cannot borrow `list.0.value` as mutable
+
+        if let Some(n) = (list.0).next.as_mut() {
+        //~^ ERROR cannot borrow `list.0.next` as mutable
+            list.1 = n;
+        } else {
+            return result;
+        }
+    }
+}
+
+fn assignment_through_projection_chain<'a, T>(
+    mut list: (((((Box<&'a mut List<T>>, Box<&'a mut List<T>>),),),),),
+) -> Vec<&'a mut T> {
+    let mut result = vec![];
+    loop {
+        result.push(&mut ((((list.0).0).0).0).0.value);
+        //~^ ERROR cannot borrow `list.0.0.0.0.0.value` as mutable
+
+        if let Some(n) = ((((list.0).0).0).0).0.next.as_mut() {
+        //~^ ERROR cannot borrow `list.0.0.0.0.0.next` as mutable
+            *((((list.0).0).0).0).1 = n;
+        } else {
+            return result;
+        }
+    }
+}
+
+fn main() {}
diff --git a/src/test/ui/nll/polonius/assignment-to-differing-field.stderr b/src/test/ui/nll/polonius/assignment-to-differing-field.stderr
new file mode 100644
index 00000000000..07ca021b53b
--- /dev/null
+++ b/src/test/ui/nll/polonius/assignment-to-differing-field.stderr
@@ -0,0 +1,51 @@
+error[E0499]: cannot borrow `list.0.value` as mutable more than once at a time
+  --> $DIR/assignment-to-differing-field.rs:21:21
+   |
+LL | fn assignment_to_field_projection<'a, T>(
+   |                                   -- lifetime `'a` defined here
+...
+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.0.value` is borrowed for `'a`
+
+error[E0499]: cannot borrow `list.0.next` as mutable more than once at a time
+  --> $DIR/assignment-to-differing-field.rs:24:26
+   |
+LL | fn assignment_to_field_projection<'a, T>(
+   |                                   -- lifetime `'a` defined here
+...
+LL |         if let Some(n) = (list.0).next.as_mut() {
+   |                          ^^^^^^^^^^^^^---------
+   |                          |
+   |                          mutable borrow starts here in previous iteration of loop
+   |                          argument requires that `list.0.next` is borrowed for `'a`
+
+error[E0499]: cannot borrow `list.0.0.0.0.0.value` as mutable more than once at a time
+  --> $DIR/assignment-to-differing-field.rs:38:21
+   |
+LL | fn assignment_through_projection_chain<'a, T>(
+   |                                        -- lifetime `'a` defined here
+...
+LL |         result.push(&mut ((((list.0).0).0).0).0.value);
+   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow starts here in previous iteration of loop
+...
+LL |             return result;
+   |                    ------ returning this value requires that `list.0.0.0.0.0.value` is borrowed for `'a`
+
+error[E0499]: cannot borrow `list.0.0.0.0.0.next` as mutable more than once at a time
+  --> $DIR/assignment-to-differing-field.rs:41:26
+   |
+LL | fn assignment_through_projection_chain<'a, T>(
+   |                                        -- lifetime `'a` defined here
+...
+LL |         if let Some(n) = ((((list.0).0).0).0).0.next.as_mut() {
+   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^---------
+   |                          |
+   |                          mutable borrow starts here in previous iteration of loop
+   |                          argument requires that `list.0.0.0.0.0.next` is borrowed for `'a`
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0499`.