Skip to content

Commit

Permalink
Rollup merge of rust-lang#95374 - RalfJung:assert_uninit_valid, r=Mar…
Browse files Browse the repository at this point in the history
…k-Simulacrum

assert_uninit_valid: ensure we detect at least arrays of uninhabited types

We can't easily extend this check to *all* arrays (Cc rust-lang#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 f4a7ce9 + e132077 commit bdcefe9
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 bdcefe9

Please sign in to comment.