about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-06-14 06:36:49 +0000
committerbors <bors@rust-lang.org>2014-06-14 06:36:49 +0000
commit18c451fc497a23944b7b759cca5ff0b0be1122fd (patch)
tree6d51b100a0e6a6b2f1b6c95b28b36125cacad5e5 /src/test
parent2c6caad1bab0660ce8b4797c10d5530964d6e8d9 (diff)
parent6fc788916c297d6e03464b80f12ba0e62fccccac (diff)
downloadrust-18c451fc497a23944b7b759cca5ff0b0be1122fd.tar.gz
rust-18c451fc497a23944b7b759cca5ff0b0be1122fd.zip
auto merge of #14739 : zwarich/rust/mut-unique-path, r=nikomatsakis
Implement the stronger guarantees for mutable borrows proposed in #12624.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/borrowck-field-sensitivity.rs17
-rw-r--r--src/test/compile-fail/borrowck-use-mut-borrow.rs93
-rw-r--r--src/test/compile-fail/borrowck-vec-pattern-loan-from-mut.rs2
-rw-r--r--src/test/compile-fail/regions-escape-loop-via-vec.rs4
-rw-r--r--src/test/run-pass/borrowck-field-sensitivity.rs16
-rw-r--r--src/test/run-pass/borrowck-use-mut-borrow.rs57
6 files changed, 169 insertions, 20 deletions
diff --git a/src/test/compile-fail/borrowck-field-sensitivity.rs b/src/test/compile-fail/borrowck-field-sensitivity.rs
index 2fa9067af54..72042b8373d 100644
--- a/src/test/compile-fail/borrowck-field-sensitivity.rs
+++ b/src/test/compile-fail/borrowck-field-sensitivity.rs
@@ -84,20 +84,6 @@ fn fu_move_after_fu_move() {
 
 // The following functions aren't yet accepted, but they should be.
 
-fn move_after_borrow_correct() {
-    let x = A { a: 1, b: box 2 };
-    let p = &x.a;
-    drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed
-    drop(*p);
-}
-
-fn fu_move_after_borrow_correct() {
-    let x = A { a: 1, b: box 2 };
-    let p = &x.a;
-    let _y = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
-    drop(*p);
-}
-
 fn copy_after_field_assign_after_uninit() {
     let mut x: A;
     x.a = 1;
@@ -132,9 +118,6 @@ fn main() {
     fu_move_after_move();
     fu_move_after_fu_move();
 
-    move_after_borrow_correct();
-    fu_move_after_borrow_correct();
-
     copy_after_field_assign_after_uninit();
     borrow_after_field_assign_after_uninit();
     move_after_field_assign_after_uninit();
diff --git a/src/test/compile-fail/borrowck-use-mut-borrow.rs b/src/test/compile-fail/borrowck-use-mut-borrow.rs
new file mode 100644
index 00000000000..7414bb930d4
--- /dev/null
+++ b/src/test/compile-fail/borrowck-use-mut-borrow.rs
@@ -0,0 +1,93 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct A { a: int, b: int }
+struct B { a: int, b: Box<int> }
+
+fn var_copy_after_var_borrow() {
+    let mut x: int = 1;
+    let p = &mut x;
+    drop(x); //~ ERROR cannot use `x` because it was mutably borrowed
+    *p = 2;
+}
+
+fn var_copy_after_field_borrow() {
+    let mut x = A { a: 1, b: 2 };
+    let p = &mut x.a;
+    drop(x); //~ ERROR cannot use `x` because it was mutably borrowed
+    *p = 3;
+}
+
+fn field_copy_after_var_borrow() {
+    let mut x = A { a: 1, b: 2 };
+    let p = &mut x;
+    drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed
+    p.a = 3;
+}
+
+fn field_copy_after_field_borrow() {
+    let mut x = A { a: 1, b: 2 };
+    let p = &mut x.a;
+    drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed
+    *p = 3;
+}
+
+fn fu_field_copy_after_var_borrow() {
+    let mut x = A { a: 1, b: 2 };
+    let p = &mut x;
+    let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed
+    drop(y);
+    p.a = 4;
+}
+
+fn fu_field_copy_after_field_borrow() {
+    let mut x = A { a: 1, b: 2 };
+    let p = &mut x.a;
+    let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed
+    drop(y);
+    *p = 4;
+}
+
+fn var_deref_after_var_borrow() {
+    let mut x: Box<int> = box 1;
+    let p = &mut x;
+    drop(*x); //~ ERROR cannot use `*x` because it was mutably borrowed
+    **p = 2;
+}
+
+fn field_deref_after_var_borrow() {
+    let mut x = B { a: 1, b: box 2 };
+    let p = &mut x;
+    drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed
+    p.a = 3;
+}
+
+fn field_deref_after_field_borrow() {
+    let mut x = B { a: 1, b: box 2 };
+    let p = &mut x.b;
+    drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed
+    **p = 3;
+}
+
+fn main() {
+    var_copy_after_var_borrow();
+    var_copy_after_field_borrow();
+
+    field_copy_after_var_borrow();
+    field_copy_after_field_borrow();
+
+    fu_field_copy_after_var_borrow();
+    fu_field_copy_after_field_borrow();
+
+    var_deref_after_var_borrow();
+    field_deref_after_var_borrow();
+    field_deref_after_field_borrow();
+}
+
diff --git a/src/test/compile-fail/borrowck-vec-pattern-loan-from-mut.rs b/src/test/compile-fail/borrowck-vec-pattern-loan-from-mut.rs
index 393ec8b0b1b..ff029ce624c 100644
--- a/src/test/compile-fail/borrowck-vec-pattern-loan-from-mut.rs
+++ b/src/test/compile-fail/borrowck-vec-pattern-loan-from-mut.rs
@@ -12,7 +12,7 @@ fn a() {
     let mut v = vec!(1, 2, 3);
     let vb: &mut [int] = v.as_mut_slice();
     match vb {
-        [_a, ..tail] => {
+        [_a, ..tail] => { //~ ERROR cannot use `vb[..]` because it was mutably borrowed
             v.push(tail[0] + tail[1]); //~ ERROR cannot borrow
         }
         _ => {}
diff --git a/src/test/compile-fail/regions-escape-loop-via-vec.rs b/src/test/compile-fail/regions-escape-loop-via-vec.rs
index 7d9629a5220..89350f16167 100644
--- a/src/test/compile-fail/regions-escape-loop-via-vec.rs
+++ b/src/test/compile-fail/regions-escape-loop-via-vec.rs
@@ -12,8 +12,8 @@
 fn broken() {
     let mut x = 3;
     let mut _y = vec!(&mut x);
-    while x < 10 {
-        let mut z = x;
+    while x < 10 { //~ ERROR cannot use `x` because it was mutably borrowed
+        let mut z = x; //~ ERROR cannot use `x` because it was mutably borrowed
         _y.push(&mut z); //~ ERROR `z` does not live long enough
         x += 1; //~ ERROR cannot assign
     }
diff --git a/src/test/run-pass/borrowck-field-sensitivity.rs b/src/test/run-pass/borrowck-field-sensitivity.rs
index a297300daf1..33be47e504b 100644
--- a/src/test/run-pass/borrowck-field-sensitivity.rs
+++ b/src/test/run-pass/borrowck-field-sensitivity.rs
@@ -73,6 +73,20 @@ fn borrow_after_fu_move() {
     drop(*p);
 }
 
+fn move_after_borrow() {
+    let x = A { a: 1, b: box 2 };
+    let p = &x.a;
+    drop(x.b);
+    drop(*p);
+}
+
+fn fu_move_after_borrow() {
+    let x = A { a: 1, b: box 2 };
+    let p = &x.a;
+    let _y = A { a: 3, .. x };
+    drop(*p);
+}
+
 fn mut_borrow_after_mut_borrow() {
     let mut x = A { a: 1, b: box 2 };
     let p = &mut x.a;
@@ -225,6 +239,8 @@ fn main() {
 
     borrow_after_move();
     borrow_after_fu_move();
+    move_after_borrow();
+    fu_move_after_borrow();
     mut_borrow_after_mut_borrow();
 
     move_after_move();
diff --git a/src/test/run-pass/borrowck-use-mut-borrow.rs b/src/test/run-pass/borrowck-use-mut-borrow.rs
new file mode 100644
index 00000000000..cbfdd5961ff
--- /dev/null
+++ b/src/test/run-pass/borrowck-use-mut-borrow.rs
@@ -0,0 +1,57 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+struct A { a: int, b: Box<int> }
+
+fn field_copy_after_field_borrow() {
+    let mut x = A { a: 1, b: box 2 };
+    let p = &mut x.b;
+    drop(x.a);
+    **p = 3;
+}
+
+fn fu_field_copy_after_field_borrow() {
+    let mut x = A { a: 1, b: box 2 };
+    let p = &mut x.b;
+    let y = A { b: box 3, .. x };
+    drop(y);
+    **p = 4;
+}
+
+fn field_deref_after_field_borrow() {
+    let mut x = A { a: 1, b: box 2 };
+    let p = &mut x.a;
+    drop(*x.b);
+    *p = 3;
+}
+
+fn field_move_after_field_borrow() {
+    let mut x = A { a: 1, b: box 2 };
+    let p = &mut x.a;
+    drop(x.b);
+    *p = 3;
+}
+
+fn fu_field_move_after_field_borrow() {
+    let mut x = A { a: 1, b: box 2 };
+    let p = &mut x.a;
+    let y = A { a: 3, .. x };
+    drop(y);
+    *p = 4;
+}
+
+fn main() {
+    field_copy_after_field_borrow();
+    fu_field_copy_after_field_borrow();
+    field_deref_after_field_borrow();
+    field_move_after_field_borrow();
+    fu_field_move_after_field_borrow();
+}
+