]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-box-insensitivity.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / borrowck / borrowck-box-insensitivity.rs
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 #![feature(box_syntax, rustc_attrs)]
12
13 struct A {
14     x: Box<isize>,
15     y: isize,
16 }
17
18 struct B {
19     x: Box<isize>,
20     y: Box<isize>,
21 }
22
23 struct C {
24     x: Box<A>,
25     y: isize,
26 }
27
28 struct D {
29     x: Box<A>,
30     y: Box<isize>,
31 }
32
33 fn copy_after_move() {
34     let a: Box<_> = box A { x: box 0, y: 1 };
35     let _x = a.x;
36     //~^ value moved here
37     let _y = a.y; //~ ERROR use of moved
38     //~^ move occurs because `a.x` has type `std::boxed::Box<isize>`
39     //~| value used here after move
40 }
41
42 fn move_after_move() {
43     let a: Box<_> = box B { x: box 0, y: box 1 };
44     let _x = a.x;
45     //~^ value moved here
46     let _y = a.y; //~ ERROR use of moved
47     //~^ move occurs because `a.x` has type `std::boxed::Box<isize>`
48     //~| value used here after move
49 }
50
51 fn borrow_after_move() {
52     let a: Box<_> = box A { x: box 0, y: 1 };
53     let _x = a.x;
54     //~^ value moved here
55     let _y = &a.y; //~ ERROR use of moved
56     //~^ move occurs because `a.x` has type `std::boxed::Box<isize>`
57     //~| value used here after move
58 }
59
60 fn move_after_borrow() {
61     let a: Box<_> = box B { x: box 0, y: box 1 };
62     let _x = &a.x;
63     let _y = a.y;
64     //~^ ERROR cannot move
65     //~| move out of
66 }
67
68 fn copy_after_mut_borrow() {
69     let mut a: Box<_> = box A { x: box 0, y: 1 };
70     let _x = &mut a.x;
71     let _y = a.y; //~ ERROR cannot use
72 }
73
74 fn move_after_mut_borrow() {
75     let mut a: Box<_> = box B { x: box 0, y: box 1 };
76     let _x = &mut a.x;
77     let _y = a.y;
78     //~^ ERROR cannot move
79     //~| move out of
80 }
81
82 fn borrow_after_mut_borrow() {
83     let mut a: Box<_> = box A { x: box 0, y: 1 };
84     let _x = &mut a.x;
85     let _y = &a.y; //~ ERROR cannot borrow
86     //~^ immutable borrow occurs here (via `a.y`)
87 }
88
89 fn mut_borrow_after_borrow() {
90     let mut a: Box<_> = box A { x: box 0, y: 1 };
91     let _x = &a.x;
92     let _y = &mut a.y; //~ ERROR cannot borrow
93     //~^ mutable borrow occurs here (via `a.y`)
94 }
95
96 fn copy_after_move_nested() {
97     let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
98     let _x = a.x.x;
99     //~^ value moved here
100     let _y = a.y; //~ ERROR use of collaterally moved
101     //~| value used here after move
102 }
103
104 fn move_after_move_nested() {
105     let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
106     let _x = a.x.x;
107     //~^ value moved here
108     let _y = a.y; //~ ERROR use of collaterally moved
109     //~| value used here after move
110 }
111
112 fn borrow_after_move_nested() {
113     let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
114     let _x = a.x.x;
115     //~^ value moved here
116     let _y = &a.y; //~ ERROR use of collaterally moved
117     //~| value used here after move
118 }
119
120 fn move_after_borrow_nested() {
121     let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
122     let _x = &a.x.x;
123     //~^ borrow of `a.x.x` occurs here
124     let _y = a.y;
125     //~^ ERROR cannot move
126     //~| move out of
127 }
128
129 fn copy_after_mut_borrow_nested() {
130     let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
131     let _x = &mut a.x.x;
132     let _y = a.y; //~ ERROR cannot use
133 }
134
135 fn move_after_mut_borrow_nested() {
136     let mut a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
137     let _x = &mut a.x.x;
138     let _y = a.y;
139     //~^ ERROR cannot move
140     //~| move out of
141 }
142
143 fn borrow_after_mut_borrow_nested() {
144     let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
145     let _x = &mut a.x.x;
146     //~^ mutable borrow occurs here
147     let _y = &a.y; //~ ERROR cannot borrow
148     //~^ immutable borrow occurs here
149 }
150
151 fn mut_borrow_after_borrow_nested() {
152     let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
153     let _x = &a.x.x;
154     //~^ immutable borrow occurs here
155     let _y = &mut a.y; //~ ERROR cannot borrow
156     //~^ mutable borrow occurs here
157 }
158
159 #[rustc_error]
160 fn main() {
161     copy_after_move();
162     move_after_move();
163     borrow_after_move();
164
165     move_after_borrow();
166
167     copy_after_mut_borrow();
168     move_after_mut_borrow();
169     borrow_after_mut_borrow();
170     mut_borrow_after_borrow();
171
172     copy_after_move_nested();
173     move_after_move_nested();
174     borrow_after_move_nested();
175
176     move_after_borrow_nested();
177
178     copy_after_mut_borrow_nested();
179     move_after_mut_borrow_nested();
180     borrow_after_mut_borrow_nested();
181     mut_borrow_after_borrow_nested();
182 }