2022-08-09 08:40:44 -05:00
|
|
|
// revisions: no_flag with_flag
|
2022-11-20 02:54:45 -06:00
|
|
|
// [no_flag] check-pass
|
2022-08-09 08:40:44 -05:00
|
|
|
// [with_flag] compile-flags: -Zextra-const-ub-checks
|
2022-09-04 12:13:44 -05:00
|
|
|
#![feature(never_type)]
|
2022-08-07 07:30:03 -05:00
|
|
|
|
|
|
|
use std::mem::transmute;
|
2023-07-24 04:44:58 -05:00
|
|
|
use std::ptr::addr_of;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum E { A, B }
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum Never {}
|
|
|
|
|
2023-08-02 09:14:36 -05:00
|
|
|
#[repr(usize)]
|
|
|
|
enum PtrSizedEnum { V }
|
|
|
|
|
2023-07-24 04:44:58 -05:00
|
|
|
// An enum with uninhabited variants but also at least 2 inhabited variants -- so the uninhabited
|
|
|
|
// variants *do* have a discriminant.
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum UninhDiscriminant {
|
|
|
|
A,
|
|
|
|
B(!),
|
|
|
|
C,
|
|
|
|
D(Never),
|
|
|
|
}
|
2022-08-07 07:30:03 -05:00
|
|
|
|
|
|
|
const INVALID_BOOL: () = unsafe {
|
|
|
|
let _x: bool = transmute(3u8);
|
2022-08-09 08:40:44 -05:00
|
|
|
//[with_flag]~^ ERROR: evaluation of constant value failed
|
|
|
|
//[with_flag]~| invalid value
|
2022-08-07 07:30:03 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const INVALID_PTR_IN_INT: () = unsafe {
|
|
|
|
let _x: usize = transmute(&3u8);
|
2022-09-21 06:05:20 -05:00
|
|
|
//[with_flag]~^ ERROR: evaluation of constant value failed
|
2023-08-02 09:14:36 -05:00
|
|
|
//[with_flag]~| invalid value
|
|
|
|
};
|
|
|
|
|
|
|
|
const INVALID_PTR_IN_ENUM: () = unsafe {
|
|
|
|
let _x: PtrSizedEnum = transmute(&3u8);
|
|
|
|
//[with_flag]~^ ERROR: evaluation of constant value failed
|
|
|
|
//[with_flag]~| invalid value
|
2022-08-07 07:30:03 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const INVALID_SLICE_TO_USIZE_TRANSMUTE: () = unsafe {
|
|
|
|
let x: &[u8] = &[0; 32];
|
|
|
|
let _x: (usize, usize) = transmute(x);
|
2022-09-21 06:05:20 -05:00
|
|
|
//[with_flag]~^ ERROR: evaluation of constant value failed
|
2023-08-02 09:14:36 -05:00
|
|
|
//[with_flag]~| invalid value
|
2022-08-07 07:30:03 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const UNALIGNED_PTR: () = unsafe {
|
|
|
|
let _x: &u32 = transmute(&[0u8; 4]);
|
2022-08-09 08:40:44 -05:00
|
|
|
//[with_flag]~^ ERROR: evaluation of constant value failed
|
|
|
|
//[with_flag]~| invalid value
|
2022-08-07 07:30:03 -05:00
|
|
|
};
|
|
|
|
|
2023-07-24 04:44:58 -05:00
|
|
|
const UNINHABITED_VARIANT: () = unsafe {
|
|
|
|
let data = [1u8];
|
|
|
|
// Not using transmute, we want to hit the ImmTy code path.
|
|
|
|
let v = *addr_of!(data).cast::<UninhDiscriminant>();
|
|
|
|
//[with_flag]~^ ERROR: evaluation of constant value failed
|
2023-08-02 09:14:36 -05:00
|
|
|
//[with_flag]~| invalid value
|
|
|
|
};
|
|
|
|
|
|
|
|
const PARTIAL_POINTER: () = unsafe {
|
|
|
|
#[repr(C, packed)]
|
|
|
|
struct Packed {
|
|
|
|
pad1: u8,
|
|
|
|
ptr: *const u8,
|
|
|
|
pad2: [u8; 7],
|
|
|
|
}
|
|
|
|
// `Align` ensures that the entire thing has pointer alignment again.
|
|
|
|
#[repr(C)]
|
|
|
|
struct Align {
|
|
|
|
p: Packed,
|
|
|
|
align: usize,
|
|
|
|
}
|
|
|
|
let mem = Packed { pad1: 0, ptr: &0u8 as *const u8, pad2: [0; 7] };
|
|
|
|
let mem = Align { p: mem, align: 0 };
|
|
|
|
let _val = *(&mem as *const Align as *const [*const u8; 2]);
|
|
|
|
//[with_flag]~^ ERROR: evaluation of constant value failed
|
|
|
|
//[with_flag]~| invalid value
|
2023-07-24 04:44:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Regression tests for an ICE (related to <https://github.com/rust-lang/rust/issues/113988>).
|
|
|
|
const VALID_ENUM1: E = { let e = E::A; e };
|
|
|
|
const VALID_ENUM2: Result<&'static [u8], ()> = { let e = Err(()); e };
|
|
|
|
|
2023-09-02 09:12:57 -05:00
|
|
|
// Htting the (non-integer) array code in validation with an immediate local.
|
|
|
|
const VALID_ARRAY: [Option<i32>; 0] = { let e = [None; 0]; e };
|
|
|
|
|
2023-10-16 06:52:19 -05:00
|
|
|
// Detecting oversized references.
|
|
|
|
const OVERSIZED_REF: () = { unsafe {
|
|
|
|
let slice: *const [u8] = transmute((1usize, usize::MAX));
|
|
|
|
let _val = &*slice;
|
|
|
|
//[with_flag]~^ ERROR: evaluation of constant value failed
|
|
|
|
//[with_flag]~| slice is bigger than largest supported object
|
|
|
|
} };
|
|
|
|
|
2022-08-07 07:30:03 -05:00
|
|
|
fn main() {}
|