diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2021-01-27 04:43:37 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-27 04:43:37 +0900 |
| commit | b2f6c2aa9b0f87770b3160500cca2ae370aa618d (patch) | |
| tree | af5df905c660b9ceda7a7b7b5c5b1d7eded76b48 | |
| parent | d68570c78f698bd913bfc8dae33ebe81590457b7 (diff) | |
| parent | f52066726dd24ae07b1991b53f48ffa25c2b716d (diff) | |
| download | rust-b2f6c2aa9b0f87770b3160500cca2ae370aa618d.tar.gz rust-b2f6c2aa9b0f87770b3160500cca2ae370aa618d.zip | |
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
| -rw-r--r-- | library/core/src/mem/maybe_uninit.rs | 2 | ||||
| -rw-r--r-- | library/core/tests/mem.rs | 2 |
2 files changed, 3 insertions, 1 deletions
diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index fda0553f94c..05bcd90d3ca 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -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() } } diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs index 2279a16429f..38084f401bc 100644 --- a/library/core/tests/mem.rs +++ b/library/core/tests/mem.rs @@ -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] |
