File: allow-use-behind-cousin-variant.rs

package info (click to toggle)
rustc 1.86.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 913,560 kB
  • sloc: xml: 158,127; python: 35,921; javascript: 19,689; sh: 19,600; cpp: 18,906; ansic: 13,124; asm: 4,376; makefile: 708; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (59 lines) | stat: -rw-r--r-- 1,516 bytes parent folder | download | duplicates (10)
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
// rust-lang/rust#62614: we want to allow matching on constants of types that
// have non-structural-match variants, *if* the constant itself does not use
// any such variant.

// NOTE: for now, deliberately leaving the lint `indirect_structural_match` set
// to its default, so that we will not issue a diangostic even if
// rust-lang/rust#62614 remains an open issue.

//@ run-pass

struct Sum(u32, u32);

impl PartialEq for Sum {
    fn eq(&self, other: &Self) -> bool { self.0 + self.1 == other.0 + other.1 }
}

impl Eq for Sum { }

#[derive(PartialEq, Eq)]
enum Eek {
    TheConst,
    UnusedByTheConst(Sum)
}

const THE_CONST: Eek = Eek::TheConst;
const SUM_THREE: Eek = Eek::UnusedByTheConst(Sum(3,0));

const EEK_ZERO: &[Eek] = &[];
const EEK_ONE: &[Eek] = &[THE_CONST];

pub fn main() {
    match Eek::UnusedByTheConst(Sum(1,2)) {
        ref sum if sum == &SUM_THREE => { println!("Hello 0"); }
        _ => { println!("Gbye"); }
    }

    match Eek::TheConst {
        THE_CONST => { println!("Hello 1"); }
        _ => { println!("Gbye"); }
    }


    match & &Eek::TheConst {
        & & THE_CONST => { println!("Hello 2"); }
        _ => { println!("Gbye"); }
    }

    match & & &[][..] {
        & & EEK_ZERO => { println!("Hello 3"); }
        & & EEK_ONE => { println!("Gbye"); }
        _ => { println!("Gbye"); }
    }

    match & & &[Eek::TheConst][..] {
        & & EEK_ZERO => { println!("Gby"); }
        & & EEK_ONE => { println!("Hello 4"); }
        _ => { println!("Gbye"); }
    }
}