2019-08-31 13:25:28 -05:00
|
|
|
#![feature(stmt_expr_attributes)]
|
2022-08-29 15:28:35 -05:00
|
|
|
#![allow(clippy::let_unit_value, invalid_value)]
|
2019-08-31 13:25:28 -05:00
|
|
|
|
2021-04-22 04:31:13 -05:00
|
|
|
use std::mem::{self, MaybeUninit};
|
2019-08-31 13:25:28 -05:00
|
|
|
|
2023-03-18 17:22:04 -05:00
|
|
|
union MyOwnMaybeUninit {
|
|
|
|
value: u8,
|
|
|
|
uninit: (),
|
|
|
|
}
|
|
|
|
|
2019-08-31 13:25:28 -05:00
|
|
|
fn main() {
|
|
|
|
let _: usize = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
2023-03-18 17:22:04 -05:00
|
|
|
// This is OK, because ZSTs do not contain data.
|
2019-08-31 13:25:28 -05:00
|
|
|
let _: () = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
|
|
|
// This is OK, because `MaybeUninit` allows uninitialized data.
|
|
|
|
let _: MaybeUninit<usize> = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
|
|
|
// This is OK, because all constitutent types are uninit-compatible.
|
|
|
|
let _: (MaybeUninit<usize>, MaybeUninit<bool>) = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
|
|
|
// This is OK, because all constitutent types are uninit-compatible.
|
|
|
|
let _: (MaybeUninit<usize>, [MaybeUninit<bool>; 2]) = unsafe { MaybeUninit::uninit().assume_init() };
|
2021-04-22 04:31:13 -05:00
|
|
|
|
2023-03-18 17:22:04 -05:00
|
|
|
// This is OK, because our own MaybeUninit is just as fine as the one from core.
|
|
|
|
let _: MyOwnMaybeUninit = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
|
|
|
// This is OK, because empty arrays don't contain data.
|
|
|
|
let _: [u8; 0] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
|
|
|
2021-04-22 04:31:13 -05:00
|
|
|
// Was a false negative.
|
|
|
|
let _: usize = unsafe { mem::MaybeUninit::uninit().assume_init() };
|
2023-03-18 17:22:04 -05:00
|
|
|
|
|
|
|
polymorphic::<()>();
|
|
|
|
|
|
|
|
fn polymorphic<T>() {
|
|
|
|
// We are conservative around polymorphic types.
|
|
|
|
let _: T = unsafe { mem::MaybeUninit::uninit().assume_init() };
|
|
|
|
}
|
2019-08-31 13:25:28 -05:00
|
|
|
}
|