mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 17:03:35 +00:00
add tests for refutable patterns
This commit is contained in:
parent
b3413c643b
commit
39c2785aec
@ -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();
|
||||
}
|
||||
|
34
tests/ui/pattern/slice-pattern-refutable.rs
Normal file
34
tests/ui/pattern/slice-pattern-refutable.rs
Normal 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() {}
|
40
tests/ui/pattern/slice-pattern-refutable.stderr
Normal file
40
tests/ui/pattern/slice-pattern-refutable.stderr
Normal 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`.
|
47
tests/ui/pattern/slice-patterns-ambiguity.rs
Normal file
47
tests/ui/pattern/slice-patterns-ambiguity.rs
Normal 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() {}
|
40
tests/ui/pattern/slice-patterns-ambiguity.stderr
Normal file
40
tests/ui/pattern/slice-patterns-ambiguity.stderr
Normal 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`.
|
Loading…
Reference in New Issue
Block a user