Skip to content

Commit 03e9c4d

Browse files
committed
add more tests where the rulesets disagree
These come directly from the "Compare" tab of Typing Rust Patterns, though they had to be split across multiple files. They're not comprehensive, but they do provide some previously-missing coverage and are easier to check against the spec. Possibly they should be split up some more, since `pattern-errors.rs` is getting a bit unwieldy, but I'm not sure how best to go about that.
1 parent 493156c commit 03e9c4d

11 files changed

+659
-6
lines changed

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic.stderr

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,55 @@ error[E0596]: cannot borrow data in a `&` reference as mutable
2525
LL | if let &Some(Some(x)) = &Some(&mut Some(0)) {
2626
| ^ cannot borrow as mutable
2727

28-
error: aborting due to 3 previous errors
28+
error[E0596]: cannot borrow data in a `&` reference as mutable
29+
--> $DIR/borrowck-errors.rs:22:11
30+
|
31+
LL | let &[x] = &&mut [0];
32+
| ^ cannot borrow as mutable
33+
34+
error[E0508]: cannot move out of type `[&mut u32; 1]`, a non-copy array
35+
--> $DIR/borrowck-errors.rs:26:16
36+
|
37+
LL | let [&x] = &[&mut 0];
38+
| - ^^^^^^^^^ cannot move out of here
39+
| |
40+
| data moved here
41+
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
42+
|
43+
help: consider borrowing the pattern binding
44+
|
45+
LL | let [&ref x] = &[&mut 0];
46+
| +++
47+
48+
error[E0508]: cannot move out of type `[&mut u32; 1]`, a non-copy array
49+
--> $DIR/borrowck-errors.rs:31:16
50+
|
51+
LL | let [&x] = &mut [&mut 0];
52+
| - ^^^^^^^^^^^^^ cannot move out of here
53+
| |
54+
| data moved here
55+
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
56+
|
57+
help: consider borrowing the pattern binding
58+
|
59+
LL | let [&ref x] = &mut [&mut 0];
60+
| +++
61+
62+
error[E0508]: cannot move out of type `[&mut u32; 1]`, a non-copy array
63+
--> $DIR/borrowck-errors.rs:34:20
64+
|
65+
LL | let [&mut x] = &mut [&mut 0];
66+
| - ^^^^^^^^^^^^^ cannot move out of here
67+
| |
68+
| data moved here
69+
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
70+
|
71+
help: consider borrowing the pattern binding
72+
|
73+
LL | let [&mut ref x] = &mut [&mut 0];
74+
| +++
75+
76+
error: aborting due to 7 previous errors
2977

30-
Some errors have detailed explanations: E0507, E0596.
78+
Some errors have detailed explanations: E0507, E0508, E0596.
3179
For more information about an error, try `rustc --explain E0507`.

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,20 @@ pub fn main() {
1818
//[classic]~^ ERROR: cannot borrow data in a `&` reference as mutable
1919
let _: &u32 = x;
2020
}
21+
22+
let &[x] = &&mut [0];
23+
//[classic]~^ ERROR: cannot borrow data in a `&` reference as mutable
24+
let _: &u32 = x;
25+
26+
let [&x] = &[&mut 0];
27+
//[classic]~^ ERROR: cannot move out of type
28+
let _: &u32 = x;
29+
30+
#[cfg(classic)] // TODO: this should pass on `structural` but doesn't
31+
let [&x] = &mut [&mut 0]; //[classic]~ ERROR: cannot move out of type
32+
let _: &u32 = x;
33+
34+
let [&mut x] = &mut [&mut 0];
35+
//[classic]~^ ERROR: cannot move out of type
36+
let _: &mut u32 = x;
2137
}

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic.stderr

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,54 @@ help: replace this `&mut` pattern with `&`
8282
LL | if let Some(&Some(x)) = &Some(Some(0)) {
8383
| ~
8484

85-
error: aborting due to 7 previous errors
85+
error[E0308]: mismatched types
86+
--> $DIR/pattern-errors.rs:119:10
87+
|
88+
LL | let [&mut x] = &[&mut 0];
89+
| ^^^^^
90+
|
91+
= note: cannot match inherited `&` with `&mut` pattern
92+
help: replace this `&mut` pattern with `&`
93+
|
94+
LL | let [&x] = &[&mut 0];
95+
| ~
96+
97+
error[E0308]: mismatched types
98+
--> $DIR/pattern-errors.rs:124:10
99+
|
100+
LL | let [&mut &x] = &[&mut 0];
101+
| ^^^^^
102+
|
103+
= note: cannot match inherited `&` with `&mut` pattern
104+
help: replace this `&mut` pattern with `&`
105+
|
106+
LL | let [&&x] = &[&mut 0];
107+
| ~
108+
109+
error[E0308]: mismatched types
110+
--> $DIR/pattern-errors.rs:129:10
111+
|
112+
LL | let [&mut &ref x] = &[&mut 0];
113+
| ^^^^^
114+
|
115+
= note: cannot match inherited `&` with `&mut` pattern
116+
help: replace this `&mut` pattern with `&`
117+
|
118+
LL | let [&&ref x] = &[&mut 0];
119+
| ~
120+
121+
error[E0308]: mismatched types
122+
--> $DIR/pattern-errors.rs:134:10
123+
|
124+
LL | let [&mut &(mut x)] = &[&mut 0];
125+
| ^^^^^
126+
|
127+
= note: cannot match inherited `&` with `&mut` pattern
128+
help: replace this `&mut` pattern with `&`
129+
|
130+
LL | let [&&(mut x)] = &[&mut 0];
131+
| ~
132+
133+
error: aborting due to 11 previous errors
86134

87135
For more information about this error, try `rustc --explain E0308`.

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,95 @@ pub fn main() {
4444
//~^ ERROR: mismatched types
4545
}
4646
}
47+
48+
// TODO: these should be mutability mismatches on `structural`
49+
fn structural_errors_0() {
50+
let &[&mut x] = &&mut [0];
51+
//[structural]~^ ERROR: mismatched types
52+
let _: u32 = x;
53+
//[structural]~^ ERROR: mismatched types
54+
55+
let &[&mut x] = &mut &mut [0];
56+
//[structural]~^ ERROR: mismatched types
57+
let _: u32 = x;
58+
//[structural]~^ ERROR: mismatched types
59+
60+
let &[&mut ref x] = &&mut [0];
61+
//[structural]~^ ERROR: mismatched types
62+
let _: &u32 = x;
63+
64+
let &[&mut ref x] = &mut &mut [0];
65+
//[structural]~^ ERROR: mismatched types
66+
let _: &u32 = x;
67+
68+
let &[&mut mut x] = &&mut [0];
69+
//[structural]~^ ERROR: mismatched types
70+
//[structural]~| ERROR: binding cannot be both mutable and by-reference
71+
let _: u32 = x;
72+
//[structural]~^ ERROR: mismatched types
73+
74+
let &[&mut mut x] = &mut &mut [0];
75+
//[structural]~^ ERROR: mismatched types
76+
//[structural]~| ERROR: binding cannot be both mutable and by-reference
77+
let _: u32 = x;
78+
//[structural]~^ ERROR: mismatched types
79+
}
80+
81+
fn structural_errors_1() {
82+
let [&(mut x)] = &[&0];
83+
//[structural]~^ ERROR: binding cannot be both mutable and by-reference
84+
let _: &u32 = x;
85+
86+
let [&(mut x)] = &mut [&0];
87+
//[structural]~^ ERROR: binding cannot be both mutable and by-reference
88+
let _: &u32 = x;
89+
}
90+
91+
// TODO: these should be mutability mismatches on `structural`
92+
fn structural_errors_2() {
93+
let [&&mut x] = &[&mut 0];
94+
//[structural]~^ ERROR: mismatched types
95+
let _: u32 = x;
96+
//[structural]~^ ERROR: mismatched types
97+
98+
let [&&mut x] = &mut [&mut 0];
99+
let _: u32 = x;
100+
101+
let [&&mut ref x] = &[&mut 0];
102+
//[structural]~^ ERROR: mismatched types
103+
let _: &u32 = x;
104+
105+
let [&&mut ref x] = &mut [&mut 0];
106+
let _: &u32 = x;
107+
108+
let [&&mut mut x] = &[&mut 0];
109+
//[structural]~^ ERROR: binding cannot be both mutable and by-reference
110+
//[structural]~| ERROR: mismatched types
111+
let _: u32 = x;
112+
//[structural]~^ ERROR: mismatched types
113+
114+
let [&&mut mut x] = &mut [&mut 0];
115+
let _: u32 = x;
116+
}
117+
118+
fn classic_errors_0() {
119+
let [&mut x] = &[&mut 0];
120+
//[classic]~^ ERROR: mismatched types
121+
//[classic]~| cannot match inherited `&` with `&mut` pattern
122+
let _: &u32 = x;
123+
124+
let [&mut &x] = &[&mut 0];
125+
//[classic]~^ ERROR: mismatched types
126+
//[classic]~| cannot match inherited `&` with `&mut` pattern
127+
let _: u32 = x;
128+
129+
let [&mut &ref x] = &[&mut 0];
130+
//[classic]~^ ERROR: mismatched types
131+
//[classic]~| cannot match inherited `&` with `&mut` pattern
132+
let _: &u32 = x;
133+
134+
let [&mut &(mut x)] = &[&mut 0];
135+
//[classic]~^ ERROR: mismatched types
136+
//[classic]~| cannot match inherited `&` with `&mut` pattern
137+
let _: u32 = x;
138+
}

0 commit comments

Comments
 (0)