diff options
Diffstat (limited to '')
-rw-r--r-- | tests/ui/pattern/slice-array-infer.rs | 27 | ||||
-rw-r--r-- | tests/ui/pattern/slice-pattern-refutable.rs | 36 | ||||
-rw-r--r-- | tests/ui/pattern/slice-pattern-refutable.stderr | 40 | ||||
-rw-r--r-- | tests/ui/pattern/slice-patterns-ambiguity.rs | 47 | ||||
-rw-r--r-- | tests/ui/pattern/slice-patterns-ambiguity.stderr | 40 | ||||
-rw-r--r-- | tests/ui/pattern/slice-patterns-irrefutable.rs | 74 | ||||
-rw-r--r-- | tests/ui/pattern/slice-patterns-irrefutable.stderr | 14 | ||||
-rw-r--r-- | tests/ui/pattern/slice-patterns-nested.rs | 15 |
8 files changed, 293 insertions, 0 deletions
diff --git a/tests/ui/pattern/slice-array-infer.rs b/tests/ui/pattern/slice-array-infer.rs new file mode 100644 index 000000000..f94a3dcfe --- /dev/null +++ b/tests/ui/pattern/slice-array-infer.rs @@ -0,0 +1,27 @@ +// check-pass + +#![allow(unused_variables)] +#![feature(generic_arg_infer)] + +struct Zeroes; +impl Into<&'static [usize; 3]> for Zeroes { + fn into(self) -> &'static [usize; 3] { + &[0; 3] + } +} +impl Into<[usize; 3]> for Zeroes { + fn into(self) -> [usize; 3] { + [0; 3] + } +} +fn main() { + let [a, b, c] = Zeroes.into(); + let [d, e, f] = <Zeroes as Into<&'static [usize; 3]>>::into(Zeroes); + let &[g, h, i] = Zeroes.into(); + let [j, k, l]: [usize; _] = Zeroes.into(); + let [m, n, o]: &[usize; _] = Zeroes.into(); + + // check the binding mode of these patterns: + let _: &[usize] = &[a, b, c, g, h, i, j, k, l]; + let _: &[&usize] = &[d, e, f, m, n, o]; +} diff --git a/tests/ui/pattern/slice-pattern-refutable.rs b/tests/ui/pattern/slice-pattern-refutable.rs new file mode 100644 index 000000000..1be3c6ef8 --- /dev/null +++ b/tests/ui/pattern/slice-pattern-refutable.rs @@ -0,0 +1,36 @@ +// Test that we do not infer the expected types of patterns to an array +// if we're in a refutable pattern. +#![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() {} diff --git a/tests/ui/pattern/slice-pattern-refutable.stderr b/tests/ui/pattern/slice-pattern-refutable.stderr new file mode 100644 index 000000000..df5b58d3e --- /dev/null +++ b/tests/ui/pattern/slice-pattern-refutable.stderr @@ -0,0 +1,40 @@ +error[E0282]: type annotations needed + --> $DIR/slice-pattern-refutable.rs:14: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:21: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:28: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`. diff --git a/tests/ui/pattern/slice-patterns-ambiguity.rs b/tests/ui/pattern/slice-patterns-ambiguity.rs new file mode 100644 index 000000000..0fe24b0e5 --- /dev/null +++ b/tests/ui/pattern/slice-patterns-ambiguity.rs @@ -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() {} diff --git a/tests/ui/pattern/slice-patterns-ambiguity.stderr b/tests/ui/pattern/slice-patterns-ambiguity.stderr new file mode 100644 index 000000000..3ef99d0e2 --- /dev/null +++ b/tests/ui/pattern/slice-patterns-ambiguity.stderr @@ -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`. diff --git a/tests/ui/pattern/slice-patterns-irrefutable.rs b/tests/ui/pattern/slice-patterns-irrefutable.rs new file mode 100644 index 000000000..bd230608e --- /dev/null +++ b/tests/ui/pattern/slice-patterns-irrefutable.rs @@ -0,0 +1,74 @@ +// Test that we infer the expected type of a pattern to an array of the given length. + +#![allow(unused_variables)] + +use std::array::TryFromSliceError; +use std::convert::TryInto; + +struct Zeroes; +impl Into<[usize; 2]> for Zeroes { + fn into(self) -> [usize; 2] { + [0; 2] + } +} +impl Into<[usize; 3]> for Zeroes { + fn into(self) -> [usize; 3] { + [0; 3] + } +} +impl Into<[usize; 4]> for Zeroes { + fn into(self) -> [usize; 4] { + [0; 4] + } +} + +fn zeroes_into() { + let [a, b, c] = Zeroes.into(); + let [d, e, f]: [_; 3] = Zeroes.into(); +} + +fn array_try_from(x: &[usize]) -> Result<usize, TryFromSliceError> { + let [a, b] = x.try_into()?; + Ok(a + b) +} + +fn destructuring_assignment() { + let a: i32; + let b; + [a, b] = Default::default(); +} + +fn test_nested_array() { + let a: [_; 3]; + let b; + //~^ ERROR type annotations needed + [a, b] = Default::default(); +} + +fn test_nested_array_type_hint() { + let a: [_; 3]; + let b; + [a, b] = Default::default(); + let _: i32 = b[1]; +} + +fn test_working_nested_array() { + let a: i32; + [[a, _, _], _, _] = Default::default(); +} + +struct Foo<T>([T; 2]); + +impl<T: Default + Copy> Default for Foo<T> { + fn default() -> Self { + Foo([Default::default(); 2]) + } +} + +fn field_array() { + let a: i32; + let b; + Foo([a, b]) = Default::default(); +} + +fn main() {} diff --git a/tests/ui/pattern/slice-patterns-irrefutable.stderr b/tests/ui/pattern/slice-patterns-irrefutable.stderr new file mode 100644 index 000000000..fac99534f --- /dev/null +++ b/tests/ui/pattern/slice-patterns-irrefutable.stderr @@ -0,0 +1,14 @@ +error[E0282]: type annotations needed for `[_; 3]` + --> $DIR/slice-patterns-irrefutable.rs:43:9 + | +LL | let b; + | ^ + | +help: consider giving `b` an explicit type, where the placeholders `_` are specified + | +LL | let b: [_; 3]; + | ++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0282`. diff --git a/tests/ui/pattern/slice-patterns-nested.rs b/tests/ui/pattern/slice-patterns-nested.rs new file mode 100644 index 000000000..077e0a139 --- /dev/null +++ b/tests/ui/pattern/slice-patterns-nested.rs @@ -0,0 +1,15 @@ +// check-pass +#![allow(unused_variables)] + +struct Zeroes; +struct Foo<T>(T); + +impl Into<[usize; 3]> for Zeroes { + fn into(self) -> [usize; 3] { + [0; 3] + } +} + +fn main() { + let Foo([a, b, c]) = Foo(Zeroes.into()); +} |