Skip to content

Commit 5878b5e

Browse files
author
Cameron Zwarich
committed
Add new tests for uses of mutably borrowed paths
1 parent d7de4e9 commit 5878b5e

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct A { a: int, b: int }
12+
struct B { a: int, b: Box<int> }
13+
14+
fn var_copy_after_var_borrow() {
15+
let mut x: int = 1;
16+
let p = &mut x;
17+
drop(x); //~ ERROR cannot use `x` because it was mutably borrowed
18+
*p = 2;
19+
}
20+
21+
fn var_copy_after_field_borrow() {
22+
let mut x = A { a: 1, b: 2 };
23+
let p = &mut x.a;
24+
drop(x); //~ ERROR cannot use `x` because it was mutably borrowed
25+
*p = 3;
26+
}
27+
28+
fn field_copy_after_var_borrow() {
29+
let mut x = A { a: 1, b: 2 };
30+
let p = &mut x;
31+
drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed
32+
p.a = 3;
33+
}
34+
35+
fn field_copy_after_field_borrow() {
36+
let mut x = A { a: 1, b: 2 };
37+
let p = &mut x.a;
38+
drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed
39+
*p = 3;
40+
}
41+
42+
fn fu_field_copy_after_var_borrow() {
43+
let mut x = A { a: 1, b: 2 };
44+
let p = &mut x;
45+
let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed
46+
drop(y);
47+
p.a = 4;
48+
}
49+
50+
fn fu_field_copy_after_field_borrow() {
51+
let mut x = A { a: 1, b: 2 };
52+
let p = &mut x.a;
53+
let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed
54+
drop(y);
55+
*p = 4;
56+
}
57+
58+
fn var_deref_after_var_borrow() {
59+
let mut x: Box<int> = box 1;
60+
let p = &mut x;
61+
drop(*x); //~ ERROR cannot use `*x` because it was mutably borrowed
62+
**p = 2;
63+
}
64+
65+
fn field_deref_after_var_borrow() {
66+
let mut x = B { a: 1, b: box 2 };
67+
let p = &mut x;
68+
drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed
69+
p.a = 3;
70+
}
71+
72+
fn field_deref_after_field_borrow() {
73+
let mut x = B { a: 1, b: box 2 };
74+
let p = &mut x.b;
75+
drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed
76+
**p = 3;
77+
}
78+
79+
fn main() {
80+
var_copy_after_var_borrow();
81+
var_copy_after_field_borrow();
82+
83+
field_copy_after_var_borrow();
84+
field_copy_after_field_borrow();
85+
86+
fu_field_copy_after_var_borrow();
87+
fu_field_copy_after_field_borrow();
88+
89+
var_deref_after_var_borrow();
90+
field_deref_after_var_borrow();
91+
field_deref_after_field_borrow();
92+
}
93+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct A { a: int, b: Box<int> }
12+
13+
fn field_copy_after_field_borrow() {
14+
let mut x = A { a: 1, b: box 2 };
15+
let p = &mut x.b;
16+
drop(x.a);
17+
**p = 3;
18+
}
19+
20+
fn fu_field_copy_after_field_borrow() {
21+
let mut x = A { a: 1, b: box 2 };
22+
let p = &mut x.b;
23+
let y = A { b: box 3, .. x };
24+
drop(y);
25+
**p = 4;
26+
}
27+
28+
fn field_deref_after_field_borrow() {
29+
let mut x = A { a: 1, b: box 2 };
30+
let p = &mut x.a;
31+
drop(*x.b);
32+
*p = 3;
33+
}
34+
35+
fn field_move_after_field_borrow() {
36+
let mut x = A { a: 1, b: box 2 };
37+
let p = &mut x.a;
38+
drop(x.b);
39+
*p = 3;
40+
}
41+
42+
fn fu_field_move_after_field_borrow() {
43+
let mut x = A { a: 1, b: box 2 };
44+
let p = &mut x.a;
45+
let y = A { a: 3, .. x };
46+
drop(y);
47+
*p = 4;
48+
}
49+
50+
fn main() {
51+
field_copy_after_field_borrow();
52+
fu_field_copy_after_field_borrow();
53+
field_deref_after_field_borrow();
54+
field_move_after_field_borrow();
55+
fu_field_move_after_field_borrow();
56+
}
57+

0 commit comments

Comments
 (0)