Skip to content

Commit 8c83dcb

Browse files
committed
move existing tests away from using boxes
Since deref patterns on boxes will be lowered differently, I'll be making a separate test file for them. This makes sure we're still testing the generic `Deref(Mut)::deref(_mut)`-based lowering.
1 parent 191df20 commit 8c83dcb

7 files changed

+96
-50
lines changed

tests/ui/pattern/deref-patterns/bindings.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#![feature(deref_patterns)]
44
#![allow(incomplete_features)]
55

6+
use std::rc::Rc;
7+
68
#[cfg(explicit)]
79
fn simple_vec(vec: Vec<u32>) -> u32 {
810
match vec {
@@ -53,37 +55,37 @@ fn nested_vec(vecvec: Vec<Vec<u32>>) -> u32 {
5355

5456
#[cfg(explicit)]
5557
fn ref_mut(val: u32) -> u32 {
56-
let mut b = Box::new(0u32);
58+
let mut b = vec![0u32];
5759
match &mut b {
58-
deref!(_x) if false => unreachable!(),
59-
deref!(x) => {
60+
deref!([_x]) if false => unreachable!(),
61+
deref!([x]) => {
6062
*x = val;
6163
}
6264
_ => unreachable!(),
6365
}
64-
let deref!(x) = &b else { unreachable!() };
66+
let deref!([x]) = &b else { unreachable!() };
6567
*x
6668
}
6769

6870
#[cfg(implicit)]
6971
fn ref_mut(val: u32) -> u32 {
70-
let mut b = Box::new((0u32,));
72+
let mut b = vec![0u32];
7173
match &mut b {
72-
(_x,) if false => unreachable!(),
73-
(x,) => {
74+
[_x] if false => unreachable!(),
75+
[x] => {
7476
*x = val;
7577
}
7678
_ => unreachable!(),
7779
}
78-
let (x,) = &b else { unreachable!() };
80+
let [x] = &b else { unreachable!() };
7981
*x
8082
}
8183

8284
#[cfg(explicit)]
8385
#[rustfmt::skip]
8486
fn or_and_guard(tuple: (u32, u32)) -> u32 {
8587
let mut sum = 0;
86-
let b = Box::new(tuple);
88+
let b = Rc::new(tuple);
8789
match b {
8890
deref!((x, _) | (_, x)) if { sum += x; false } => {},
8991
_ => {},
@@ -95,7 +97,7 @@ fn or_and_guard(tuple: (u32, u32)) -> u32 {
9597
#[rustfmt::skip]
9698
fn or_and_guard(tuple: (u32, u32)) -> u32 {
9799
let mut sum = 0;
98-
let b = Box::new(tuple);
100+
let b = Rc::new(tuple);
99101
match b {
100102
(x, _) | (_, x) if { sum += x; false } => {},
101103
_ => {},

tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use std::rc::Rc;
55

66
struct Struct;
77

8-
fn cant_move_out_box(b: Box<Struct>) -> Struct {
8+
fn cant_move_out_vec(b: Vec<Struct>) -> Struct {
99
match b {
10-
//~^ ERROR: cannot move out of a shared reference
11-
deref!(x) => x,
12-
_ => unreachable!(),
10+
//~^ ERROR: cannot move out of type `[Struct]`, a non-copy slice
11+
deref!([x]) => x,
12+
_ => panic!(),
1313
}
1414
}
1515

@@ -21,16 +21,16 @@ fn cant_move_out_rc(rc: Rc<Struct>) -> Struct {
2121
}
2222
}
2323

24-
struct Container(Struct);
25-
26-
fn cant_move_out_box_implicit(b: Box<Container>) -> Struct {
24+
fn cant_move_out_vec_implicit(b: Vec<Struct>) -> Struct {
2725
match b {
28-
//~^ ERROR: cannot move out of a shared reference
29-
Container(x) => x,
30-
_ => unreachable!(),
26+
//~^ ERROR: cannot move out of type `[Struct]`, a non-copy slice
27+
[x] => x,
28+
_ => panic!(),
3129
}
3230
}
3331

32+
struct Container(Struct);
33+
3434
fn cant_move_out_rc_implicit(rc: Rc<Container>) -> Struct {
3535
match rc {
3636
//~^ ERROR: cannot move out of a shared reference

tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.stderr

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
error[E0507]: cannot move out of a shared reference
1+
error[E0508]: cannot move out of type `[Struct]`, a non-copy slice
22
--> $DIR/cant_move_out_of_pattern.rs:9:11
33
|
44
LL | match b {
5-
| ^
5+
| ^ cannot move out of here
66
LL |
7-
LL | deref!(x) => x,
8-
| -
9-
| |
10-
| data moved here
11-
| move occurs because `x` has type `Struct`, which does not implement the `Copy` trait
7+
LL | deref!([x]) => x,
8+
| -
9+
| |
10+
| data moved here
11+
| move occurs because `x` has type `Struct`, which does not implement the `Copy` trait
1212
|
1313
help: consider borrowing the pattern binding
1414
|
15-
LL | deref!(ref x) => x,
16-
| +++
15+
LL | deref!([ref x]) => x,
16+
| +++
1717

1818
error[E0507]: cannot move out of a shared reference
1919
--> $DIR/cant_move_out_of_pattern.rs:17:11
@@ -32,22 +32,22 @@ help: consider borrowing the pattern binding
3232
LL | deref!(ref x) => x,
3333
| +++
3434

35-
error[E0507]: cannot move out of a shared reference
36-
--> $DIR/cant_move_out_of_pattern.rs:27:11
35+
error[E0508]: cannot move out of type `[Struct]`, a non-copy slice
36+
--> $DIR/cant_move_out_of_pattern.rs:25:11
3737
|
3838
LL | match b {
39-
| ^
39+
| ^ cannot move out of here
4040
LL |
41-
LL | Container(x) => x,
42-
| -
43-
| |
44-
| data moved here
45-
| move occurs because `x` has type `Struct`, which does not implement the `Copy` trait
41+
LL | [x] => x,
42+
| -
43+
| |
44+
| data moved here
45+
| move occurs because `x` has type `Struct`, which does not implement the `Copy` trait
4646
|
4747
help: consider borrowing the pattern binding
4848
|
49-
LL | Container(ref x) => x,
50-
| +++
49+
LL | [ref x] => x,
50+
| +++
5151

5252
error[E0507]: cannot move out of a shared reference
5353
--> $DIR/cant_move_out_of_pattern.rs:35:11
@@ -68,4 +68,5 @@ LL | Container(ref x) => x,
6868

6969
error: aborting due to 4 previous errors
7070

71-
For more information about this error, try `rustc --explain E0507`.
71+
Some errors have detailed explanations: E0507, E0508.
72+
For more information about an error, try `rustc --explain E0507`.

tests/ui/pattern/deref-patterns/closure_capture.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#![feature(deref_patterns)]
33
#![allow(incomplete_features)]
44

5+
use std::rc::Rc;
6+
57
fn main() {
6-
let b = Box::new("aaa".to_string());
8+
let b = Rc::new("aaa".to_string());
79
let f = || {
810
let deref!(ref s) = b else { unreachable!() };
911
assert_eq!(s.len(), 3);
@@ -20,13 +22,13 @@ fn main() {
2022
assert_eq!(v, [1, 2, 3]);
2123
f();
2224

23-
let mut b = Box::new("aaa".to_string());
25+
let mut b = "aaa".to_string();
2426
let mut f = || {
2527
let deref!(ref mut s) = b else { unreachable!() };
26-
s.push_str("aa");
28+
s.make_ascii_uppercase();
2729
};
2830
f();
29-
assert_eq!(b.len(), 5);
31+
assert_eq!(b, "AAA");
3032

3133
let mut v = vec![1, 2, 3];
3234
let mut f = || {

tests/ui/pattern/deref-patterns/fake_borrows.rs

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33

44
#[rustfmt::skip]
55
fn main() {
6+
let mut v = vec![false];
7+
match v {
8+
deref!([true]) => {}
9+
_ if { v[0] = true; false } => {}
10+
//~^ ERROR cannot borrow `v` as mutable because it is also borrowed as immutable
11+
deref!([false]) => {}
12+
_ => {},
13+
}
14+
match v {
15+
[true] => {}
16+
_ if { v[0] = true; false } => {}
17+
//~^ ERROR cannot borrow `v` as mutable because it is also borrowed as immutable
18+
[false] => {}
19+
_ => {},
20+
}
21+
22+
// deref patterns on boxes are lowered specially; test them separately.
623
let mut b = Box::new(false);
724
match b {
825
deref!(true) => {}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
1-
error[E0510]: cannot assign `*b` in match guard
1+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
22
--> $DIR/fake_borrows.rs:9:16
33
|
4+
LL | match v {
5+
| - immutable borrow occurs here
6+
LL | deref!([true]) => {}
7+
LL | _ if { v[0] = true; false } => {}
8+
| ^ - immutable borrow later used here
9+
| |
10+
| mutable borrow occurs here
11+
12+
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
13+
--> $DIR/fake_borrows.rs:16:16
14+
|
15+
LL | match v {
16+
| - immutable borrow occurs here
17+
LL | [true] => {}
18+
LL | _ if { v[0] = true; false } => {}
19+
| ^ - immutable borrow later used here
20+
| |
21+
| mutable borrow occurs here
22+
23+
error[E0510]: cannot assign `*b` in match guard
24+
--> $DIR/fake_borrows.rs:26:16
25+
|
426
LL | match b {
527
| - value is immutable in match guard
628
LL | deref!(true) => {}
729
LL | _ if { *b = true; false } => {}
830
| ^^^^^^^^^ cannot assign
931

1032
error[E0510]: cannot assign `*b` in match guard
11-
--> $DIR/fake_borrows.rs:16:16
33+
--> $DIR/fake_borrows.rs:33:16
1234
|
1335
LL | match b {
1436
| - value is immutable in match guard
1537
LL | true => {}
1638
LL | _ if { *b = true; false } => {}
1739
| ^^^^^^^^^ cannot assign
1840

19-
error: aborting due to 2 previous errors
41+
error: aborting due to 4 previous errors
2042

21-
For more information about this error, try `rustc --explain E0510`.
43+
Some errors have detailed explanations: E0502, E0510.
44+
For more information about an error, try `rustc --explain E0502`.

tests/ui/pattern/deref-patterns/implicit-cow-deref.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(incomplete_features)]
55

66
use std::borrow::Cow;
7+
use std::rc::Rc;
78

89
fn main() {
910
let cow: Cow<'static, [u8]> = Cow::Borrowed(&[1, 2, 3]);
@@ -18,7 +19,7 @@ fn main() {
1819
Cow::Owned(_) => unreachable!(),
1920
}
2021

21-
match Box::new(&cow) {
22+
match Rc::new(&cow) {
2223
Cow::Borrowed { 0: _ } => {}
2324
Cow::Owned { 0: _ } => unreachable!(),
2425
_ => unreachable!(),
@@ -37,7 +38,7 @@ fn main() {
3738
Cow::Owned(_) => {}
3839
}
3940

40-
match Box::new(&cow_of_cow) {
41+
match Rc::new(&cow_of_cow) {
4142
Cow::Borrowed { 0: _ } => unreachable!(),
4243
Cow::Owned { 0: _ } => {}
4344
_ => unreachable!(),

0 commit comments

Comments
 (0)