Skip to content

Commit

Permalink
Rollup merge of #95374 - RalfJung:assert_uninit_valid, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
assert_uninit_valid: ensure we detect at least arrays of uninhabited types

We can't easily extend this check to *all* arrays (Cc #87041), but it turns out the existing check already catches arrays of uninhabited types. So let's make sure it stays that way by adding them to the test.
  • Loading branch information
Dylan-DPC authored Apr 9, 2022
2 parents ee8cea8 + e132077 commit 9010879
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/test/ui/intrinsics/panic-uninitialized-zeroed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ fn main() {
"attempted to instantiate uninhabited type `Bar`"
);

test_panic_msg(
|| mem::uninitialized::<[Foo; 2]>(),
"attempted to instantiate uninhabited type `[Foo; 2]`"
);
test_panic_msg(
|| mem::zeroed::<[Foo; 2]>(),
"attempted to instantiate uninhabited type `[Foo; 2]`"
);
test_panic_msg(
|| MaybeUninit::<[Foo; 2]>::uninit().assume_init(),
"attempted to instantiate uninhabited type `[Foo; 2]`"
);

test_panic_msg(
|| mem::uninitialized::<[Bar; 2]>(),
"attempted to instantiate uninhabited type `[Bar; 2]`"
);
test_panic_msg(
|| mem::zeroed::<[Bar; 2]>(),
"attempted to instantiate uninhabited type `[Bar; 2]`"
);
test_panic_msg(
|| MaybeUninit::<[Bar; 2]>::uninit().assume_init(),
"attempted to instantiate uninhabited type `[Bar; 2]`"
);

// Types that do not like zero-initialziation
test_panic_msg(
|| mem::uninitialized::<fn()>(),
Expand Down Expand Up @@ -199,7 +225,9 @@ fn main() {
let _val = mem::zeroed::<OneVariant>();
let _val = mem::zeroed::<Option<&'static i32>>();
let _val = mem::zeroed::<MaybeUninit<NonNull<u32>>>();
let _val = mem::zeroed::<[!; 0]>();
let _val = mem::uninitialized::<MaybeUninit<bool>>();
let _val = mem::uninitialized::<[!; 0]>();

// These are UB because they have not been officially blessed, but we await the resolution
// of <https://github.com/rust-lang/unsafe-code-guidelines/issues/71> before doing
Expand Down

0 comments on commit 9010879

Please sign in to comment.