summaryrefslogtreecommitdiffstats
path: root/tests/ui/enum-discriminant/discriminant_value.rs
blob: f3dfac298ad7b4b70c090d67b537d8dd0b87006e (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
// run-pass
#![allow(stable_features)]
#![feature(core, core_intrinsics)]

extern crate core;
use core::intrinsics::discriminant_value;

enum CLike1 {
    A,
    B,
    C,
    D
}

enum CLike2 {
    A = 5,
    B = 2,
    C = 19,
    D
}

#[repr(i8)]
enum CLike3 {
    A = 5,
    B,
    C = -1,
    D
}

#[allow(unused_tuple_struct_fields)]
enum ADT {
    First(u32, u32),
    Second(u64)
}

enum NullablePointer {
    Something(#[allow(unused_tuple_struct_fields)] &'static u32),
    Nothing
}

static CONST : u32 = 0xBEEF;

#[allow(dead_code)]
#[repr(isize)]
enum Mixed {
    Unit = 3,
    Tuple(u16) = 2,
    Struct {
        a: u8,
        b: u16,
    } = 1,
}

pub fn main() {
    assert_eq!(discriminant_value(&CLike1::A), 0isize);
    assert_eq!(discriminant_value(&CLike1::B), 1);
    assert_eq!(discriminant_value(&CLike1::C), 2);
    assert_eq!(discriminant_value(&CLike1::D), 3);

    assert_eq!(discriminant_value(&CLike2::A), 5isize);
    assert_eq!(discriminant_value(&CLike2::B), 2);
    assert_eq!(discriminant_value(&CLike2::C), 19);
    assert_eq!(discriminant_value(&CLike2::D), 20);

    assert_eq!(discriminant_value(&CLike3::A), 5i8);
    assert_eq!(discriminant_value(&CLike3::B), 6);
    assert_eq!(discriminant_value(&CLike3::C), -1);
    assert_eq!(discriminant_value(&CLike3::D), 0);

    assert_eq!(discriminant_value(&ADT::First(0,0)), 0isize);
    assert_eq!(discriminant_value(&ADT::Second(5)), 1);

    assert_eq!(discriminant_value(&NullablePointer::Nothing), 1isize);
    assert_eq!(discriminant_value(&NullablePointer::Something(&CONST)), 0);

    assert_eq!(discriminant_value(&10), 0u8);
    assert_eq!(discriminant_value(&"test"), 0u8);

    assert_eq!(discriminant_value(&Mixed::Unit), 3isize);
    assert_eq!(discriminant_value(&Mixed::Tuple(5)), 2);
    assert_eq!(discriminant_value(&Mixed::Struct{a: 7, b: 11}), 1);
}