about summary refs log tree commit diff
path: root/tests/ui/consts/const-eval/transmute-size-mismatch.rs
blob: 15b69af14e9c5a87f04d2af537d337a836b60222 (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
#![feature(core_intrinsics)]
#![feature(custom_mir)]

// These cases are statically rejected by `mem::transmute`, so we need custom
// MIR to be able to get to constant evaluation.
use std::intrinsics::mir::*;

#[custom_mir(dialect = "runtime", phase = "initial")]
const unsafe fn mir_transmute<T, U>(x: T) -> U {
    mir!{
        {
            RET = CastTransmute(x);
            //~^ NOTE inside `mir_transmute
            //~| NOTE inside `mir_transmute
            //~| NOTE the failure occurred here
            //~| NOTE the failure occurred here
            Return()
        }
    }
}

const FROM_BIGGER: u16 = unsafe { mir_transmute(123_i32) }; //~ NOTE failed inside this call
//~^ ERROR transmuting from 4-byte type to 2-byte type: `i32` -> `u16`

const FROM_SMALLER: u32 = unsafe { mir_transmute(123_i16) }; //~ NOTE failed inside this call
//~^ ERROR transmuting from 2-byte type to 4-byte type: `i16` -> `u32`

fn main() {}