rust/tests/ui/consts/const-eval/transmute-size-mismatch.rs
Ralf Jung 17946c22b1 const-eval error: always say in which item the error occurred
also adjust the wording a little so that we don't say "the error occurred here" for two different spans
2025-06-07 13:42:30 +02:00

29 lines
938 B
Rust

#![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() {}