Skip to content

Commit 39c2785

Browse files
committed
add tests for refutable patterns
1 parent b3413c6 commit 39c2785

5 files changed

+172
-5
lines changed

tests/ui/pattern/issue-76342.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
#![allow(unused_variables)]
33
struct Zeroes;
44
impl Into<[usize; 2]> for Zeroes {
5-
fn into(self) -> [usize; 2] { [0; 2] }
5+
fn into(self) -> [usize; 2] {
6+
[0; 2]
7+
}
68
}
79
impl Into<[usize; 3]> for Zeroes {
8-
fn into(self) -> [usize; 3] { [0; 3] }
10+
fn into(self) -> [usize; 3] {
11+
[0; 3]
12+
}
913
}
1014
impl Into<[usize; 4]> for Zeroes {
11-
fn into(self) -> [usize; 4] { [0; 4] }
15+
fn into(self) -> [usize; 4] {
16+
[0; 4]
17+
}
1218
}
1319
fn main() {
14-
let [a, b, c] = Zeroes.into(); // ERROR: type annotations needed
15-
let [d, e, f]: [_; 3] = Zeroes.into(); // Works great
20+
let [a, b, c] = Zeroes.into();
21+
let [d, e, f]: [_; 3] = Zeroes.into();
1622
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![allow(unused_variables)]
2+
3+
struct Zeroes;
4+
5+
impl Into<[usize; 3]> for Zeroes {
6+
fn into(self) -> [usize; 3] {
7+
[0; 3]
8+
}
9+
}
10+
11+
fn let_else() {
12+
let [a, b, c] = Zeroes.into() else {
13+
//~^ ERROR type annotations needed
14+
unreachable!();
15+
};
16+
}
17+
18+
fn if_let() {
19+
if let [a, b, c] = Zeroes.into() {
20+
//~^ ERROR type annotations needed
21+
unreachable!();
22+
}
23+
}
24+
25+
fn if_let_else() {
26+
if let [a, b, c] = Zeroes.into() {
27+
//~^ ERROR type annotations needed
28+
unreachable!();
29+
} else {
30+
unreachable!();
31+
}
32+
}
33+
34+
fn main() {}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/slice-pattern-refutable.rs:12:9
3+
|
4+
LL | let [a, b, c] = Zeroes.into() else {
5+
| ^^^^^^^^^
6+
|
7+
help: consider giving this pattern a type
8+
|
9+
LL | let [a, b, c]: /* Type */ = Zeroes.into() else {
10+
| ++++++++++++
11+
12+
error[E0282]: type annotations needed
13+
--> $DIR/slice-pattern-refutable.rs:19:31
14+
|
15+
LL | if let [a, b, c] = Zeroes.into() {
16+
| --------- ^^^^
17+
| |
18+
| type must be known at this point
19+
|
20+
help: try using a fully qualified path to specify the expected types
21+
|
22+
LL | if let [a, b, c] = <Zeroes as Into<T>>::into(Zeroes) {
23+
| ++++++++++++++++++++++++++ ~
24+
25+
error[E0282]: type annotations needed
26+
--> $DIR/slice-pattern-refutable.rs:26:31
27+
|
28+
LL | if let [a, b, c] = Zeroes.into() {
29+
| --------- ^^^^
30+
| |
31+
| type must be known at this point
32+
|
33+
help: try using a fully qualified path to specify the expected types
34+
|
35+
LL | if let [a, b, c] = <Zeroes as Into<T>>::into(Zeroes) {
36+
| ++++++++++++++++++++++++++ ~
37+
38+
error: aborting due to 3 previous errors
39+
40+
For more information about this error, try `rustc --explain E0282`.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![allow(unused_variables)]
2+
3+
struct Zeroes;
4+
5+
const ARR: [usize; 2] = [0; 2];
6+
const ARR2: [usize; 2] = [2; 2];
7+
8+
impl Into<&'static [usize; 2]> for Zeroes {
9+
fn into(self) -> &'static [usize; 2] {
10+
&ARR
11+
}
12+
}
13+
14+
impl Into<&'static [usize]> for Zeroes {
15+
fn into(self) -> &'static [usize] {
16+
&ARR2
17+
}
18+
}
19+
20+
fn let_decl() {
21+
let &[a, b] = Zeroes.into();
22+
}
23+
24+
fn let_else() {
25+
let &[a, b] = Zeroes.into() else {
26+
//~^ ERROR type annotations needed
27+
unreachable!();
28+
};
29+
}
30+
31+
fn if_let() {
32+
if let &[a, b] = Zeroes.into() {
33+
//~^ ERROR type annotations needed
34+
unreachable!();
35+
}
36+
}
37+
38+
fn if_let_else() {
39+
if let &[a, b] = Zeroes.into() {
40+
//~^ ERROR type annotations needed
41+
unreachable!();
42+
} else {
43+
unreachable!();
44+
}
45+
}
46+
47+
fn main() {}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0282]: type annotations needed for `&_`
2+
--> $DIR/slice-patterns-ambiguity.rs:25:9
3+
|
4+
LL | let &[a, b] = Zeroes.into() else {
5+
| ^^^^^^^
6+
|
7+
help: consider giving this pattern a type, where the placeholders `_` are specified
8+
|
9+
LL | let &[a, b]: &_ = Zeroes.into() else {
10+
| ++++
11+
12+
error[E0282]: type annotations needed
13+
--> $DIR/slice-patterns-ambiguity.rs:32:29
14+
|
15+
LL | if let &[a, b] = Zeroes.into() {
16+
| ------ ^^^^
17+
| |
18+
| type must be known at this point
19+
|
20+
help: try using a fully qualified path to specify the expected types
21+
|
22+
LL | if let &[a, b] = <Zeroes as Into<&_>>::into(Zeroes) {
23+
| +++++++++++++++++++++++++++ ~
24+
25+
error[E0282]: type annotations needed
26+
--> $DIR/slice-patterns-ambiguity.rs:39:29
27+
|
28+
LL | if let &[a, b] = Zeroes.into() {
29+
| ------ ^^^^
30+
| |
31+
| type must be known at this point
32+
|
33+
help: try using a fully qualified path to specify the expected types
34+
|
35+
LL | if let &[a, b] = <Zeroes as Into<&_>>::into(Zeroes) {
36+
| +++++++++++++++++++++++++++ ~
37+
38+
error: aborting due to 3 previous errors
39+
40+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)