rust/tests/mir-opt/instsimplify/casts.rs
Scott McMurray 4630d1b23b Ban ArrayToPointer and MutToConstPointer from runtime MIR
Apparently MIR borrowck cares about at least one of these for checking variance.

In runtime MIR, though, there's no need for them as `PtrToPtr` does the same thing.

(Banning them simplifies passes like GVN that no longer need to handle multiple cast possibilities.)
2024-06-19 10:44:01 -07:00

35 lines
1.0 KiB
Rust

//@ test-mir-pass: InstSimplify
//@ compile-flags: -Zinline-mir
#![crate_type = "lib"]
#![feature(core_intrinsics)]
#[inline(always)]
fn generic_cast<T, U>(x: *const T) -> *const U {
x as *const U
}
// EMIT_MIR casts.redundant.InstSimplify.diff
pub fn redundant<'a, 'b: 'a>(x: *const &'a u8) -> *const &'a u8 {
// CHECK-LABEL: fn redundant(
// CHECK: inlined generic_cast
// CHECK-NOT: as
generic_cast::<&'a u8, &'b u8>(x) as *const &'a u8
}
// EMIT_MIR casts.roundtrip.InstSimplify.diff
pub fn roundtrip(x: *const u8) -> *const u8 {
// CHECK-LABEL: fn roundtrip(
// CHECK: _4 = _1;
// CHECK: _3 = move _4 as *mut u8 (PtrToPtr);
// CHECK: _2 = move _3 as *const u8 (PtrToPtr);
x as *mut u8 as *const u8
}
// EMIT_MIR casts.roundtrip.InstSimplify.diff
pub fn cast_thin_via_aggregate(x: *const u8) -> *const () {
// CHECK-LABEL: fn cast_thin_via_aggregate(
// CHECK: _2 = _1;
// CHECK: _0 = move _2 as *const () (PtrToPtr);
std::intrinsics::aggregate_raw_ptr(x, ())
}