]> git.proxmox.com Git - rustc.git/blob - tests/ui/borrowck/borrowck-box-sensitivity.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / borrowck / borrowck-box-sensitivity.rs
1 // Test that `Box<T>` is treated specially by borrow checking. This is the case
2 // because NLL reverted the deicision in rust-lang/rfcs#130.
3
4 // run-pass
5
6 struct A {
7 x: Box<isize>,
8 y: isize,
9 }
10
11 struct B {
12 x: Box<isize>,
13 y: Box<isize>,
14 }
15
16 struct C {
17 x: Box<A>,
18 y: isize,
19 }
20
21 struct D {
22 x: Box<A>,
23 y: Box<isize>,
24 }
25
26 fn copy_after_move() {
27 let a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
28 let _x = a.x;
29 let _y = a.y;
30 }
31
32 fn move_after_move() {
33 let a: Box<_> = Box::new(B { x: Box::new(0), y: Box::new(1) });
34 let _x = a.x;
35 let _y = a.y;
36 }
37
38 fn borrow_after_move() {
39 let a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
40 let _x = a.x;
41 let _y = &a.y;
42 }
43
44 fn move_after_borrow() {
45 let a: Box<_> = Box::new(B { x: Box::new(0), y: Box::new(1) });
46 let _x = &a.x;
47 let _y = a.y;
48 use_imm(_x);
49 }
50 fn copy_after_mut_borrow() {
51 let mut a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
52 let _x = &mut a.x;
53 let _y = a.y;
54 use_mut(_x);
55 }
56 fn move_after_mut_borrow() {
57 let mut a: Box<_> = Box::new(B { x: Box::new(0), y: Box::new(1) });
58 let _x = &mut a.x;
59 let _y = a.y;
60 use_mut(_x);
61 }
62 fn borrow_after_mut_borrow() {
63 let mut a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
64 let _x = &mut a.x;
65 let _y = &a.y;
66 use_mut(_x);
67 }
68 fn mut_borrow_after_borrow() {
69 let mut a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
70 let _x = &a.x;
71 let _y = &mut a.y;
72 use_imm(_x);
73 }
74 fn copy_after_move_nested() {
75 let a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
76 let _x = a.x.x;
77 let _y = a.y;
78 }
79
80 fn move_after_move_nested() {
81 let a: Box<_> = Box::new(D { x: Box::new(A { x: Box::new(0), y: 1 }), y: Box::new(2) });
82 let _x = a.x.x;
83 let _y = a.y;
84 }
85
86 fn borrow_after_move_nested() {
87 let a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
88 let _x = a.x.x;
89 let _y = &a.y;
90 }
91
92 fn move_after_borrow_nested() {
93 let a: Box<_> = Box::new(D { x: Box::new(A { x: Box::new(0), y: 1 }), y: Box::new(2) });
94 let _x = &a.x.x;
95 let _y = a.y;
96 use_imm(_x);
97 }
98 fn copy_after_mut_borrow_nested() {
99 let mut a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
100 let _x = &mut a.x.x;
101 let _y = a.y;
102 use_mut(_x);
103 }
104 fn move_after_mut_borrow_nested() {
105 let mut a: Box<_> = Box::new(D { x: Box::new(A { x: Box::new(0), y: 1 }), y: Box::new(2) });
106 let _x = &mut a.x.x;
107 let _y = a.y;
108 use_mut(_x);
109 }
110 fn borrow_after_mut_borrow_nested() {
111 let mut a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
112 let _x = &mut a.x.x;
113 let _y = &a.y;
114 use_mut(_x);
115 }
116 fn mut_borrow_after_borrow_nested() {
117 let mut a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
118 let _x = &a.x.x;
119 let _y = &mut a.y;
120 use_imm(_x);
121 }
122
123 fn main() {
124 copy_after_move();
125 move_after_move();
126 borrow_after_move();
127
128 move_after_borrow();
129
130 copy_after_mut_borrow();
131 move_after_mut_borrow();
132 borrow_after_mut_borrow();
133 mut_borrow_after_borrow();
134
135 copy_after_move_nested();
136 move_after_move_nested();
137 borrow_after_move_nested();
138
139 move_after_borrow_nested();
140
141 copy_after_mut_borrow_nested();
142 move_after_mut_borrow_nested();
143 borrow_after_mut_borrow_nested();
144 mut_borrow_after_borrow_nested();
145 }
146
147 fn use_mut<T>(_: &mut T) { }
148 fn use_imm<T>(_: &T) { }