about summary refs log tree commit diff
path: root/src/tools/miri/tests/fail/enum-set-discriminant-niche-variant-wrong.rs
blob: eca6d908b448b5dcf47502b521e7ed57371e2f9b (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
#![feature(core_intrinsics)]
#![feature(custom_mir)]

use std::intrinsics::mir::*;
use std::num::NonZero;

// We define our own option type so that we can control the variant indices.
#[allow(unused)]
enum Option<T> {
    None,    // variant 0
    Some(T), // variant 1
}
use Option::*;

#[custom_mir(dialect = "runtime", phase = "optimized")]
fn set_discriminant(ptr: &mut Option<NonZero<i32>>) {
    mir! {
        {
            // We set the discriminant to `Some`, which is a NOP since this is the niched variant.
            // However, the enum is actually encoding `None` currently! That's not good...
            SetDiscriminant(*ptr, 1);
            //~^ ERROR: trying to set discriminant of a Option<std::num::NonZero<i32>> to the niched variant, but the value does not match
            Return()
        }
    }
}

pub fn main() {
    let mut v = None;
    set_discriminant(&mut v);
}