]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/borrowck-field-sensitivity.rs
Rollup merge of #101675 - beetrees:set-times-no-panic, r=joshtriplett
[rust.git] / src / test / ui / borrowck / borrowck-field-sensitivity.rs
1 struct A { a: isize, b: Box<isize> }
2
3
4
5 fn deref_after_move() {
6     let x = A { a: 1, b: Box::new(2) };
7     drop(x.b);
8     drop(*x.b); //~ ERROR use of moved value: `x.b`
9 }
10
11 fn deref_after_fu_move() {
12     let x = A { a: 1, b: Box::new(2) };
13     let y = A { a: 3, .. x };
14     drop(*x.b); //~ ERROR use of moved value: `x.b`
15 }
16
17 fn borrow_after_move() {
18     let x = A { a: 1, b: Box::new(2) };
19     drop(x.b);
20     let p = &x.b; //~ ERROR borrow of moved value: `x.b`
21     drop(**p);
22 }
23
24 fn borrow_after_fu_move() {
25     let x = A { a: 1, b: Box::new(2) };
26     let _y = A { a: 3, .. x };
27     let p = &x.b; //~ ERROR borrow of moved value: `x.b`
28     drop(**p);
29 }
30
31 fn move_after_borrow() {
32     let x = A { a: 1, b: Box::new(2) };
33     let p = &x.b;
34     drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed
35     drop(**p);
36 }
37
38 fn fu_move_after_borrow() {
39     let x = A { a: 1, b: Box::new(2) };
40     let p = &x.b;
41     let _y = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
42     drop(**p);
43 }
44
45 fn mut_borrow_after_mut_borrow() {
46     let mut x = A { a: 1, b: Box::new(2) };
47     let p = &mut x.a;
48     let q = &mut x.a; //~ ERROR cannot borrow `x.a` as mutable more than once at a time
49     drop(*p);
50     drop(*q);
51 }
52
53 fn move_after_move() {
54     let x = A { a: 1, b: Box::new(2) };
55     drop(x.b);
56     drop(x.b);  //~ ERROR use of moved value: `x.b`
57 }
58
59 fn move_after_fu_move() {
60     let x = A { a: 1, b: Box::new(2) };
61     let _y = A { a: 3, .. x };
62     drop(x.b);  //~ ERROR use of moved value: `x.b`
63 }
64
65 fn fu_move_after_move() {
66     let x = A { a: 1, b: Box::new(2) };
67     drop(x.b);
68     let _z = A { a: 3, .. x };  //~ ERROR use of moved value: `x.b`
69 }
70
71 fn fu_move_after_fu_move() {
72     let x = A { a: 1, b: Box::new(2) };
73     let _y = A { a: 3, .. x };
74     let _z = A { a: 4, .. x };  //~ ERROR use of moved value: `x.b`
75 }
76
77 // The following functions aren't yet accepted, but they should be.
78
79 fn copy_after_field_assign_after_uninit() {
80     let mut x: A;
81     x.a = 1; //~ ERROR E0381
82     drop(x.a);
83 }
84
85 fn borrow_after_field_assign_after_uninit() {
86     let mut x: A;
87     x.a = 1; //~ ERROR E0381
88     let p = &x.a;
89     drop(*p);
90 }
91
92 fn move_after_field_assign_after_uninit() {
93     let mut x: A;
94     x.b = Box::new(1); //~ ERROR E0381
95     drop(x.b);
96 }
97
98 fn main() {
99     deref_after_move();
100     deref_after_fu_move();
101
102     borrow_after_move();
103     borrow_after_fu_move();
104     move_after_borrow();
105     fu_move_after_borrow();
106     mut_borrow_after_mut_borrow();
107
108     move_after_move();
109     move_after_fu_move();
110     fu_move_after_move();
111     fu_move_after_fu_move();
112
113     copy_after_field_assign_after_uninit();
114     borrow_after_field_assign_after_uninit();
115     move_after_field_assign_after_uninit();
116 }