Skip to content

Commit b350d94

Browse files
committed
reorganize and comment some of the experimental pattern typing tests
This only includes previously existing tests (with a couple duplicates removed). I plan on adding more comprarisons where the rules differ once I've updated the pattern typing rules. I also haven't touched the tests for new rules in old editions; I'll see how best to handle that once those rules are updated as well.
1 parent 5dfb972 commit b350d94

18 files changed

+529
-497
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0507]: cannot move out of a shared reference
2-
--> $DIR/ref_pat_eat_one_layer_2024_fail2.rs:6:29
2+
--> $DIR/borrowck-errors.rs:9: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/ref_pat_eat_one_layer_2024_fail2.rs:11:10
17+
--> $DIR/borrowck-errors.rs:14:10
1818
|
1919
LL | let &ref mut x = &0;
2020
| ^^^^^^^^^ cannot borrow as mutable
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//@ edition: 2024
2+
//@ revisions: classic structural
3+
//! Tests for pattern errors not handled by the pattern typing rules, but by borrowck.
24
#![allow(incomplete_features)]
3-
#![feature(ref_pat_eat_one_layer_2024)]
5+
#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))]
6+
#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))]
47

58
pub fn main() {
69
if let Some(&Some(x)) = Some(&Some(&mut 0)) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0507]: cannot move out of a shared reference
2+
--> $DIR/borrowck-errors.rs:9:29
3+
|
4+
LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) {
5+
| - ^^^^^^^^^^^^^^^^^^^
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 removing the borrow
11+
|
12+
LL - if let Some(&Some(x)) = Some(&Some(&mut 0)) {
13+
LL + if let Some(Some(x)) = Some(&Some(&mut 0)) {
14+
|
15+
16+
error[E0596]: cannot borrow data in a `&` reference as mutable
17+
--> $DIR/borrowck-errors.rs:14:10
18+
|
19+
LL | let &ref mut x = &0;
20+
| ^^^^^^^^^ cannot borrow as mutable
21+
22+
error: aborting due to 2 previous errors
23+
24+
Some errors have detailed explanations: E0507, E0596.
25+
For more information about an error, try `rustc --explain E0507`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/cannot-mutably-deref-shared-ref.rs:9:9
3+
|
4+
LL | let &mut _ = &&0;
5+
| ^^^^^^ --- this expression has type `&&{integer}`
6+
| |
7+
| types differ in mutability
8+
|
9+
= note: expected reference `&&{integer}`
10+
found mutable reference `&mut _`
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/cannot-mutably-deref-shared-ref.rs:12:9
14+
|
15+
LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0;
16+
| ^^^^^^ ----------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}`
17+
| |
18+
| types differ in mutability
19+
|
20+
= note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}`
21+
found mutable reference `&mut _`
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/cannot-mutably-deref-shared-ref.rs:15:9
25+
|
26+
LL | let &mut _ = &&mut 0;
27+
| ^^^^^^ ------- this expression has type `&&mut {integer}`
28+
| |
29+
| types differ in mutability
30+
|
31+
= note: expected reference `&&mut {integer}`
32+
found mutable reference `&mut _`
33+
34+
error[E0308]: mismatched types
35+
--> $DIR/cannot-mutably-deref-shared-ref.rs:18:9
36+
|
37+
LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0;
38+
| ^^^^^^ --------------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}`
39+
| |
40+
| types differ in mutability
41+
|
42+
= note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}`
43+
found mutable reference `&mut _`
44+
45+
error[E0308]: mismatched types
46+
--> $DIR/cannot-mutably-deref-shared-ref.rs:21:14
47+
|
48+
LL | let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0;
49+
| ^^^^^^^^^^^^^^^^ -------------------------- this expression has type `&mut &&&&mut &&&mut &mut {integer}`
50+
| |
51+
| types differ in mutability
52+
|
53+
= note: expected reference `&&&&mut &&&mut &mut {integer}`
54+
found mutable reference `&mut _`
55+
56+
error: aborting due to 5 previous errors
57+
58+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ edition: 2024
2+
//@ revisions: classic structural
3+
//! Test that `&mut` patterns don't match shared reference types under new typing rules in Rust 2024
4+
#![allow(incomplete_features)]
5+
#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))]
6+
#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))]
7+
8+
pub fn main() {
9+
let &mut _ = &&0;
10+
//~^ ERROR: mismatched types
11+
12+
let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0;
13+
//~^ ERROR: mismatched types
14+
15+
let &mut _ = &&mut 0;
16+
//~^ ERROR: mismatched types
17+
18+
let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0;
19+
//~^ ERROR: mismatched types
20+
21+
let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0;
22+
//~^ ERROR: mismatched types
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/cannot-mutably-deref-shared-ref.rs:9:9
3+
|
4+
LL | let &mut _ = &&0;
5+
| ^^^^^^ --- this expression has type `&&{integer}`
6+
| |
7+
| types differ in mutability
8+
|
9+
= note: expected reference `&&{integer}`
10+
found mutable reference `&mut _`
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/cannot-mutably-deref-shared-ref.rs:12:9
14+
|
15+
LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&0;
16+
| ^^^^^^ ----------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}`
17+
| |
18+
| types differ in mutability
19+
|
20+
= note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&{integer}`
21+
found mutable reference `&mut _`
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/cannot-mutably-deref-shared-ref.rs:15:9
25+
|
26+
LL | let &mut _ = &&mut 0;
27+
| ^^^^^^ ------- this expression has type `&&mut {integer}`
28+
| |
29+
| types differ in mutability
30+
|
31+
= note: expected reference `&&mut {integer}`
32+
found mutable reference `&mut _`
33+
34+
error[E0308]: mismatched types
35+
--> $DIR/cannot-mutably-deref-shared-ref.rs:18:9
36+
|
37+
LL | let &mut _ = &&&&&&&&&&&&&&&&&&&&&&&&&&&&mut 0;
38+
| ^^^^^^ --------------------------------- this expression has type `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}`
39+
| |
40+
| types differ in mutability
41+
|
42+
= note: expected reference `&&&&&&&&&&&&&&&&&&&&&&&&&&&&mut {integer}`
43+
found mutable reference `&mut _`
44+
45+
error[E0308]: mismatched types
46+
--> $DIR/cannot-mutably-deref-shared-ref.rs:21:14
47+
|
48+
LL | let &mut &mut &mut &mut _ = &mut &&&&mut &&&mut &mut 0;
49+
| ^^^^^^^^^^^^^^^^ -------------------------- this expression has type `&mut &&&&mut &&&mut &mut {integer}`
50+
| |
51+
| types differ in mutability
52+
|
53+
= note: expected reference `&&&&mut &&&mut &mut {integer}`
54+
found mutable reference `&mut _`
55+
56+
error: aborting due to 5 previous errors
57+
58+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0658]: binding cannot be both mutable and by-reference
2+
--> $DIR/mut-ref-mut.rs:11:13
3+
|
4+
LL | let Foo(mut a) = &Foo(0);
5+
| ^^^^
6+
|
7+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
8+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: binding cannot be both mutable and by-reference
12+
--> $DIR/mut-ref-mut.rs:15:13
13+
|
14+
LL | let Foo(mut a) = &mut Foo(0);
15+
| ^^^^
16+
|
17+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
18+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ edition: 2024
2+
//@ revisions: classic structural
3+
//! Test diagnostics for binding with `mut` when the default binding mode is by-ref.
4+
#![allow(incomplete_features)]
5+
#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))]
6+
#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))]
7+
8+
pub fn main() {
9+
struct Foo(u8);
10+
11+
let Foo(mut a) = &Foo(0);
12+
//~^ ERROR: binding cannot be both mutable and by-reference
13+
a = &42;
14+
15+
let Foo(mut a) = &mut Foo(0);
16+
//~^ ERROR: binding cannot be both mutable and by-reference
17+
a = &mut 42;
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0658]: binding cannot be both mutable and by-reference
2+
--> $DIR/mut-ref-mut.rs:11:13
3+
|
4+
LL | let Foo(mut a) = &Foo(0);
5+
| ^^^^
6+
|
7+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
8+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: binding cannot be both mutable and by-reference
12+
--> $DIR/mut-ref-mut.rs:15:13
13+
|
14+
LL | let Foo(mut a) = &mut Foo(0);
15+
| ^^^^
16+
|
17+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
18+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/pattern-errors.rs:15:17
3+
|
4+
LL | if let Some(&mut x) = &Some(&mut 0) {
5+
| ^^^^^
6+
|
7+
= note: cannot match inherited `&` with `&mut` pattern
8+
help: replace this `&mut` pattern with `&`
9+
|
10+
LL | if let Some(&x) = &Some(&mut 0) {
11+
| ~
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/pattern-errors.rs:19:17
15+
|
16+
LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
17+
| ^^^^^
18+
|
19+
= note: cannot match inherited `&` with `&mut` pattern
20+
help: replace this `&mut` pattern with `&`
21+
|
22+
LL | if let Some(&Some(&x)) = &Some(&mut Some(0)) {
23+
| ~
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/pattern-errors.rs:23:22
27+
|
28+
LL | if let Some(Some(&mut x)) = &Some(Some(&mut 0)) {
29+
| ^^^^^
30+
|
31+
= note: cannot match inherited `&` with `&mut` pattern
32+
help: replace this `&mut` pattern with `&`
33+
|
34+
LL | if let Some(Some(&x)) = &Some(Some(&mut 0)) {
35+
| ~
36+
37+
error[E0308]: mismatched types
38+
--> $DIR/pattern-errors.rs:28:17
39+
|
40+
LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) {
41+
| ^^^^^
42+
|
43+
= note: cannot match inherited `&` with `&mut` pattern
44+
help: replace this `&mut` pattern with `&`
45+
|
46+
LL | if let Some(&Some(&_)) = &Some(&Some(0)) {
47+
| ~
48+
49+
error[E0308]: mismatched types
50+
--> $DIR/pattern-errors.rs:31:23
51+
|
52+
LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) {
53+
| ^^^^^
54+
|
55+
= note: cannot match inherited `&` with `&mut` pattern
56+
help: replace this `&mut` pattern with `&`
57+
|
58+
LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) {
59+
| ~
60+
61+
error[E0308]: mismatched types
62+
--> $DIR/pattern-errors.rs:34:23
63+
|
64+
LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) {
65+
| ^^^^^
66+
|
67+
= note: cannot match inherited `&` with `&mut` pattern
68+
help: replace this `&mut` pattern with `&`
69+
|
70+
LL | if let Some(&Some(&_)) = &mut Some(&Some(0)) {
71+
| ~
72+
73+
error[E0308]: mismatched types
74+
--> $DIR/pattern-errors.rs:37:29
75+
|
76+
LL | if let Some(&Some(Some((&mut _)))) = &Some(Some(&mut Some(0))) {
77+
| ^^^^^
78+
|
79+
= note: cannot match inherited `&` with `&mut` pattern
80+
help: replace this `&mut` pattern with `&`
81+
|
82+
LL | if let Some(&Some(Some((&_)))) = &Some(Some(&mut Some(0))) {
83+
| ~
84+
85+
error[E0308]: mismatched types
86+
--> $DIR/pattern-errors.rs:40:17
87+
|
88+
LL | if let Some(&mut Some(x)) = &Some(Some(0)) {
89+
| ^^^^^
90+
|
91+
= note: cannot match inherited `&` with `&mut` pattern
92+
help: replace this `&mut` pattern with `&`
93+
|
94+
LL | if let Some(&Some(x)) = &Some(Some(0)) {
95+
| ~
96+
97+
error[E0308]: mismatched types
98+
--> $DIR/pattern-errors.rs:43:17
99+
|
100+
LL | if let Some(&mut Some(x)) = &Some(Some(0)) {
101+
| ^^^^^
102+
|
103+
= note: cannot match inherited `&` with `&mut` pattern
104+
help: replace this `&mut` pattern with `&`
105+
|
106+
LL | if let Some(&Some(x)) = &Some(Some(0)) {
107+
| ~
108+
109+
error: aborting due to 9 previous errors
110+
111+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)