]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs
Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup
[rust.git] / src / test / ui / borrowck / bindings-after-at-or-patterns-slice-patterns-box-patterns.rs
1 // Tests using a combination of pattern features has the expected borrow checking behavior
2 #![feature(bindings_after_at)]
3 #![feature(or_patterns)]
4 #![feature(box_patterns)]
5
6 enum Test {
7     Foo,
8     Bar,
9     _Baz,
10 }
11
12 // bindings_after_at + slice_patterns
13
14 fn bindings_after_at_slice_patterns_move_binding(x: [String; 4]) {
15     match x {
16         a @ [.., _] => (),
17         _ => (),
18     };
19
20     &x;
21     //~^ ERROR borrow of moved value
22 }
23
24 fn bindings_after_at_slice_patterns_borrows_binding_mut(mut x: [String; 4]) {
25     let r = match x {
26         ref mut foo @ [.., _] => Some(foo),
27         _ => None,
28     };
29
30     &x;
31     //~^ ERROR cannot borrow
32
33     drop(r);
34 }
35
36 fn bindings_after_at_slice_patterns_borrows_slice_mut1(mut x: [String; 4]) {
37     let r = match x {
38         ref foo @ [.., ref mut bar] => (),
39         //~^ ERROR cannot borrow
40         _ => (),
41     };
42
43     drop(r);
44 }
45
46 fn bindings_after_at_slice_patterns_borrows_slice_mut2(mut x: [String; 4]) {
47     let r = match x {
48         [ref foo @ .., ref bar] => Some(foo),
49         _ => None,
50     };
51
52     &mut x;
53     //~^ ERROR cannot borrow
54
55     drop(r);
56 }
57
58 fn bindings_after_at_slice_patterns_borrows_both(mut x: [String; 4]) {
59     let r = match x {
60         ref foo @ [.., ref bar] => Some(foo),
61         _ => None,
62     };
63
64     &mut x;
65     //~^ ERROR cannot borrow
66
67     drop(r);
68 }
69
70 // bindings_after_at + or_patterns
71
72 fn bindings_after_at_or_patterns_move(x: Option<Test>) {
73     match x {
74         foo @ Some(Test::Foo | Test::Bar) => (),
75         _ => (),
76     }
77
78     &x;
79     //~^ ERROR borrow of moved value
80 }
81
82 fn bindings_after_at_or_patterns_borrows(mut x: Option<Test>) {
83     let r = match x {
84         ref foo @ Some(Test::Foo | Test::Bar) => Some(foo),
85         _ => None,
86     };
87
88     &mut x;
89     //~^ ERROR cannot borrow
90
91     drop(r);
92 }
93
94 fn bindings_after_at_or_patterns_borrows_mut(mut x: Option<Test>) {
95     let r = match x {
96         ref mut foo @ Some(Test::Foo | Test::Bar) => Some(foo),
97         _ => None,
98     };
99
100     &x;
101     //~^ ERROR cannot borrow
102
103     drop(r);
104 }
105
106 // bindings_after_at + box_patterns
107
108 fn bindings_after_at_box_patterns_borrows_both(mut x: Option<Box<String>>) {
109     let r = match x {
110         ref foo @ Some(box ref s) => Some(foo),
111         _ => None,
112     };
113
114     &mut x;
115     //~^ ERROR cannot borrow
116
117     drop(r);
118 }
119
120 fn bindings_after_at_box_patterns_borrows_mut(mut x: Option<Box<String>>) {
121     match x {
122         ref foo @ Some(box ref mut s) => (),
123         //~^ ERROR cannot borrow
124         _ => (),
125     };
126 }
127
128 // bindings_after_at + slice_patterns + or_patterns
129
130 fn bindings_after_at_slice_patterns_or_patterns_moves(x: [Option<Test>; 4]) {
131     match x {
132         a @ [.., Some(Test::Foo | Test::Bar)] => (),
133         _ => (),
134     };
135
136     &x;
137     //~^ ERROR borrow of moved value
138 }
139
140 fn bindings_after_at_slice_patterns_or_patterns_borrows_binding(mut x: [Option<Test>; 4]) {
141     let r = match x {
142         ref a @ [ref b @ .., Some(Test::Foo | Test::Bar)] => Some(a),
143         _ => None,
144     };
145
146     &mut x;
147     //~^ ERROR cannot borrow
148
149     drop(r);
150 }
151
152 fn bindings_after_at_slice_patterns_or_patterns_borrows_slice(mut x: [Option<Test>; 4]) {
153     let r = match x {
154         ref a @ [ref b @ .., Some(Test::Foo | Test::Bar)] => Some(b),
155         _ => None,
156     };
157
158     &mut x;
159     //~^ ERROR cannot borrow
160
161     drop(r);
162 }
163
164 // bindings_after_at + slice_patterns + box_patterns
165
166 fn bindings_after_at_slice_patterns_box_patterns_borrows(mut x: [Option<Box<String>>; 4]) {
167     let r = match x {
168         [_, ref a @ Some(box ref b), ..] => Some(a),
169         _ => None,
170     };
171
172     &mut x;
173     //~^ ERROR cannot borrow
174
175     drop(r);
176 }
177
178 // bindings_after_at + slice_patterns + or_patterns + box_patterns
179
180 fn bindings_after_at_slice_patterns_or_patterns_box_patterns_borrows(
181     mut x: [Option<Box<Test>>; 4]
182 ) {
183     let r = match x {
184         [_, ref a @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
185         _ => None,
186     };
187
188     &mut x;
189     //~^ ERROR cannot borrow
190
191     drop(r);
192 }
193
194 fn bindings_after_at_slice_patterns_or_patterns_box_patterns_borrows_mut(
195     mut x: [Option<Box<Test>>; 4]
196 ) {
197     let r = match x {
198         [_, ref mut a @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
199         _ => None,
200     };
201
202     &x;
203     //~^ ERROR cannot borrow
204
205     drop(r);
206 }
207
208 fn bindings_after_at_slice_patterns_or_patterns_box_patterns_borrows_binding(
209     mut x: [Option<Box<Test>>; 4]
210 ) {
211     let r = match x {
212         ref a @ [_, ref b @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
213         _ => None,
214     };
215
216     &mut x;
217     //~^ ERROR cannot borrow
218
219     drop(r);
220 }
221
222 fn main() {}