Skip to content

Commit 5d4d09d

Browse files
author
Cameron Zwarich
committed
Add a new test for borrow checker Box<T> behavior
1 parent 3607c7a commit 5d4d09d

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 {
12+
x: Box<int>,
13+
y: int,
14+
}
15+
16+
struct B {
17+
x: Box<int>,
18+
y: Box<int>,
19+
}
20+
21+
struct C {
22+
x: Box<A>,
23+
y: int,
24+
}
25+
26+
struct D {
27+
x: Box<A>,
28+
y: Box<int>,
29+
}
30+
31+
fn copy_after_move() {
32+
let a = box A { x: box 0, y: 1 };
33+
let _x = a.x;
34+
let _y = a.y; //~ ERROR use of partially moved
35+
}
36+
37+
fn move_after_move() {
38+
let a = box B { x: box 0, y: box 1 };
39+
let _x = a.x;
40+
let _y = a.y; //~ ERROR use of partially moved
41+
}
42+
43+
fn borrow_after_move() {
44+
let a = box A { x: box 0, y: 1 };
45+
let _x = a.x;
46+
let _y = &a.y; //~ ERROR use of partially moved
47+
}
48+
49+
fn move_after_borrow() {
50+
let a = box B { x: box 0, y: box 1 };
51+
let _x = &a.x;
52+
let _y = a.y; //~ ERROR cannot move
53+
}
54+
55+
fn copy_after_mut_borrow() {
56+
let mut a = box A { x: box 0, y: 1 };
57+
let _x = &mut a.x;
58+
let _y = a.y; //~ ERROR cannot use
59+
}
60+
61+
fn move_after_mut_borrow() {
62+
let mut a = box B { x: box 0, y: box 1 };
63+
let _x = &mut a.x;
64+
let _y = a.y; //~ ERROR cannot move
65+
}
66+
67+
fn borrow_after_mut_borrow() {
68+
let mut a = box A { x: box 0, y: 1 };
69+
let _x = &mut a.x;
70+
let _y = &a.y; //~ ERROR cannot borrow
71+
}
72+
73+
fn mut_borrow_after_borrow() {
74+
let mut a = box A { x: box 0, y: 1 };
75+
let _x = &a.x;
76+
let _y = &mut a.y; //~ ERROR cannot borrow
77+
}
78+
79+
fn copy_after_move_nested() {
80+
let a = box C { x: box A { x: box 0, y: 1 }, y: 2 };
81+
let _x = a.x.x;
82+
let _y = a.y; //~ ERROR use of partially moved
83+
}
84+
85+
fn move_after_move_nested() {
86+
let a = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
87+
let _x = a.x.x;
88+
let _y = a.y; //~ ERROR use of partially moved
89+
}
90+
91+
fn borrow_after_move_nested() {
92+
let a = box C { x: box A { x: box 0, y: 1 }, y: 2 };
93+
let _x = a.x.x;
94+
let _y = &a.y; //~ ERROR use of partially moved
95+
}
96+
97+
fn move_after_borrow_nested() {
98+
let a = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
99+
let _x = &a.x.x;
100+
let _y = a.y; //~ ERROR cannot move
101+
}
102+
103+
fn copy_after_mut_borrow_nested() {
104+
let mut a = box C { x: box A { x: box 0, y: 1 }, y: 2 };
105+
let _x = &mut a.x.x;
106+
let _y = a.y; //~ ERROR cannot use
107+
}
108+
109+
fn move_after_mut_borrow_nested() {
110+
let mut a = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
111+
let _x = &mut a.x.x;
112+
let _y = a.y; //~ ERROR cannot move
113+
}
114+
115+
fn borrow_after_mut_borrow_nested() {
116+
let mut a = box C { x: box A { x: box 0, y: 1 }, y: 2 };
117+
let _x = &mut a.x.x;
118+
let _y = &a.y; //~ ERROR cannot borrow
119+
}
120+
121+
fn mut_borrow_after_borrow_nested() {
122+
let mut a = box C { x: box A { x: box 0, y: 1 }, y: 2 };
123+
let _x = &a.x.x;
124+
let _y = &mut a.y; //~ ERROR cannot borrow
125+
}
126+
127+
fn main() {
128+
copy_after_move();
129+
move_after_move();
130+
borrow_after_move();
131+
132+
move_after_borrow();
133+
134+
copy_after_mut_borrow();
135+
move_after_mut_borrow();
136+
borrow_after_mut_borrow();
137+
mut_borrow_after_borrow();
138+
139+
copy_after_move_nested();
140+
move_after_move_nested();
141+
borrow_after_move_nested();
142+
143+
move_after_borrow_nested();
144+
145+
copy_after_mut_borrow_nested();
146+
move_after_mut_borrow_nested();
147+
borrow_after_mut_borrow_nested();
148+
mut_borrow_after_borrow_nested();
149+
}
150+

0 commit comments

Comments
 (0)