2023-05-06 22:20:58 -05:00
|
|
|
// unit-test: InstSimplify
|
2023-03-25 16:32:31 -05:00
|
|
|
// compile-flags: -C panic=abort
|
|
|
|
|
|
|
|
#![crate_type = "lib"]
|
|
|
|
#![feature(core_intrinsics)]
|
|
|
|
#![feature(custom_mir)]
|
|
|
|
|
|
|
|
use std::intrinsics::mir::*;
|
|
|
|
use std::mem::{MaybeUninit, ManuallyDrop, transmute};
|
|
|
|
|
2023-05-06 22:20:58 -05:00
|
|
|
// EMIT_MIR combine_transmutes.identity_transmutes.InstSimplify.diff
|
2023-03-25 16:32:31 -05:00
|
|
|
pub unsafe fn identity_transmutes() {
|
2023-10-16 14:50:54 -05:00
|
|
|
// CHECK-LABEL: fn identity_transmutes(
|
|
|
|
// CHECK-NOT: as i32 (Transmute);
|
|
|
|
// CHECK-NOT: as Vec<i32> (Transmute);
|
|
|
|
|
2023-03-25 16:32:31 -05:00
|
|
|
// These are nops and should be removed
|
|
|
|
let _a = transmute::<i32, i32>(1);
|
|
|
|
let _a = transmute::<Vec<i32>, Vec<i32>>(Vec::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[custom_mir(dialect = "runtime", phase = "initial")]
|
2023-05-06 22:20:58 -05:00
|
|
|
// EMIT_MIR combine_transmutes.integer_transmutes.InstSimplify.diff
|
2023-03-25 16:32:31 -05:00
|
|
|
pub unsafe fn integer_transmutes() {
|
2023-10-16 14:50:54 -05:00
|
|
|
// CHECK-LABEL: fn integer_transmutes(
|
|
|
|
// CHECK-NOT: _i32 as u32 (Transmute);
|
|
|
|
// CHECK: _i32 as u32 (IntToInt);
|
|
|
|
// CHECK: _i32 as i64 (Transmute);
|
|
|
|
// CHECK-NOT: _u64 as i64 (Transmute);
|
|
|
|
// CHECK: _u64 as i64 (IntToInt);
|
|
|
|
// CHECK: _u64 as u32 (Transmute);
|
|
|
|
// CHECK-NOT: _isize as usize (Transmute);
|
|
|
|
// CHECK: _isize as usize (IntToInt);
|
|
|
|
|
2023-03-25 16:32:31 -05:00
|
|
|
mir! {
|
|
|
|
{
|
|
|
|
let A = CastTransmute::<i32, u32>(1); // Can be a cast
|
|
|
|
let B = CastTransmute::<i32, i64>(1); // UB
|
|
|
|
let C = CastTransmute::<u64, i64>(1); // Can be a cast
|
|
|
|
let D = CastTransmute::<u64, u32>(1); // UB
|
|
|
|
let E = CastTransmute::<isize, usize>(1); // Can be a cast
|
|
|
|
Return()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 22:20:58 -05:00
|
|
|
// EMIT_MIR combine_transmutes.adt_transmutes.InstSimplify.diff
|
2023-03-25 16:32:31 -05:00
|
|
|
pub unsafe fn adt_transmutes() {
|
2023-10-16 14:50:54 -05:00
|
|
|
// CHECK-LABEL: fn adt_transmutes(
|
|
|
|
// CHECK: as u8 (Transmute);
|
|
|
|
// CHECK: ({{_.*}}.0: i16);
|
|
|
|
// CHECK: as u16 (Transmute);
|
|
|
|
// CHECK: as u32 (Transmute);
|
|
|
|
// CHECK: as i32 (Transmute);
|
|
|
|
// CHECK: ({{_.*}}.1: std::mem::ManuallyDrop<std::string::String>);
|
|
|
|
|
2023-03-25 16:32:31 -05:00
|
|
|
let _a: u8 = transmute(Some(std::num::NonZeroU8::MAX));
|
|
|
|
let _a: i16 = transmute(std::num::Wrapping(0_i16));
|
|
|
|
let _a: u16 = transmute(std::num::Wrapping(0_i16));
|
|
|
|
let _a: u32 = transmute(Union32 { i32: 0 });
|
|
|
|
let _a: i32 = transmute(Union32 { u32: 0 });
|
|
|
|
let _a: ManuallyDrop<String> = transmute(MaybeUninit::<String>::uninit());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub union Union32 { u32: u32, i32: i32 }
|