Skip to content

Commit 4ed44c9

Browse files
committed
add a stable edition 2021 revision to pattern typing tests
This serves two purposes. First, they're additional tests that stable Rust behavior hasn't been messed with. There's plenty of other pattern tests, so this is less important, but these at least are targeted at what's being changed. Second, this helps document exactly where the new rulesets agree and disagree with stable pattern typing. This will be especially important after the new rules for old editions are updated, since they need to be strictly more permissive; any patterns well-typed on stable should also be well-typed with the same resultant bindings on the (upcoming) new new old-edition rules. The unusual test ordering on `borrowck-errors.rs` and `ref-binding-on-inh-ref-errors.rs` are to hopefully reduce how much adding new tests will mess with line numbers in their stderr.
1 parent afd976b commit 4ed44c9

23 files changed

+1103
-198
lines changed

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

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
1+
error[E0508]: cannot move out of type `[&mut u32; 1]`, a non-copy array
2+
--> $DIR/borrowck-errors.rs:13:16
3+
|
4+
LL | let [&x] = &[&mut 0];
5+
| - ^^^^^^^^^ cannot move out of here
6+
| |
7+
| data moved here
8+
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
9+
|
10+
help: consider borrowing the pattern binding
11+
|
12+
LL | let [&ref x] = &[&mut 0];
13+
| +++
14+
15+
error[E0508]: cannot move out of type `[&mut u32; 1]`, a non-copy array
16+
--> $DIR/borrowck-errors.rs:19:16
17+
|
18+
LL | let [&x] = &mut [&mut 0];
19+
| - ^^^^^^^^^^^^^ cannot move out of here
20+
| |
21+
| data moved here
22+
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
23+
|
24+
help: consider borrowing the pattern binding
25+
|
26+
LL | let [&ref x] = &mut [&mut 0];
27+
| +++
28+
129
error[E0507]: cannot move out of a shared reference
2-
--> $DIR/borrowck-errors.rs:9:29
30+
--> $DIR/borrowck-errors.rs:27:29
331
|
432
LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) {
533
| - ^^^^^^^^^^^^^^^^^^^
@@ -14,59 +42,31 @@ LL + if let Some(Some(x)) = Some(&Some(&mut 0)) {
1442
|
1543

1644
error[E0596]: cannot borrow data in a `&` reference as mutable
17-
--> $DIR/borrowck-errors.rs:14:10
45+
--> $DIR/borrowck-errors.rs:32:10
1846
|
1947
LL | let &ref mut x = &0;
2048
| ^^^^^^^^^ cannot borrow as mutable
2149

2250
error[E0596]: cannot borrow data in a `&` reference as mutable
23-
--> $DIR/borrowck-errors.rs:17:23
51+
--> $DIR/borrowck-errors.rs:35:23
2452
|
2553
LL | if let &Some(Some(x)) = &Some(&mut Some(0)) {
2654
| ^ cannot borrow as mutable
2755

2856
error[E0596]: cannot borrow data in a `&` reference as mutable
29-
--> $DIR/borrowck-errors.rs:22:11
57+
--> $DIR/borrowck-errors.rs:40:11
3058
|
3159
LL | let &[x] = &&mut [0];
3260
| ^ cannot borrow as mutable
3361

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:30: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
62+
error[E0508]: cannot move out of type `[&mut i32; 1]`, a non-copy array
63+
--> $DIR/borrowck-errors.rs:44:20
6464
|
6565
LL | let [&mut x] = &mut [&mut 0];
6666
| - ^^^^^^^^^^^^^ cannot move out of here
6767
| |
6868
| data moved here
69-
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
69+
| move occurs because `x` has type `&mut i32`, which does not implement the `Copy` trait
7070
|
7171
help: consider borrowing the pattern binding
7272
|
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1-
//@ edition: 2024
2-
//@ revisions: classic2024 structural2024
1+
//@ revisions: stable2021 classic2024 structural2024
2+
//@[stable2021] edition: 2021
3+
//@[classic2024] edition: 2024
4+
//@[structural2024] edition: 2024
35
//! Tests for pattern errors not handled by the pattern typing rules, but by borrowck.
46
#![allow(incomplete_features)]
57
#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))]
68
#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))]
79

10+
/// These patterns additionally use `&` to match a `&mut` reference type, which causes compilation
11+
/// to fail in HIR typeck on stable. As such, they need to be separate from the other tests.
12+
fn errors_caught_in_hir_typeck_on_stable() {
13+
let [&x] = &[&mut 0];
14+
//[stable2021]~^ mismatched types
15+
//[stable2021]~| types differ in mutability
16+
//[classic2024]~^^^ ERROR: cannot move out of type
17+
let _: &u32 = x;
18+
19+
let [&x] = &mut [&mut 0];
20+
//[stable2021]~^ mismatched types
21+
//[stable2021]~| types differ in mutability
22+
//[classic2024]~^^^ ERROR: cannot move out of type
23+
let _: &u32 = x;
24+
}
25+
826
pub fn main() {
927
if let Some(&Some(x)) = Some(&Some(&mut 0)) {
1028
//~^ ERROR: cannot move out of a shared reference [E0507]
@@ -15,23 +33,16 @@ pub fn main() {
1533
//~^ cannot borrow data in a `&` reference as mutable [E0596]
1634

1735
if let &Some(Some(x)) = &Some(&mut Some(0)) {
18-
//[classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
36+
//[stable2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
1937
let _: &u32 = x;
2038
}
2139

2240
let &[x] = &&mut [0];
23-
//[classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
24-
let _: &u32 = x;
25-
26-
let [&x] = &[&mut 0];
27-
//[classic2024]~^ ERROR: cannot move out of type
28-
let _: &u32 = x;
29-
30-
let [&x] = &mut [&mut 0];
31-
//[classic2024]~^ ERROR: cannot move out of type
41+
//[stable2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
3242
let _: &u32 = x;
3343

3444
let [&mut x] = &mut [&mut 0];
3545
//[classic2024]~^ ERROR: cannot move out of type
36-
let _: &mut u32 = x;
46+
#[cfg(stable2021)] let _: u32 = x;
47+
#[cfg(structural2024)] let _: &mut u32 = x;
3748
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/borrowck-errors.rs:13:10
3+
|
4+
LL | let [&x] = &[&mut 0];
5+
| ^^ --------- this expression has type `&[&mut {integer}; 1]`
6+
| |
7+
| types differ in mutability
8+
|
9+
= note: expected mutable reference `&mut {integer}`
10+
found reference `&_`
11+
help: consider removing `&` from the pattern
12+
|
13+
LL - let [&x] = &[&mut 0];
14+
LL + let [x] = &[&mut 0];
15+
|
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/borrowck-errors.rs:19:10
19+
|
20+
LL | let [&x] = &mut [&mut 0];
21+
| ^^ ------------- this expression has type `&mut [&mut {integer}; 1]`
22+
| |
23+
| types differ in mutability
24+
|
25+
= note: expected mutable reference `&mut {integer}`
26+
found reference `&_`
27+
help: consider removing `&` from the pattern
28+
|
29+
LL - let [&x] = &mut [&mut 0];
30+
LL + let [x] = &mut [&mut 0];
31+
|
32+
33+
error[E0507]: cannot move out of a shared reference
34+
--> $DIR/borrowck-errors.rs:27:29
35+
|
36+
LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) {
37+
| - ^^^^^^^^^^^^^^^^^^^
38+
| |
39+
| data moved here
40+
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
41+
|
42+
help: consider removing the borrow
43+
|
44+
LL - if let Some(&Some(x)) = Some(&Some(&mut 0)) {
45+
LL + if let Some(Some(x)) = Some(&Some(&mut 0)) {
46+
|
47+
48+
error[E0596]: cannot borrow data in a `&` reference as mutable
49+
--> $DIR/borrowck-errors.rs:32:10
50+
|
51+
LL | let &ref mut x = &0;
52+
| ^^^^^^^^^ cannot borrow as mutable
53+
54+
error[E0596]: cannot borrow data in a `&` reference as mutable
55+
--> $DIR/borrowck-errors.rs:35:23
56+
|
57+
LL | if let &Some(Some(x)) = &Some(&mut Some(0)) {
58+
| ^ cannot borrow as mutable
59+
60+
error[E0596]: cannot borrow data in a `&` reference as mutable
61+
--> $DIR/borrowck-errors.rs:40:11
62+
|
63+
LL | let &[x] = &&mut [0];
64+
| ^ cannot borrow as mutable
65+
66+
error: aborting due to 6 previous errors
67+
68+
Some errors have detailed explanations: E0308, E0507, E0596.
69+
For more information about an error, try `rustc --explain E0308`.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0507]: cannot move out of a shared reference
2-
--> $DIR/borrowck-errors.rs:9:29
2+
--> $DIR/borrowck-errors.rs:27:29
33
|
44
LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) {
55
| - ^^^^^^^^^^^^^^^^^^^
@@ -14,7 +14,7 @@ LL + if let Some(Some(x)) = Some(&Some(&mut 0)) {
1414
|
1515

1616
error[E0596]: cannot borrow data in a `&` reference as mutable
17-
--> $DIR/borrowck-errors.rs:14:10
17+
--> $DIR/borrowck-errors.rs:32:10
1818
|
1919
LL | let &ref mut x = &0;
2020
| ^^^^^^^^^ cannot borrow as mutable

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic2024.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: binding cannot be both mutable and by-reference
2-
--> $DIR/mut-ref-mut.rs:11:13
2+
--> $DIR/mut-ref-mut.rs:14:13
33
|
44
LL | let Foo(mut a) = &Foo(0);
55
| ^^^^
@@ -9,7 +9,7 @@ LL | let Foo(mut a) = &Foo(0);
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

1111
error[E0658]: binding cannot be both mutable and by-reference
12-
--> $DIR/mut-ref-mut.rs:15:13
12+
--> $DIR/mut-ref-mut.rs:19:13
1313
|
1414
LL | let Foo(mut a) = &mut Foo(0);
1515
| ^^^^
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
//@ edition: 2024
2-
//@ revisions: classic2024 structural2024
1+
//@ revisions: stable2021 classic2024 structural2024
2+
//@[stable2021] edition: 2021
3+
//@[classic2024] edition: 2024
4+
//@[structural2024] edition: 2024
5+
//@[stable2021] run-pass
36
//! Test diagnostics for binding with `mut` when the default binding mode is by-ref.
4-
#![allow(incomplete_features)]
7+
#![allow(incomplete_features, unused_assignments, unused_variables)]
58
#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))]
69
#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))]
710

811
pub fn main() {
912
struct Foo(u8);
1013

1114
let Foo(mut a) = &Foo(0);
12-
//~^ ERROR: binding cannot be both mutable and by-reference
13-
a = &42;
15+
//[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference
16+
#[cfg(stable2021)] { a = 42 }
17+
#[cfg(any(classic2024, structural2024))] { a = &42 }
1418

1519
let Foo(mut a) = &mut Foo(0);
16-
//~^ ERROR: binding cannot be both mutable and by-reference
17-
a = &mut 42;
20+
//[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference
21+
#[cfg(stable2021)] { a = 42 }
22+
#[cfg(any(classic2024, structural2024))] { a = &mut 42 }
1823
}

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural2024.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: binding cannot be both mutable and by-reference
2-
--> $DIR/mut-ref-mut.rs:11:13
2+
--> $DIR/mut-ref-mut.rs:14:13
33
|
44
LL | let Foo(mut a) = &Foo(0);
55
| ^^^^
@@ -9,7 +9,7 @@ LL | let Foo(mut a) = &Foo(0);
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

1111
error[E0658]: binding cannot be both mutable and by-reference
12-
--> $DIR/mut-ref-mut.rs:15:13
12+
--> $DIR/mut-ref-mut.rs:19:13
1313
|
1414
LL | let Foo(mut a) = &mut Foo(0);
1515
| ^^^^

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/pattern-errors.rs:10:17
2+
--> $DIR/pattern-errors.rs:12:17
33
|
44
LL | if let Some(&mut x) = &Some(&mut 0) {
55
| ^^^^^
@@ -11,7 +11,7 @@ LL | if let Some(&x) = &Some(&mut 0) {
1111
| ~
1212

1313
error[E0308]: mismatched types
14-
--> $DIR/pattern-errors.rs:15:17
14+
--> $DIR/pattern-errors.rs:18:17
1515
|
1616
LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
1717
| ^^^^^
@@ -23,7 +23,7 @@ LL | if let Some(&Some(&x)) = &Some(&mut Some(0)) {
2323
| ~
2424

2525
error[E0308]: mismatched types
26-
--> $DIR/pattern-errors.rs:20:22
26+
--> $DIR/pattern-errors.rs:24:22
2727
|
2828
LL | if let Some(Some(&mut x)) = &Some(Some(&mut 0)) {
2929
| ^^^^^
@@ -35,7 +35,7 @@ LL | if let Some(Some(&x)) = &Some(Some(&mut 0)) {
3535
| ~
3636

3737
error[E0308]: mismatched types
38-
--> $DIR/pattern-errors.rs:26:17
38+
--> $DIR/pattern-errors.rs:31:17
3939
|
4040
LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) {
4141
| ^^^^^
@@ -47,7 +47,7 @@ LL | if let Some(&Some(&_)) = &Some(&Some(0)) {
4747
| ~
4848

4949
error[E0308]: mismatched types
50-
--> $DIR/pattern-errors.rs:34:23
50+
--> $DIR/pattern-errors.rs:41:23
5151
|
5252
LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) {
5353
| ^^^^^
@@ -59,7 +59,7 @@ LL | if let Some(&Some(&_)) = &mut Some(&Some(0)) {
5959
| ~
6060

6161
error[E0308]: mismatched types
62-
--> $DIR/pattern-errors.rs:42:17
62+
--> $DIR/pattern-errors.rs:51:17
6363
|
6464
LL | if let Some(&mut Some(x)) = &Some(Some(0)) {
6565
| ^^^^^
@@ -71,7 +71,7 @@ LL | if let Some(&Some(x)) = &Some(Some(0)) {
7171
| ~
7272

7373
error[E0308]: mismatched types
74-
--> $DIR/pattern-errors.rs:123:10
74+
--> $DIR/pattern-errors.rs:147:10
7575
|
7676
LL | let [&mut x] = &[&mut 0];
7777
| ^^^^^
@@ -83,7 +83,7 @@ LL | let [&x] = &[&mut 0];
8383
| ~
8484

8585
error[E0308]: mismatched types
86-
--> $DIR/pattern-errors.rs:128:10
86+
--> $DIR/pattern-errors.rs:153:10
8787
|
8888
LL | let [&mut &x] = &[&mut 0];
8989
| ^^^^^
@@ -95,7 +95,7 @@ LL | let [&&x] = &[&mut 0];
9595
| ~
9696

9797
error[E0308]: mismatched types
98-
--> $DIR/pattern-errors.rs:133:10
98+
--> $DIR/pattern-errors.rs:159:10
9999
|
100100
LL | let [&mut &ref x] = &[&mut 0];
101101
| ^^^^^
@@ -107,7 +107,7 @@ LL | let [&&ref x] = &[&mut 0];
107107
| ~
108108

109109
error[E0308]: mismatched types
110-
--> $DIR/pattern-errors.rs:138:10
110+
--> $DIR/pattern-errors.rs:165:10
111111
|
112112
LL | let [&mut &(mut x)] = &[&mut 0];
113113
| ^^^^^

0 commit comments

Comments
 (0)