Skip to content

Commit 0034e61

Browse files
committed
bindings_after_at: harden tests wrt. promotion
1 parent 9ab3603 commit 0034e61

11 files changed

+235
-93
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
fn main() {
77
struct U; // Not copy!
88

9+
// Prevent promotion:
10+
fn u() -> U { U }
11+
912
let a @ b = U;
1013
//~^ ERROR cannot bind by-move with sub-bindings
1114
//~| ERROR use of moved value
@@ -14,6 +17,10 @@ fn main() {
1417
//~^ ERROR cannot bind by-move with sub-bindings
1518
//~| ERROR use of moved value
1619

20+
let a @ (b, c) = (u(), u());
21+
//~^ ERROR cannot bind by-move with sub-bindings
22+
//~| ERROR use of moved value
23+
1724
match Ok(U) {
1825
a @ Ok(b) | a @ Err(b) => {}
1926
//~^ ERROR cannot bind by-move with sub-bindings

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,37 @@ 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-move-and-move.rs:9:9
10+
--> $DIR/borrowck-move-and-move.rs:12:9
1111
|
1212
LL | let a @ b = U;
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-move-and-move.rs:13:9
16+
--> $DIR/borrowck-move-and-move.rs:16:9
1717
|
1818
LL | let a @ (b, c) = (U, U);
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-move-and-move.rs:18:9
22+
--> $DIR/borrowck-move-and-move.rs:20:9
23+
|
24+
LL | let a @ (b, c) = (u(), u());
25+
| ^^^^^^^^^^ binds an already bound by-move value by moving it
26+
27+
error[E0007]: cannot bind by-move with sub-bindings
28+
--> $DIR/borrowck-move-and-move.rs:25:9
2329
|
2430
LL | a @ Ok(b) | a @ Err(b) => {}
2531
| ^^^^^^^^^ binds an already bound by-move value by moving it
2632

2733
error[E0007]: cannot bind by-move with sub-bindings
28-
--> $DIR/borrowck-move-and-move.rs:18:21
34+
--> $DIR/borrowck-move-and-move.rs:25:21
2935
|
3036
LL | a @ Ok(b) | a @ Err(b) => {}
3137
| ^^^^^^^^^^ binds an already bound by-move value by moving it
3238

3339
error[E0382]: use of moved value
34-
--> $DIR/borrowck-move-and-move.rs:9:13
40+
--> $DIR/borrowck-move-and-move.rs:12:13
3541
|
3642
LL | let a @ b = U;
3743
| ----^ - move occurs because value has type `main::U`, which does not implement the `Copy` trait
@@ -40,7 +46,7 @@ LL | let a @ b = U;
4046
| value moved here
4147

4248
error[E0382]: use of moved value
43-
--> $DIR/borrowck-move-and-move.rs:13:17
49+
--> $DIR/borrowck-move-and-move.rs:16:17
4450
|
4551
LL | let a @ (b, c) = (U, U);
4652
| --------^- ------ move occurs because value has type `(main::U, main::U)`, which does not implement the `Copy` trait
@@ -49,7 +55,16 @@ LL | let a @ (b, c) = (U, U);
4955
| value moved here
5056

5157
error[E0382]: use of moved value
52-
--> $DIR/borrowck-move-and-move.rs:18:16
58+
--> $DIR/borrowck-move-and-move.rs:20:17
59+
|
60+
LL | let a @ (b, c) = (u(), u());
61+
| --------^- ---------- move occurs because value has type `(main::U, main::U)`, which does not implement the `Copy` trait
62+
| | |
63+
| | value used here after move
64+
| value moved here
65+
66+
error[E0382]: use of moved value
67+
--> $DIR/borrowck-move-and-move.rs:25:16
5368
|
5469
LL | match Ok(U) {
5570
| ----- move occurs because value has type `std::result::Result<main::U, main::U>`, which does not implement the `Copy` trait
@@ -60,7 +75,7 @@ LL | a @ Ok(b) | a @ Err(b) => {}
6075
| value moved here
6176

6277
error[E0382]: use of moved value
63-
--> $DIR/borrowck-move-and-move.rs:18:29
78+
--> $DIR/borrowck-move-and-move.rs:25:29
6479
|
6580
LL | match Ok(U) {
6681
| ----- move occurs because value has type `std::result::Result<main::U, main::U>`, which does not implement the `Copy` trait
@@ -70,7 +85,7 @@ LL | a @ Ok(b) | a @ Err(b) => {}
7085
| | value used here after move
7186
| value moved here
7287

73-
error: aborting due to 8 previous errors
88+
error: aborting due to 10 previous errors
7489

7590
Some errors have detailed explanations: E0007, E0382.
7691
For more information about an error, try `rustc --explain E0007`.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#[derive(Copy, Clone)]
88
struct C;
99

10+
fn c() -> C { C }
11+
1012
fn main() {
1113
let a @ box &b = Box::new(&C);
1214
//~^ ERROR cannot bind by-move with sub-bindings
@@ -21,14 +23,23 @@ fn main() {
2123
drop(b);
2224
drop(a);
2325

26+
let ref a @ box b = Box::new(c()); // OK; the type is `Copy`.
27+
drop(b);
28+
drop(b);
29+
drop(a);
30+
2431
struct NC;
2532

33+
fn nc() -> NC { NC }
34+
2635
let ref a @ box b = Box::new(NC); //~ ERROR cannot bind by-move and by-ref in the same pattern
2736

2837
let ref a @ box ref b = Box::new(NC); // OK.
2938
drop(a);
3039
drop(b);
3140

41+
let ref a @ box ref mut b = Box::new(nc());
42+
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
3243
let ref a @ box ref mut b = Box::new(NC);
3344
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
3445
let ref a @ box ref mut b = Box::new(NC);

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ 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:11:9
10+
--> $DIR/borrowck-pat-at-and-box.rs:13: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:15:9
16+
--> $DIR/borrowck-pat-at-and-box.rs:17: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[E0009]: cannot bind by-move and by-ref in the same pattern
22-
--> $DIR/borrowck-pat-at-and-box.rs:26:21
22+
--> $DIR/borrowck-pat-at-and-box.rs:35:21
2323
|
2424
LL | let ref a @ box b = Box::new(NC);
2525
| ------------^
@@ -28,7 +28,16 @@ LL | let ref a @ box b = Box::new(NC);
2828
| by-ref pattern here
2929

3030
error: cannot borrow `a` as mutable because it is also borrowed as immutable
31-
--> $DIR/borrowck-pat-at-and-box.rs:32:9
31+
--> $DIR/borrowck-pat-at-and-box.rs:41:9
32+
|
33+
LL | let ref a @ box ref mut b = Box::new(nc());
34+
| -----^^^^^^^---------
35+
| | |
36+
| | mutable borrow occurs here
37+
| immutable borrow occurs here
38+
39+
error: cannot borrow `a` as mutable because it is also borrowed as immutable
40+
--> $DIR/borrowck-pat-at-and-box.rs:43:9
3241
|
3342
LL | let ref a @ box ref mut b = Box::new(NC);
3443
| -----^^^^^^^---------
@@ -37,7 +46,7 @@ LL | let ref a @ box ref mut b = Box::new(NC);
3746
| immutable borrow occurs here
3847

3948
error: cannot borrow `a` as mutable because it is also borrowed as immutable
40-
--> $DIR/borrowck-pat-at-and-box.rs:34:9
49+
--> $DIR/borrowck-pat-at-and-box.rs:45:9
4150
|
4251
LL | let ref a @ box ref mut b = Box::new(NC);
4352
| -----^^^^^^^---------
@@ -46,7 +55,7 @@ LL | let ref a @ box ref mut b = Box::new(NC);
4655
| immutable borrow occurs here
4756

4857
error: cannot borrow `a` as mutable because it is also borrowed as immutable
49-
--> $DIR/borrowck-pat-at-and-box.rs:37:9
58+
--> $DIR/borrowck-pat-at-and-box.rs:48:9
5059
|
5160
LL | let ref a @ box ref mut b = Box::new(NC);
5261
| -----^^^^^^^---------
@@ -55,7 +64,7 @@ LL | let ref a @ box ref mut b = Box::new(NC);
5564
| immutable borrow occurs here
5665

5766
error: cannot borrow `a` as immutable because it is also borrowed as mutable
58-
--> $DIR/borrowck-pat-at-and-box.rs:43:9
67+
--> $DIR/borrowck-pat-at-and-box.rs:54:9
5968
|
6069
LL | let ref mut a @ box ref b = Box::new(NC);
6170
| ---------^^^^^^^-----
@@ -64,7 +73,7 @@ LL | let ref mut a @ box ref b = Box::new(NC);
6473
| mutable borrow occurs here
6574

6675
error[E0382]: use of moved value
67-
--> $DIR/borrowck-pat-at-and-box.rs:11:18
76+
--> $DIR/borrowck-pat-at-and-box.rs:13:18
6877
|
6978
LL | let a @ box &b = Box::new(&C);
7079
| ---------^ ------------ move occurs because value has type `std::boxed::Box<&C>`, which does not implement the `Copy` trait
@@ -73,7 +82,7 @@ LL | let a @ box &b = Box::new(&C);
7382
| value moved here
7483

7584
error[E0382]: use of moved value
76-
--> $DIR/borrowck-pat-at-and-box.rs:15:17
85+
--> $DIR/borrowck-pat-at-and-box.rs:17:17
7786
|
7887
LL | let a @ box b = Box::new(C);
7988
| --------^ ----------- move occurs because value has type `std::boxed::Box<C>`, which does not implement the `Copy` trait
@@ -82,7 +91,7 @@ LL | let a @ box b = Box::new(C);
8291
| value moved here
8392

8493
error[E0502]: cannot borrow `_` as mutable because it is also borrowed as immutable
85-
--> $DIR/borrowck-pat-at-and-box.rs:37:21
94+
--> $DIR/borrowck-pat-at-and-box.rs:48:21
8695
|
8796
LL | let ref a @ box ref mut b = Box::new(NC);
8897
| ------------^^^^^^^^^
@@ -94,7 +103,7 @@ LL | drop(a);
94103
| - immutable borrow later used here
95104

96105
error[E0502]: cannot borrow `_` as immutable because it is also borrowed as mutable
97-
--> $DIR/borrowck-pat-at-and-box.rs:43:25
106+
--> $DIR/borrowck-pat-at-and-box.rs:54:25
98107
|
99108
LL | let ref mut a @ box ref b = Box::new(NC);
100109
| ----------------^^^^^
@@ -105,7 +114,7 @@ LL | let ref mut a @ box ref b = Box::new(NC);
105114
LL | *a = Box::new(NC);
106115
| -- mutable borrow later used here
107116

108-
error: aborting due to 11 previous errors
117+
error: aborting due to 12 previous errors
109118

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

src/test/ui/pattern/bindings-after-at/borrowck-pat-by-copy-bindings-in-at.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
#[derive(Copy, Clone)]
1010
struct C;
1111

12+
fn mk_c() -> C { C }
13+
1214
#[derive(Copy, Clone)]
1315
struct P<A, B>(A, B);
1416

1517
enum E<A, B> { L(A), R(B) }
1618

1719
fn main() {
1820
let a @ b @ c @ d = C;
19-
let a @ (b, c) = (C, C);
20-
let a @ P(b, P(c, d)) = P(C, P(C, C));
21+
let a @ (b, c) = (C, mk_c());
22+
let a @ P(b, P(c, d)) = P(mk_c(), P(C, C));
2123
let a @ [b, c] = [C, C];
22-
let a @ [b, .., c] = [C, C, C];
24+
let a @ [b, .., c] = [C, mk_c(), C];
2325
let a @ &(b, c) = &(C, C);
24-
let a @ &(b, &P(c, d)) = &(C, &P(C, C));
26+
let a @ &(b, &P(c, d)) = &(mk_c(), &P(C, C));
2527

2628
use self::E::*;
2729
match L(C) {
@@ -31,7 +33,7 @@ fn main() {
3133
drop(a);
3234
}
3335
}
34-
match R(&L(&C)) {
36+
match R(&L(&mk_c())) {
3537
L(L(&a)) | L(R(&a)) | R(L(&a)) | R(R(&a)) => {
3638
let a: C = a;
3739
drop(a);

src/test/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
fn main() {
55
match Some("hi".to_string()) {
66
ref op_string_ref @ Some(s) => {},
7-
//~^ ERROR E0009
7+
//~^ ERROR cannot bind by-move and by-ref in the same pattern [E0009]
88
None => {},
99
}
1010
}

src/test/ui/pattern/bindings-after-at/borrowck-pat-ref-both-sides.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,35 @@
99
fn main() {
1010
struct U; // Not copy!
1111

12+
// Promotion:
1213
let ref a @ ref b = U;
1314
let _: &U = a;
1415
let _: &U = b;
1516

16-
let ref a @ (ref b, [ref c, ref d]) = (U, [U, U]);
17+
// Prevent promotion:
18+
fn u() -> U { U }
19+
20+
let ref a @ ref b = u();
21+
let _: &U = a;
22+
let _: &U = b;
23+
24+
let ref a @ (ref b, [ref c, ref d]) = (u(), [u(), u()]);
1725
let _: &(U, [U; 2]) = a;
1826
let _: &U = b;
1927
let _: &U = c;
2028
let _: &U = d;
2129

22-
let a @ (b, [c, d]) = &(U, [U, U]);
30+
let a @ (b, [c, d]) = &(u(), [u(), u()]);
2331
let _: &(U, [U; 2]) = a;
2432
let _: &U = b;
2533
let _: &U = c;
2634
let _: &U = d;
2735

28-
let ref a @ &ref b = &U;
36+
let ref a @ &ref b = &u();
2937
let _: &&U = a;
3038
let _: &U = b;
3139

32-
match Ok(U) {
40+
match Ok(u()) {
3341
ref a @ Ok(ref b) | ref a @ Err(ref b) => {
3442
let _: &Result<U, U> = a;
3543
let _: &U = b;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ fn main() {
1919

2020
struct U;
2121

22+
// Prevent promotion:
23+
fn u() -> U { U }
24+
2225
let ref a @ ref mut b = U;
2326
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
2427
let ref mut a @ ref b = U;
@@ -28,6 +31,17 @@ fn main() {
2831
let ref mut a @ (ref b, ref c) = (U, U);
2932
//~^ ERROR cannot borrow `a` as immutable because it is also borrowed as mutable
3033

34+
let ref mut a @ ref b = u();
35+
//~^ ERROR cannot borrow `a` as immutable because it is also borrowed as mutable
36+
//~| ERROR cannot borrow `_` as immutable because it is also borrowed as mutable
37+
*a = u();
38+
drop(b);
39+
let ref a @ ref mut b = u();
40+
//~^ ERROR cannot borrow `a` as mutable because it is also borrowed as immutable
41+
//~| ERROR cannot borrow `_` as mutable because it is also borrowed as immutable
42+
*b = u();
43+
drop(a);
44+
3145
let ref mut a @ ref b = U;
3246
//~^ ERROR cannot borrow `a` as immutable because it is also borrowed as mutable
3347
*a = U;

0 commit comments

Comments
 (0)