summaryrefslogtreecommitdiffstats
path: root/tests/ui/consts/const_discriminant.rs
blob: 79e68590e85d4ff0dcb25f0d02d9ae7b1056c2a6 (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
// run-pass
#![feature(const_discriminant)]
#![allow(dead_code)]

use std::mem::{discriminant, Discriminant};
use std::hint::black_box;

enum Test {
    A(u8),
    B,
    C { a: u8, b: u8 },
}

const TEST_A: Discriminant<Test> = discriminant(&Test::A(5));
const TEST_A_OTHER: Discriminant<Test> = discriminant(&Test::A(17));
const TEST_B: Discriminant<Test> = discriminant(&Test::B);

enum Void {}

enum SingleVariant {
    V,
    Never(Void),
}

const TEST_V: Discriminant<SingleVariant> = discriminant(&SingleVariant::V);

pub const TEST_VOID: () = {
    // This is UB, but CTFE does not check validity so it does not detect this.
    // This is a regression test for https://github.com/rust-lang/rust/issues/89765.
    unsafe { std::mem::discriminant(&*(&() as *const () as *const Void)); };
};


fn main() {
    assert_eq!(TEST_A, TEST_A_OTHER);
    assert_eq!(TEST_A, discriminant(black_box(&Test::A(17))));
    assert_eq!(TEST_B, discriminant(black_box(&Test::B)));
    assert_ne!(TEST_A, TEST_B);
    assert_ne!(TEST_B, discriminant(black_box(&Test::C { a: 42, b: 7 })));

    assert_eq!(TEST_V, discriminant(black_box(&SingleVariant::V)));
}