Rollup merge of #81412 - hyd-dev:array-assume-init-wrong-assertion, r=m-ou-se

Fix assertion in `MaybeUninit::array_assume_init()` for zero-length arrays

That assertion has a false positive ([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=63922b8c897b04112adcdf346deb1d0e)):
```rust
#![feature(maybe_uninit_array_assume_init)]

use std::mem::MaybeUninit;

enum Uninhabited {}

fn main() {
    unsafe {
        // thread 'main' panicked at 'attempted to instantiate uninhabited type `Uninhabited`'
        MaybeUninit::<Uninhabited>::array_assume_init([]);
    }
}
```
*Previously reported in https://github.com/rust-lang/rust/pull/80600#discussion_r564496692.*

This PR makes it ignore zero-length arrays.

cc #80908
This commit is contained in:
Yuki Okushi 2021-01-27 04:43:37 +09:00 committed by GitHub
commit b2f6c2aa9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 1 deletions

View file

@ -839,7 +839,7 @@ impl<T> MaybeUninit<T> {
// * MaybeUnint does not drop, so there are no double-frees
// And thus the conversion is safe
unsafe {
intrinsics::assert_inhabited::<T>();
intrinsics::assert_inhabited::<[T; N]>();
(&array as *const _ as *const [T; N]).read()
}
}

View file

@ -152,6 +152,8 @@ fn uninit_array_assume_init() {
let array = unsafe { MaybeUninit::array_assume_init(array) };
assert_eq!(array, [3, 1, 4, 1, 5]);
let [] = unsafe { MaybeUninit::<!>::array_assume_init([]) };
}
#[test]