From 8cecac260298a9b096e347a0489c64aafcd07ca1 Mon Sep 17 00:00:00 2001 From: Katherine Philip Date: Mon, 30 Aug 2021 13:19:50 -0700 Subject: [PATCH 1/2] Add test case for using `slice::fill` with MaybeUninit --- library/core/tests/slice.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs index 43e2af3eb18..a6c7794c150 100644 --- a/library/core/tests/slice.rs +++ b/library/core/tests/slice.rs @@ -2144,3 +2144,12 @@ fn test_slice_run_destructors() { assert_eq!(x.get(), 1); } + +#[test] +fn test_slice_fill_with_uninit() { + // This should not UB. See #87891 + use core::mem::MaybeUninit; + + let mut a = [MaybeUninit::::uninit(); 10]; + a.fill(MaybeUninit::uninit()); +} From 5390ea46440d051085c0cf06f64cb2720bb41678 Mon Sep 17 00:00:00 2001 From: Katherine Philip Date: Tue, 31 Aug 2021 08:28:51 -0700 Subject: [PATCH 2/2] Move to the top of file --- library/core/tests/slice.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs index a6c7794c150..c591dd3e1a6 100644 --- a/library/core/tests/slice.rs +++ b/library/core/tests/slice.rs @@ -1,5 +1,6 @@ use core::cell::Cell; use core::cmp::Ordering; +use core::mem::MaybeUninit; use core::result::Result::{Err, Ok}; #[test] @@ -2148,8 +2149,6 @@ fn test_slice_run_destructors() { #[test] fn test_slice_fill_with_uninit() { // This should not UB. See #87891 - use core::mem::MaybeUninit; - let mut a = [MaybeUninit::::uninit(); 10]; a.fill(MaybeUninit::uninit()); }