add tests for refutable patterns

This commit is contained in:
b-naber 2023-06-30 11:42:37 +00:00
parent b3413c643b
commit 39c2785aec
5 changed files with 172 additions and 5 deletions

View file

@ -2,15 +2,21 @@
#![allow(unused_variables)]
struct Zeroes;
impl Into<[usize; 2]> for Zeroes {
fn into(self) -> [usize; 2] { [0; 2] }
fn into(self) -> [usize; 2] {
[0; 2]
}
}
impl Into<[usize; 3]> for Zeroes {
fn into(self) -> [usize; 3] { [0; 3] }
fn into(self) -> [usize; 3] {
[0; 3]
}
}
impl Into<[usize; 4]> for Zeroes {
fn into(self) -> [usize; 4] { [0; 4] }
fn into(self) -> [usize; 4] {
[0; 4]
}
}
fn main() {
let [a, b, c] = Zeroes.into(); // ERROR: type annotations needed
let [d, e, f]: [_; 3] = Zeroes.into(); // Works great
let [a, b, c] = Zeroes.into();
let [d, e, f]: [_; 3] = Zeroes.into();
}

View file

@ -0,0 +1,34 @@
#![allow(unused_variables)]
struct Zeroes;
impl Into<[usize; 3]> for Zeroes {
fn into(self) -> [usize; 3] {
[0; 3]
}
}
fn let_else() {
let [a, b, c] = Zeroes.into() else {
//~^ ERROR type annotations needed
unreachable!();
};
}
fn if_let() {
if let [a, b, c] = Zeroes.into() {
//~^ ERROR type annotations needed
unreachable!();
}
}
fn if_let_else() {
if let [a, b, c] = Zeroes.into() {
//~^ ERROR type annotations needed
unreachable!();
} else {
unreachable!();
}
}
fn main() {}

View file

@ -0,0 +1,40 @@
error[E0282]: type annotations needed
--> $DIR/slice-pattern-refutable.rs:12:9
|
LL | let [a, b, c] = Zeroes.into() else {
| ^^^^^^^^^
|
help: consider giving this pattern a type
|
LL | let [a, b, c]: /* Type */ = Zeroes.into() else {
| ++++++++++++
error[E0282]: type annotations needed
--> $DIR/slice-pattern-refutable.rs:19:31
|
LL | if let [a, b, c] = Zeroes.into() {
| --------- ^^^^
| |
| type must be known at this point
|
help: try using a fully qualified path to specify the expected types
|
LL | if let [a, b, c] = <Zeroes as Into<T>>::into(Zeroes) {
| ++++++++++++++++++++++++++ ~
error[E0282]: type annotations needed
--> $DIR/slice-pattern-refutable.rs:26:31
|
LL | if let [a, b, c] = Zeroes.into() {
| --------- ^^^^
| |
| type must be known at this point
|
help: try using a fully qualified path to specify the expected types
|
LL | if let [a, b, c] = <Zeroes as Into<T>>::into(Zeroes) {
| ++++++++++++++++++++++++++ ~
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0282`.

View file

@ -0,0 +1,47 @@
#![allow(unused_variables)]
struct Zeroes;
const ARR: [usize; 2] = [0; 2];
const ARR2: [usize; 2] = [2; 2];
impl Into<&'static [usize; 2]> for Zeroes {
fn into(self) -> &'static [usize; 2] {
&ARR
}
}
impl Into<&'static [usize]> for Zeroes {
fn into(self) -> &'static [usize] {
&ARR2
}
}
fn let_decl() {
let &[a, b] = Zeroes.into();
}
fn let_else() {
let &[a, b] = Zeroes.into() else {
//~^ ERROR type annotations needed
unreachable!();
};
}
fn if_let() {
if let &[a, b] = Zeroes.into() {
//~^ ERROR type annotations needed
unreachable!();
}
}
fn if_let_else() {
if let &[a, b] = Zeroes.into() {
//~^ ERROR type annotations needed
unreachable!();
} else {
unreachable!();
}
}
fn main() {}

View file

@ -0,0 +1,40 @@
error[E0282]: type annotations needed for `&_`
--> $DIR/slice-patterns-ambiguity.rs:25:9
|
LL | let &[a, b] = Zeroes.into() else {
| ^^^^^^^
|
help: consider giving this pattern a type, where the placeholders `_` are specified
|
LL | let &[a, b]: &_ = Zeroes.into() else {
| ++++
error[E0282]: type annotations needed
--> $DIR/slice-patterns-ambiguity.rs:32:29
|
LL | if let &[a, b] = Zeroes.into() {
| ------ ^^^^
| |
| type must be known at this point
|
help: try using a fully qualified path to specify the expected types
|
LL | if let &[a, b] = <Zeroes as Into<&_>>::into(Zeroes) {
| +++++++++++++++++++++++++++ ~
error[E0282]: type annotations needed
--> $DIR/slice-patterns-ambiguity.rs:39:29
|
LL | if let &[a, b] = Zeroes.into() {
| ------ ^^^^
| |
| type must be known at this point
|
help: try using a fully qualified path to specify the expected types
|
LL | if let &[a, b] = <Zeroes as Into<&_>>::into(Zeroes) {
| +++++++++++++++++++++++++++ ~
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0282`.