2 // ignore-wasm32-bare compiled with panic=abort by default
4 // This test checks panic emitted from `mem::{uninitialized,zeroed}`.
6 #![feature(never_type, arbitrary_enum_discriminant)]
7 #![allow(deprecated, invalid_value)]
10 mem
::{self, MaybeUninit, ManuallyDrop}
,
25 enum OneVariant { Variant(i32) }
27 #[allow(dead_code, non_camel_case_types)]
28 enum OneVariant_NonZero
{
29 Variant(i32, i32, num
::NonZeroI32
),
33 // An `Aggregate` abi enum where 0 is not a valid discriminant.
37 Variant1(i32, i32) = 1,
38 Variant2(i32, i32) = 2,
41 // An enum with ScalarPair layout
47 #[allow(dead_code, non_camel_case_types)]
49 Left(num
::NonZeroI64
),
50 Right(num
::NonZeroI64
),
53 fn test_panic_msg
<T
>(op
: impl (FnOnce() -> T
) + panic
::UnwindSafe
, msg
: &str) {
54 let err
= panic
::catch_unwind(op
).err();
56 err
.as_ref().and_then(|a
| a
.downcast_ref
::<String
>()).map(|s
| &**s
),
65 || mem
::uninitialized
::<!>(),
66 "attempted to instantiate uninhabited type `!`"
69 || mem
::zeroed
::<!>(),
70 "attempted to instantiate uninhabited type `!`"
73 || MaybeUninit
::<!>::uninit().assume_init(),
74 "attempted to instantiate uninhabited type `!`"
78 || mem
::uninitialized
::<Foo
>(),
79 "attempted to instantiate uninhabited type `Foo`"
82 || mem
::zeroed
::<Foo
>(),
83 "attempted to instantiate uninhabited type `Foo`"
86 || MaybeUninit
::<Foo
>::uninit().assume_init(),
87 "attempted to instantiate uninhabited type `Foo`"
91 || mem
::uninitialized
::<Bar
>(),
92 "attempted to instantiate uninhabited type `Bar`"
95 || mem
::zeroed
::<Bar
>(),
96 "attempted to instantiate uninhabited type `Bar`"
99 || MaybeUninit
::<Bar
>::uninit().assume_init(),
100 "attempted to instantiate uninhabited type `Bar`"
103 // Types that do not like zero-initialziation
105 || mem
::uninitialized
::<fn()>(),
106 "attempted to leave type `fn()` uninitialized, which is invalid"
109 || mem
::zeroed
::<fn()>(),
110 "attempted to zero-initialize type `fn()`, which is invalid"
114 || mem
::uninitialized
::<*const dyn Send
>(),
115 "attempted to leave type `*const dyn std::marker::Send` uninitialized, which is invalid"
118 || mem
::zeroed
::<*const dyn Send
>(),
119 "attempted to zero-initialize type `*const dyn std::marker::Send`, which is invalid"
122 /* FIXME(#66151) we conservatively do not error here yet.
124 || mem::uninitialized::<LR_NonZero>(),
125 "attempted to leave type `LR_NonZero` uninitialized, which is invalid"
128 || mem::zeroed::<LR_NonZero>(),
129 "attempted to zero-initialize type `LR_NonZero`, which is invalid"
133 || mem::uninitialized::<ManuallyDrop<LR_NonZero>>(),
134 "attempted to leave type `std::mem::ManuallyDrop<LR_NonZero>` uninitialized, \
138 || mem::zeroed::<ManuallyDrop<LR_NonZero>>(),
139 "attempted to zero-initialize type `std::mem::ManuallyDrop<LR_NonZero>`, \
145 || mem
::uninitialized
::<(NonNull
<u32>, u32, u32)>(),
146 "attempted to leave type `(std::ptr::NonNull<u32>, u32, u32)` uninitialized, \
150 || mem
::zeroed
::<(NonNull
<u32>, u32, u32)>(),
151 "attempted to zero-initialize type `(std::ptr::NonNull<u32>, u32, u32)`, \
156 || mem
::uninitialized
::<OneVariant_NonZero
>(),
157 "attempted to leave type `OneVariant_NonZero` uninitialized, \
161 || mem
::zeroed
::<OneVariant_NonZero
>(),
162 "attempted to zero-initialize type `OneVariant_NonZero`, \
167 || mem
::uninitialized
::<NoNullVariant
>(),
168 "attempted to leave type `NoNullVariant` uninitialized, \
172 || mem
::zeroed
::<NoNullVariant
>(),
173 "attempted to zero-initialize type `NoNullVariant`, \
177 // Types that can be zero, but not uninit.
179 || mem
::uninitialized
::<bool
>(),
180 "attempted to leave type `bool` uninitialized, which is invalid"
183 || mem
::uninitialized
::<LR
>(),
184 "attempted to leave type `LR` uninitialized, which is invalid"
187 || mem
::uninitialized
::<ManuallyDrop
<LR
>>(),
188 "attempted to leave type `std::mem::ManuallyDrop<LR>` uninitialized, which is invalid"
191 // Some things that should work.
192 let _val
= mem
::zeroed
::<bool
>();
193 let _val
= mem
::zeroed
::<LR
>();
194 let _val
= mem
::zeroed
::<ManuallyDrop
<LR
>>();
195 let _val
= mem
::zeroed
::<OneVariant
>();
196 let _val
= mem
::zeroed
::<Option
<&'
static i32>>();
197 let _val
= mem
::zeroed
::<MaybeUninit
<NonNull
<u32>>>();
198 let _val
= mem
::uninitialized
::<MaybeUninit
<bool
>>();
200 // These are UB because they have not been officially blessed, but we await the resolution
201 // of <https://github.com/rust-lang/unsafe-code-guidelines/issues/71> before doing
202 // anything about that.
203 let _val
= mem
::uninitialized
::<i32>();
204 let _val
= mem
::uninitialized
::<*const ()>();