summaryrefslogtreecommitdiffstats
path: root/tests/ui/pattern/usefulness/integer-ranges/exhaustiveness.rs
blob: 0f5f49c4ca4731cd6daae00c5364a127bf084a7d (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#![feature(exclusive_range_pattern)]
#![allow(overlapping_range_endpoints)]
#![deny(unreachable_patterns)]

macro_rules! m {
    ($s:expr, $($t:tt)+) => {
        match $s { $($t)+ => {} }
    }
}

macro_rules! test_int {
    ($s:expr, $min:path, $max:path) => {
        m!($s, $min..=$max);
        m!($s, $min..5 | 5..=$max);
        m!($s, $min..=4 | 5..=$max);
        m!($s, $min..$max | $max);
        m!(($s, true), ($min..5, true) | (5..=$max, true) | ($min..=$max, false));
    }
}

fn main() {
    test_int!(0u8, u8::MIN, u8::MAX);
    test_int!(0u16, u16::MIN, u16::MAX);
    test_int!(0u32, u32::MIN, u32::MAX);
    test_int!(0u64, u64::MIN, u64::MAX);
    test_int!(0u128, u128::MIN, u128::MAX);

    test_int!(0i8, i8::MIN, i8::MAX);
    test_int!(0i16, i16::MIN, i16::MAX);
    test_int!(0i32, i32::MIN, i32::MAX);
    test_int!(0i64, i64::MIN, i64::MAX);
    test_int!(0i128, i128::MIN, i128::MAX);

    m!('a', '\u{0}'..=char::MAX);
    m!('a', '\u{0}'..='\u{10_FFFF}');
    // We can get away with just covering the following two ranges, which correspond to all valid
    // Unicode Scalar Values.
    m!('a', '\u{0}'..='\u{D7FF}' | '\u{E000}'..=char::MAX);
    m!('a', '\u{0}'..'\u{D7FF}' | '\u{D7FF}' | '\u{E000}'..=char::MAX);

    let 0..=255 = 0u8;
    let -128..=127 = 0i8;
    let -2147483648..=2147483647 = 0i32;
    let '\u{0000}'..='\u{10FFFF}' = 'v';

    // Almost exhaustive
    m!(0u8, 0..255); //~ ERROR non-exhaustive patterns
    m!(0u8, 0..=254); //~ ERROR non-exhaustive patterns
    m!(0u8, 1..=255); //~ ERROR non-exhaustive patterns
    m!(0u8, 0..42 | 43..=255); //~ ERROR non-exhaustive patterns
    m!(0i8, -128..127); //~ ERROR non-exhaustive patterns
    m!(0i8, -128..=126); //~ ERROR non-exhaustive patterns
    m!(0i8, -127..=127); //~ ERROR non-exhaustive patterns
    match 0i8 { //~ ERROR non-exhaustive patterns
        i8::MIN ..= -1 => {}
        1 ..= i8::MAX => {}
    }
    const ALMOST_MAX: u128 = u128::MAX - 1;
    m!(0u128, 0..=ALMOST_MAX); //~ ERROR non-exhaustive patterns
    m!(0u128, 0..=4); //~ ERROR non-exhaustive patterns
    m!(0u128, 1..=u128::MAX); //~ ERROR non-exhaustive patterns

    // More complicatedly (non-)exhaustive
    match 0u8 {
        0 ..= 30 => {}
        20 ..= 70 => {}
        50 ..= 255 => {}
    }
    match (0u8, true) { //~ ERROR non-exhaustive patterns
        (0 ..= 125, false) => {}
        (128 ..= 255, false) => {}
        (0 ..= 255, true) => {}
    }
    match (0u8, true) { // ok
        (0 ..= 125, false) => {}
        (128 ..= 255, false) => {}
        (0 ..= 255, true) => {}
        (125 .. 128, false) => {}
    }
    match (true, 0u8) {
        (true, 0 ..= 255) => {}
        (false, 0 ..= 125) => {}
        (false, 128 ..= 255) => {}
        (false, 125 .. 128) => {}
    }
    match Some(0u8) {
        None => {}
        Some(0 ..= 125) => {}
        Some(128 ..= 255) => {}
        Some(125 .. 128) => {}
    }
    const FOO: u8 = 41;
    const BAR: &u8 = &42;
    match &0u8 {
        0..41 => {}
        &FOO => {}
        BAR => {}
        43..=255 => {}
    }

}