Skip to content

Commit e39abcf

Browse files
committed
harden & split borrowck-pat-at-and-box
1 parent 48f2766 commit e39abcf

File tree

4 files changed

+139
-62
lines changed

4 files changed

+139
-62
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// check-pass
2+
3+
// Test `@` patterns combined with `box` patterns.
4+
5+
#![feature(bindings_after_at)]
6+
//~^ WARN the feature `bindings_after_at` is incomplete and may cause the compiler to crash
7+
#![feature(box_patterns)]
8+
#![feature(slice_patterns)]
9+
10+
#[derive(Copy, Clone)]
11+
struct C;
12+
13+
fn c() -> C { C }
14+
15+
struct NC;
16+
17+
fn nc() -> NC { NC }
18+
19+
fn main() {
20+
let ref a @ box b = Box::new(C); // OK; the type is `Copy`.
21+
drop(b);
22+
drop(b);
23+
drop(a);
24+
25+
let ref a @ box b = Box::new(c()); // OK; the type is `Copy`.
26+
drop(b);
27+
drop(b);
28+
drop(a);
29+
30+
fn f3(ref a @ box b: Box<C>) { // OK; the type is `Copy`.
31+
drop(b);
32+
drop(b);
33+
drop(a);
34+
}
35+
match Box::new(c()) {
36+
ref a @ box b => { // OK; the type is `Copy`.
37+
drop(b);
38+
drop(b);
39+
drop(a);
40+
}
41+
}
42+
43+
let ref a @ box ref b = Box::new(NC); // OK.
44+
drop(a);
45+
drop(b);
46+
47+
fn f4(ref a @ box ref b: Box<NC>) { // OK.
48+
drop(a);
49+
drop(b)
50+
}
51+
52+
match Box::new(nc()) {
53+
ref a @ box ref b => { // OK.
54+
drop(a);
55+
drop(b);
56+
}
57+
}
58+
59+
match Box::new([Ok(c()), Err(nc()), Ok(c())]) {
60+
box [Ok(a), ref xs @ .., Err(ref b)] => {
61+
let _: C = a;
62+
let _: &[Result<C, NC>; 1] = xs;
63+
let _: &NC = b;
64+
}
65+
_ => {}
66+
}
67+
68+
match [Ok(Box::new(c())), Err(Box::new(nc())), Ok(Box::new(c())), Ok(Box::new(c()))] {
69+
[Ok(box a), ref xs @ .., Err(box ref b), Err(box ref c)] => {
70+
let _: C = a;
71+
let _: &[Result<Box<C>, Box<NC>>; 1] = xs;
72+
let _: &NC = b;
73+
let _: &NC = c;
74+
}
75+
_ => {}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `bindings_after_at` is incomplete and may cause the compiler to crash
2+
--> $DIR/borrowck-pat-at-and-box-pass.rs:5:12
3+
|
4+
LL | #![feature(bindings_after_at)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+

src/test/ui/pattern/bindings-after-at/borrowck-pat-at-and-box.rs

+13-39
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(bindings_after_at)]
44
//~^ WARN the feature `bindings_after_at` is incomplete and may cause the compiler to crash
55
#![feature(box_patterns)]
6+
#![feature(slice_patterns)]
67

78
#[derive(Copy, Clone)]
89
struct C;
@@ -34,47 +35,8 @@ fn main() {
3435
//~^ ERROR cannot bind by-move with sub-bindings
3536
//~| ERROR use of moved value
3637

37-
let ref a @ box b = Box::new(C); // OK; the type is `Copy`.
38-
drop(b);
39-
drop(b);
40-
drop(a);
41-
42-
let ref a @ box b = Box::new(c()); // OK; the type is `Copy`.
43-
drop(b);
44-
drop(b);
45-
drop(a);
46-
47-
fn f3(ref a @ box b: Box<C>) { // OK; the type is `Copy`.
48-
drop(b);
49-
drop(b);
50-
drop(a);
51-
}
52-
match Box::new(c()) {
53-
ref a @ box b => { // OK; the type is `Copy`.
54-
drop(b);
55-
drop(b);
56-
drop(a);
57-
}
58-
}
59-
6038
let ref a @ box b = Box::new(NC); //~ ERROR cannot bind by-move and by-ref in the same pattern
6139

62-
let ref a @ box ref b = Box::new(NC); // OK.
63-
drop(a);
64-
drop(b);
65-
66-
fn f4(ref a @ box ref b: Box<NC>) { // OK.
67-
drop(a);
68-
drop(b)
69-
}
70-
71-
match Box::new(nc()) {
72-
ref a @ box ref b => { // OK.
73-
drop(a);
74-
drop(b);
75-
}
76-
}
77-
7840
let ref a @ box ref mut b = Box::new(nc());
7941
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
8042
let ref a @ box ref mut b = Box::new(NC);
@@ -109,4 +71,16 @@ fn main() {
10971
drop(b);
11072
}
11173
}
74+
75+
match Box::new([Ok(c()), Err(nc()), Ok(c())]) {
76+
box [Ok(a), ref xs @ .., Err(b)] => {}
77+
//~^ ERROR cannot bind by-move and by-ref in the same pattern
78+
_ => {}
79+
}
80+
81+
match [Ok(Box::new(c())), Err(Box::new(nc())), Ok(Box::new(c())), Ok(Box::new(c()))] {
82+
[Ok(box ref a), ref xs @ .., Err(box b), Err(box ref mut c)] => {}
83+
//~^ ERROR cannot bind by-move and by-ref in the same pattern
84+
_ => {}
85+
}
11286
}

src/test/ui/pattern/bindings-after-at/borrowck-pat-at-and-box.stderr

+41-23
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ LL | #![feature(bindings_after_at)]
77
= note: `#[warn(incomplete_features)]` on by default
88

99
error[E0007]: cannot bind by-move with sub-bindings
10-
--> $DIR/borrowck-pat-at-and-box.rs:17:9
10+
--> $DIR/borrowck-pat-at-and-box.rs:18:9
1111
|
1212
LL | let a @ box &b = Box::new(&C);
1313
| ^^^^^^^^^^ binds an already bound by-move value by moving it
1414

1515
error[E0007]: cannot bind by-move with sub-bindings
16-
--> $DIR/borrowck-pat-at-and-box.rs:21:9
16+
--> $DIR/borrowck-pat-at-and-box.rs:22:9
1717
|
1818
LL | let a @ box b = Box::new(C);
1919
| ^^^^^^^^^ binds an already bound by-move value by moving it
2020

2121
error[E0007]: cannot bind by-move with sub-bindings
22-
--> $DIR/borrowck-pat-at-and-box.rs:33:25
22+
--> $DIR/borrowck-pat-at-and-box.rs:34:25
2323
|
2424
LL | match Box::new(C) { a @ box b => {} }
2525
| ^^^^^^^^^ binds an already bound by-move value by moving it
2626

2727
error[E0009]: cannot bind by-move and by-ref in the same pattern
28-
--> $DIR/borrowck-pat-at-and-box.rs:60:21
28+
--> $DIR/borrowck-pat-at-and-box.rs:38:21
2929
|
3030
LL | let ref a @ box b = Box::new(NC);
3131
| ------------^
@@ -34,7 +34,7 @@ LL | let ref a @ box b = Box::new(NC);
3434
| by-ref pattern here
3535

3636
error: cannot borrow `a` as mutable because it is also borrowed as immutable
37-
--> $DIR/borrowck-pat-at-and-box.rs:78:9
37+
--> $DIR/borrowck-pat-at-and-box.rs:40:9
3838
|
3939
LL | let ref a @ box ref mut b = Box::new(nc());
4040
| -----^^^^^^^---------
@@ -43,7 +43,7 @@ LL | let ref a @ box ref mut b = Box::new(nc());
4343
| immutable borrow occurs here
4444

4545
error: cannot borrow `a` as mutable because it is also borrowed as immutable
46-
--> $DIR/borrowck-pat-at-and-box.rs:80:9
46+
--> $DIR/borrowck-pat-at-and-box.rs:42:9
4747
|
4848
LL | let ref a @ box ref mut b = Box::new(NC);
4949
| -----^^^^^^^---------
@@ -52,7 +52,7 @@ LL | let ref a @ box ref mut b = Box::new(NC);
5252
| immutable borrow occurs here
5353

5454
error: cannot borrow `a` as mutable because it is also borrowed as immutable
55-
--> $DIR/borrowck-pat-at-and-box.rs:82:9
55+
--> $DIR/borrowck-pat-at-and-box.rs:44:9
5656
|
5757
LL | let ref a @ box ref mut b = Box::new(NC);
5858
| -----^^^^^^^---------
@@ -61,7 +61,7 @@ LL | let ref a @ box ref mut b = Box::new(NC);
6161
| immutable borrow occurs here
6262

6363
error: cannot borrow `a` as mutable because it is also borrowed as immutable
64-
--> $DIR/borrowck-pat-at-and-box.rs:85:9
64+
--> $DIR/borrowck-pat-at-and-box.rs:47:9
6565
|
6666
LL | let ref a @ box ref mut b = Box::new(NC);
6767
| -----^^^^^^^---------
@@ -70,7 +70,7 @@ LL | let ref a @ box ref mut b = Box::new(NC);
7070
| immutable borrow occurs here
7171

7272
error: cannot borrow `a` as immutable because it is also borrowed as mutable
73-
--> $DIR/borrowck-pat-at-and-box.rs:91:9
73+
--> $DIR/borrowck-pat-at-and-box.rs:53:9
7474
|
7575
LL | let ref mut a @ box ref b = Box::new(NC);
7676
| ---------^^^^^^^-----
@@ -79,28 +79,46 @@ LL | let ref mut a @ box ref b = Box::new(NC);
7979
| mutable borrow occurs here
8080

8181
error: cannot borrow `a` as immutable because it is also borrowed as mutable
82-
--> $DIR/borrowck-pat-at-and-box.rs:105:9
82+
--> $DIR/borrowck-pat-at-and-box.rs:67:9
8383
|
8484
LL | ref mut a @ box ref b => {
8585
| ---------^^^^^^^-----
8686
| | |
8787
| | immutable borrow occurs here
8888
| mutable borrow occurs here
8989

90+
error[E0009]: cannot bind by-move and by-ref in the same pattern
91+
--> $DIR/borrowck-pat-at-and-box.rs:76:38
92+
|
93+
LL | box [Ok(a), ref xs @ .., Err(b)] => {}
94+
| ----------- ^ by-move pattern here
95+
| |
96+
| by-ref pattern here
97+
98+
error[E0009]: cannot bind by-move and by-ref in the same pattern
99+
--> $DIR/borrowck-pat-at-and-box.rs:82:46
100+
|
101+
LL | [Ok(box ref a), ref xs @ .., Err(box b), Err(box ref mut c)] => {}
102+
| ----- ----------- ^ --------- by-ref pattern here
103+
| | | |
104+
| | | by-move pattern here
105+
| | by-ref pattern here
106+
| by-ref pattern here
107+
90108
error[E0007]: cannot bind by-move with sub-bindings
91-
--> $DIR/borrowck-pat-at-and-box.rs:25:11
109+
--> $DIR/borrowck-pat-at-and-box.rs:26:11
92110
|
93111
LL | fn f1(a @ box &b: Box<&C>) {}
94112
| ^^^^^^^^^^ binds an already bound by-move value by moving it
95113

96114
error[E0007]: cannot bind by-move with sub-bindings
97-
--> $DIR/borrowck-pat-at-and-box.rs:29:11
115+
--> $DIR/borrowck-pat-at-and-box.rs:30:11
98116
|
99117
LL | fn f2(a @ box b: Box<C>) {}
100118
| ^^^^^^^^^ binds an already bound by-move value by moving it
101119

102120
error: cannot borrow `a` as immutable because it is also borrowed as mutable
103-
--> $DIR/borrowck-pat-at-and-box.rs:97:11
121+
--> $DIR/borrowck-pat-at-and-box.rs:59:11
104122
|
105123
LL | fn f5(ref mut a @ box ref b: Box<NC>) {
106124
| ---------^^^^^^^-----
@@ -109,7 +127,7 @@ LL | fn f5(ref mut a @ box ref b: Box<NC>) {
109127
| mutable borrow occurs here
110128

111129
error[E0382]: use of moved value
112-
--> $DIR/borrowck-pat-at-and-box.rs:17:18
130+
--> $DIR/borrowck-pat-at-and-box.rs:18:18
113131
|
114132
LL | let a @ box &b = Box::new(&C);
115133
| ---------^ ------------ move occurs because value has type `std::boxed::Box<&C>`, which does not implement the `Copy` trait
@@ -118,7 +136,7 @@ LL | let a @ box &b = Box::new(&C);
118136
| value moved here
119137

120138
error[E0382]: use of moved value
121-
--> $DIR/borrowck-pat-at-and-box.rs:21:17
139+
--> $DIR/borrowck-pat-at-and-box.rs:22:17
122140
|
123141
LL | let a @ box b = Box::new(C);
124142
| --------^ ----------- move occurs because value has type `std::boxed::Box<C>`, which does not implement the `Copy` trait
@@ -127,7 +145,7 @@ LL | let a @ box b = Box::new(C);
127145
| value moved here
128146

129147
error[E0382]: use of moved value
130-
--> $DIR/borrowck-pat-at-and-box.rs:33:33
148+
--> $DIR/borrowck-pat-at-and-box.rs:34:33
131149
|
132150
LL | match Box::new(C) { a @ box b => {} }
133151
| ----------- --------^
@@ -137,7 +155,7 @@ LL | match Box::new(C) { a @ box b => {} }
137155
| move occurs because value has type `std::boxed::Box<C>`, which does not implement the `Copy` trait
138156

139157
error[E0502]: cannot borrow `_` as mutable because it is also borrowed as immutable
140-
--> $DIR/borrowck-pat-at-and-box.rs:85:21
158+
--> $DIR/borrowck-pat-at-and-box.rs:47:21
141159
|
142160
LL | let ref a @ box ref mut b = Box::new(NC);
143161
| ------------^^^^^^^^^
@@ -149,7 +167,7 @@ LL | drop(a);
149167
| - immutable borrow later used here
150168

151169
error[E0502]: cannot borrow `_` as immutable because it is also borrowed as mutable
152-
--> $DIR/borrowck-pat-at-and-box.rs:91:25
170+
--> $DIR/borrowck-pat-at-and-box.rs:53:25
153171
|
154172
LL | let ref mut a @ box ref b = Box::new(NC);
155173
| ----------------^^^^^
@@ -161,7 +179,7 @@ LL | *a = Box::new(NC);
161179
| -- mutable borrow later used here
162180

163181
error[E0502]: cannot borrow `_` as immutable because it is also borrowed as mutable
164-
--> $DIR/borrowck-pat-at-and-box.rs:105:25
182+
--> $DIR/borrowck-pat-at-and-box.rs:67:25
165183
|
166184
LL | ref mut a @ box ref b => {
167185
| ----------------^^^^^
@@ -173,7 +191,7 @@ LL | *a = Box::new(NC);
173191
| -- mutable borrow later used here
174192

175193
error[E0382]: use of moved value
176-
--> $DIR/borrowck-pat-at-and-box.rs:25:20
194+
--> $DIR/borrowck-pat-at-and-box.rs:26:20
177195
|
178196
LL | fn f1(a @ box &b: Box<&C>) {}
179197
| ---------^
@@ -183,7 +201,7 @@ LL | fn f1(a @ box &b: Box<&C>) {}
183201
| move occurs because value has type `std::boxed::Box<&C>`, which does not implement the `Copy` trait
184202

185203
error[E0382]: use of moved value
186-
--> $DIR/borrowck-pat-at-and-box.rs:29:19
204+
--> $DIR/borrowck-pat-at-and-box.rs:30:19
187205
|
188206
LL | fn f2(a @ box b: Box<C>) {}
189207
| --------^
@@ -193,7 +211,7 @@ LL | fn f2(a @ box b: Box<C>) {}
193211
| move occurs because value has type `std::boxed::Box<C>`, which does not implement the `Copy` trait
194212

195213
error[E0502]: cannot borrow `_` as immutable because it is also borrowed as mutable
196-
--> $DIR/borrowck-pat-at-and-box.rs:97:27
214+
--> $DIR/borrowck-pat-at-and-box.rs:59:27
197215
|
198216
LL | fn f5(ref mut a @ box ref b: Box<NC>) {
199217
| ----------------^^^^^
@@ -204,7 +222,7 @@ LL | fn f5(ref mut a @ box ref b: Box<NC>) {
204222
LL | *a = Box::new(NC);
205223
| -- mutable borrow later used here
206224

207-
error: aborting due to 22 previous errors
225+
error: aborting due to 24 previous errors
208226

209227
Some errors have detailed explanations: E0007, E0009, E0382, E0502.
210228
For more information about an error, try `rustc --explain E0007`.

0 commit comments

Comments
 (0)