-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #66059 - RalfJung:panic-on-non-zero, r=<try>
mem::zeroed/uninit: panic on types that do not permit zero-initialization r? @eddyb @oli-obk Cc #62825
- Loading branch information
Showing
10 changed files
with
237 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// run-pass | ||
// ignore-wasm32-bare compiled with panic=abort by default | ||
|
||
// This test checks panic emitted from `mem::{uninitialized,zeroed}`. | ||
|
||
#![feature(never_type)] | ||
#![allow(deprecated, invalid_value)] | ||
|
||
use std::{mem, panic}; | ||
use std::ptr::NonNull; | ||
|
||
#[allow(dead_code)] | ||
struct Foo { | ||
x: u8, | ||
y: !, | ||
} | ||
|
||
enum Bar {} | ||
|
||
#[allow(dead_code)] | ||
enum OneVariant { Variant(i32) } | ||
|
||
fn test_panic_msg<T>(op: impl (FnOnce() -> T) + panic::UnwindSafe, msg: &str) { | ||
let err = panic::catch_unwind(op).err(); | ||
assert_eq!( | ||
err.as_ref().and_then(|a| a.downcast_ref::<String>()).map(|s| &**s), | ||
Some(msg) | ||
); | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
// Uninitialized types | ||
test_panic_msg( | ||
|| mem::uninitialized::<!>(), | ||
"attempted to instantiate uninhabited type `!`" | ||
); | ||
test_panic_msg( | ||
|| mem::zeroed::<!>(), | ||
"attempted to instantiate uninhabited type `!`" | ||
); | ||
test_panic_msg( | ||
|| mem::MaybeUninit::<!>::uninit().assume_init(), | ||
"attempted to instantiate uninhabited type `!`" | ||
); | ||
|
||
test_panic_msg( | ||
|| mem::uninitialized::<Foo>(), | ||
"attempted to instantiate uninhabited type `Foo`" | ||
); | ||
test_panic_msg( | ||
|| mem::zeroed::<Foo>(), | ||
"attempted to instantiate uninhabited type `Foo`" | ||
); | ||
test_panic_msg( | ||
|| mem::MaybeUninit::<Foo>::uninit().assume_init(), | ||
"attempted to instantiate uninhabited type `Foo`" | ||
); | ||
|
||
test_panic_msg( | ||
|| mem::uninitialized::<Bar>(), | ||
"attempted to instantiate uninhabited type `Bar`" | ||
); | ||
test_panic_msg( | ||
|| mem::zeroed::<Bar>(), | ||
"attempted to instantiate uninhabited type `Bar`" | ||
); | ||
test_panic_msg( | ||
|| mem::MaybeUninit::<Bar>::uninit().assume_init(), | ||
"attempted to instantiate uninhabited type `Bar`" | ||
); | ||
|
||
// Types that do not like zero-initialziation | ||
test_panic_msg( | ||
|| mem::uninitialized::<fn()>(), | ||
"attempted to zero-initialize non-zero type `fn()`" | ||
); | ||
test_panic_msg( | ||
|| mem::zeroed::<fn()>(), | ||
"attempted to zero-initialize non-zero type `fn()`" | ||
); | ||
|
||
test_panic_msg( | ||
|| mem::uninitialized::<*const dyn Send>(), | ||
"attempted to zero-initialize non-zero type `*const dyn std::marker::Send`" | ||
); | ||
test_panic_msg( | ||
|| mem::zeroed::<*const dyn Send>(), | ||
"attempted to zero-initialize non-zero type `*const dyn std::marker::Send`" | ||
); | ||
|
||
test_panic_msg( | ||
|| mem::uninitialized::<(NonNull<u32>, u32, u32)>(), | ||
"attempted to zero-initialize non-zero type `(std::ptr::NonNull<u32>, u32, u32)`" | ||
); | ||
test_panic_msg( | ||
|| mem::zeroed::<(NonNull<u32>, u32, u32)>(), | ||
"attempted to zero-initialize non-zero type `(std::ptr::NonNull<u32>, u32, u32)`" | ||
); | ||
|
||
// Some things that should work. | ||
let _val = mem::zeroed::<bool>(); | ||
let _val = mem::zeroed::<OneVariant>(); | ||
let _val = mem::zeroed::<Option<&'static i32>>(); | ||
} | ||
} |
Oops, something went wrong.