about summary refs log tree commit diff
path: root/tests/ui/traits/const-traits/pattern-custom-partial-eq.rs
blob: 1003775be2f0ba7291e7abfc607e0b1c549d38ad (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
//! Ensure that a `const fn` can match on constants of a type that is `PartialEq`
//! but not `const PartialEq`. This is accepted for backwards compatibility reasons.
//@ check-pass
#![feature(const_trait_impl)]

#[derive(Eq, PartialEq)]
pub struct Y(u8);
pub const GREEN: Y = Y(4);
pub const fn is_green(x: Y) -> bool {
    match x { GREEN => true, _ => false }
}

struct CustomEq;

impl Eq for CustomEq {}
impl PartialEq for CustomEq {
    fn eq(&self, _: &Self) -> bool {
        false
    }
}

#[derive(PartialEq, Eq)]
#[allow(unused)]
enum Foo {
    Bar,
    Baz,
    Qux(CustomEq),
}

const BAR_BAZ: Foo = if 42 == 42 {
    Foo::Bar
} else {
    Foo::Qux(CustomEq) // dead arm
};

const EMPTY: &[CustomEq] = &[];

const fn test() {
    // BAR_BAZ itself is fine but the enum has other variants
    // that are non-structural. Still, this should be accepted.
    match Foo::Qux(CustomEq) {
        BAR_BAZ => panic!(),
        _ => {}
    }

    // Similarly, an empty slice of a type that is non-structural
    // is accepted.
    match &[CustomEq] as &[CustomEq] {
        EMPTY => panic!(),
        _ => {},
    }
}

fn main() {}