//@ run-pass #![feature(const_ptr_write)] #![feature(const_mut_refs)] // Or, equivalently: `MaybeUninit`. pub union BagOfBits { uninit: (), _storage: T, } pub const fn make_1u8_bag() -> BagOfBits { assert!(core::mem::size_of::() >= 1); let mut bag = BagOfBits { uninit: () }; unsafe { (&mut bag as *mut _ as *mut u8).write(1); }; bag } pub fn check_bag(bag: &BagOfBits) { let val = unsafe { (bag as *const _ as *const u8).read() }; assert_eq!(val, 1); } fn main() { check_bag(&make_1u8_bag::<[usize; 1]>()); // Fine check_bag(&make_1u8_bag::()); // Fine const CONST_ARRAY_BAG: BagOfBits<[usize; 1]> = make_1u8_bag(); check_bag(&CONST_ARRAY_BAG); // Fine. const CONST_USIZE_BAG: BagOfBits = make_1u8_bag(); // Used to panic since CTFE would make the entire `BagOfBits` uninit check_bag(&CONST_USIZE_BAG); }