about summary refs log tree commit diff
path: root/tests/ui/pattern/slice-patterns-irrefutable.rs
blob: 7be02b44e43d72738ce86b4849e10f6c0c42b0f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 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 default() {
    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();
}

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() {}