Skip to content

Commit e1e7553

Browse files
committed
add more tests
1 parent 96af415 commit e1e7553

6 files changed

+106
-49
lines changed

tests/ui/destructuring-assignment/slice_destructure_fail.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ fn main() {
33
[a, .., b, ..] = [0, 1]; //~ ERROR `..` can only be used once per slice pattern
44
[a, a, b] = [1, 2];
55
//~^ ERROR pattern requires 3 elements but array has 2
6-
//~| ERROR mismatched types
76
[_] = [1, 2];
87
//~^ ERROR pattern requires 1 element but array has 2
9-
//~| ERROR mismatched types
108
}

tests/ui/destructuring-assignment/slice_destructure_fail.stderr

+3-22
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,18 @@ LL | [a, .., b, ..] = [0, 1];
66
| |
77
| previously used here
88

9-
error[E0308]: mismatched types
10-
--> $DIR/slice_destructure_fail.rs:4:5
11-
|
12-
LL | [a, a, b] = [1, 2];
13-
| ^^^^^^^^^ expected an array with a fixed size of 2 elements, found one with 3 elements
14-
|
15-
= note: expected array `[{integer}; 2]`
16-
found array `[_; 3]`
17-
189
error[E0527]: pattern requires 3 elements but array has 2
1910
--> $DIR/slice_destructure_fail.rs:4:5
2011
|
2112
LL | [a, a, b] = [1, 2];
2213
| ^^^^^^^^^ expected 2 elements
2314

24-
error[E0308]: mismatched types
25-
--> $DIR/slice_destructure_fail.rs:7:5
26-
|
27-
LL | [_] = [1, 2];
28-
| ^^^ expected an array with a fixed size of 2 elements, found one with 1 element
29-
|
30-
= note: expected array `[{integer}; 2]`
31-
found array `[_; 1]`
32-
3315
error[E0527]: pattern requires 1 element but array has 2
34-
--> $DIR/slice_destructure_fail.rs:7:5
16+
--> $DIR/slice_destructure_fail.rs:6:5
3517
|
3618
LL | [_] = [1, 2];
3719
| ^^^ expected 2 elements
3820

39-
error: aborting due to 5 previous errors
21+
error: aborting due to 3 previous errors
4022

41-
Some errors have detailed explanations: E0308, E0527.
42-
For more information about an error, try `rustc --explain E0308`.
23+
For more information about this error, try `rustc --explain E0527`.

tests/ui/pattern/issue-76342.rs

-25
This file was deleted.

tests/ui/pattern/slice-array-infer.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// check-pass
2+
3+
#![allow(unused_variables)]
4+
#![feature(generic_arg_infer)]
5+
6+
struct Zeroes;
7+
impl Into<&'static [usize; 3]> for Zeroes {
8+
fn into(self) -> &'static [usize; 3] {
9+
&[0; 3]
10+
}
11+
}
12+
impl Into<[usize; 3]> for Zeroes {
13+
fn into(self) -> [usize; 3] {
14+
[0; 3]
15+
}
16+
}
17+
fn main() {
18+
let [a, b, c] = Zeroes.into();
19+
let [d, e, f] = <Zeroes as Into<&'static [usize; 3]>>::into(Zeroes);
20+
let &[g, h, i] = Zeroes.into();
21+
let [j, k, l]: [usize; _] = Zeroes.into();
22+
let [m, n, o]: &[usize; _] = Zeroes.into();
23+
24+
// check the binding mode of these patterns:
25+
let _: &[usize] = &[a, b, c, g, h, i, j, k, l];
26+
let _: &[&usize] = &[d, e, f, m, n, o];
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Test that we infer the expected type of a pattern to an array of the given length.
2+
3+
#![allow(unused_variables)]
4+
5+
use std::array::TryFromSliceError;
6+
use std::convert::TryInto;
7+
8+
struct Zeroes;
9+
impl Into<[usize; 2]> for Zeroes {
10+
fn into(self) -> [usize; 2] {
11+
[0; 2]
12+
}
13+
}
14+
impl Into<[usize; 3]> for Zeroes {
15+
fn into(self) -> [usize; 3] {
16+
[0; 3]
17+
}
18+
}
19+
impl Into<[usize; 4]> for Zeroes {
20+
fn into(self) -> [usize; 4] {
21+
[0; 4]
22+
}
23+
}
24+
25+
fn zeroes_into() {
26+
let [a, b, c] = Zeroes.into();
27+
let [d, e, f]: [_; 3] = Zeroes.into();
28+
}
29+
30+
fn array_try_from(x: &[usize]) -> Result<usize, TryFromSliceError> {
31+
let [a, b] = x.try_into()?;
32+
Ok(a + b)
33+
}
34+
35+
fn default() {
36+
let a: i32;
37+
let b;
38+
[a, b] = Default::default();
39+
}
40+
41+
fn test_nested_array() {
42+
let a: [_; 3];
43+
let b;
44+
//~^ ERROR type annotations needed
45+
[a, b] = Default::default();
46+
}
47+
48+
struct Foo<T>([T; 2]);
49+
50+
impl<T: Default + Copy> Default for Foo<T> {
51+
fn default() -> Self {
52+
Foo([Default::default(); 2])
53+
}
54+
}
55+
56+
fn field_array() {
57+
let a: i32;
58+
let b;
59+
Foo([a, b]) = Default::default();
60+
}
61+
62+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed for `[_; 3]`
2+
--> $DIR/slice-patterns-irrefutable.rs:43:9
3+
|
4+
LL | let b;
5+
| ^
6+
|
7+
help: consider giving `b` an explicit type, where the placeholders `_` are specified
8+
|
9+
LL | let b: [_; 3];
10+
| ++++++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)