Skip to content

Commit c205f6a

Browse files
Remove mem::uninitalized from tests
This purges uses of uninitialized where possible from test cases. Some are merely moved over to the equally bad pattern of MaybeUninit::uninit().assume_init() but with an annotation that this is "the best we can do".
1 parent 3982d35 commit c205f6a

File tree

15 files changed

+81
-48
lines changed

15 files changed

+81
-48
lines changed

src/test/run-make-fulldeps/sanitizer-memory/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
all:
88
$(RUSTC) -g -Z sanitizer=memory -Z print-link-args uninit.rs | $(CGREP) librustc_msan
99
$(TMPDIR)/uninit 2>&1 | $(CGREP) use-of-uninitialized-value
10+
$(RUSTC) -g -Z sanitizer=memory -Z print-link-args maybeuninit.rs | $(CGREP) librustc_msan
11+
$(TMPDIR)/maybeuninit 2>&1 | $(CGREP) use-of-uninitialized-value
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::mem::MaybeUninit;
2+
3+
fn main() {
4+
// This is technically not sound -- but we're literally trying to test
5+
// that the sanitizer catches this, so I guess "intentionally unsound"?
6+
let xs: [u8; 4] = unsafe { MaybeUninit::uninit().assume_init() };
7+
let y = xs[0] + xs[1];
8+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::mem;
2-
31
fn main() {
2+
// This is technically not sound -- but we're literally trying to test
3+
// that the sanitizer catches this, so I guess "intentionally unsound"?
44
#[allow(deprecated)]
5-
let xs: [u8; 4] = unsafe { mem::uninitialized() };
5+
let xs: [u8; 4] = unsafe { std::mem::uninitialized() };
66
let y = xs[0] + xs[1];
77
}

src/test/rustdoc/issue-52873.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ impl<U: Unsigned, B: Bit> Add<B0> for UInt<U, B> {
105105
impl<U: Unsigned> Add<U> for UTerm {
106106
type Output = U;
107107
fn add(self, _: U) -> Self::Output {
108-
#[allow(deprecated)]
109-
unsafe { ::std::mem::uninitialized() }
108+
unimplemented!()
110109
}
111110
}
112111

@@ -137,7 +136,7 @@ where
137136
{
138137
type Output = UInt<Prod<Ul, UInt<Ur, B>>, B0>;
139138
fn mul(self, _: UInt<Ur, B>) -> Self::Output {
140-
unsafe { ::std::mem::uninitialized() }
139+
unimplemented!()
141140
}
142141
}
143142

src/test/ui/abi/stack-probes.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// ignore-sgx no processes
1414
// ignore-musl FIXME #31506
1515

16-
use std::mem;
16+
use std::mem::MaybeUninit;
1717
use std::process::Command;
1818
use std::thread;
1919
use std::env;
@@ -28,8 +28,8 @@ fn main() {
2828
let args = env::args().skip(1).collect::<Vec<_>>();
2929
if args.len() > 0 {
3030
match &args[0][..] {
31-
"main-thread" => recurse(&[]),
32-
"child-thread" => thread::spawn(|| recurse(&[])).join().unwrap(),
31+
"main-thread" => recurse(&MaybeUninit::uninit()),
32+
"child-thread" => thread::spawn(|| recurse(&MaybeUninit::uninit())).join().unwrap(),
3333
_ => panic!(),
3434
}
3535
return
@@ -48,10 +48,11 @@ fn main() {
4848
}
4949

5050
#[allow(unconditional_recursion)]
51-
fn recurse(array: &[u64]) {
52-
unsafe { black_box(array.as_ptr() as u64); }
53-
#[allow(deprecated)]
54-
let local: [_; 1024] = unsafe { mem::uninitialized() };
51+
fn recurse(array: &MaybeUninit<[u64; 1024]>) {
52+
unsafe {
53+
black_box(array.as_ptr() as u64);
54+
}
55+
let local: MaybeUninit<[u64; 1024]> = MaybeUninit::uninit();
5556
recurse(&local);
5657
}
5758

src/test/ui/const-generics/issues/issue-61422.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
use std::mem;
77

8+
// Neither of the uninits below are currently accepted as not UB, however,
9+
// this code does not run and is merely checking that we do not ICE on this pattern,
10+
// so this is fine.
11+
812
fn foo<const SIZE: usize>() {
913
let arr: [u8; SIZE] = unsafe {
1014
#[allow(deprecated)]
@@ -13,4 +17,12 @@ fn foo<const SIZE: usize>() {
1317
};
1418
}
1519

20+
fn bar<const SIZE: usize>() {
21+
let arr: [u8; SIZE] = unsafe {
22+
let array: [u8; SIZE] = mem::MaybeUninit::uninit().assume_init();
23+
array
24+
};
25+
}
26+
27+
1628
fn main() {}

src/test/ui/for-loop-while/for-loop-has-unit-body.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
fn main() {
33
// Check that the tail statement in the body unifies with something
44
for _ in 0..3 {
5-
#[allow(deprecated)]
6-
unsafe { std::mem::uninitialized() }
5+
// `()` is fine to zero-initialize as it is zero sized and inhabited.
6+
unsafe { std::mem::zeroed() }
77
}
88

99
// Check that the tail statement in the body can be unit

src/test/ui/issues/issue-48131.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This note is annotated because the purpose of the test
22
// is to ensure that certain other notes are not generated.
33
#![deny(unused_unsafe)] //~ NOTE
4-
#![allow(deprecated)]
4+
55

66
// (test that no note is generated on this unsafe fn)
77
pub unsafe fn a() {
@@ -20,8 +20,8 @@ pub fn b() {
2020
unsafe { /* unnecessary */ } //~ ERROR unnecessary `unsafe`
2121
//~^ NOTE
2222
}
23-
24-
let () = ::std::mem::uninitialized();
23+
// `()` is fine to zero-initialize as it is zero sized and inhabited.
24+
let () = ::std::mem::zeroed();
2525

2626
inner()
2727
}

src/test/ui/issues/issue-58212.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// run-pass
1+
// check-pass
22

33
trait FromUnchecked {
4-
unsafe fn from_unchecked();
4+
fn from_unchecked();
55
}
66

77
impl FromUnchecked for [u8; 1] {
8-
unsafe fn from_unchecked() {
9-
#[allow(deprecated)]
10-
let mut array: Self = std::mem::uninitialized();
8+
fn from_unchecked() {
9+
let mut array: Self = [0; 1];
1110
let _ptr = &mut array as *mut [u8] as *mut u8;
1211
}
1312
}

src/test/ui/structs-enums/enum-non-c-like-repr-c-and-int.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ fn main() {
6969
unsafe {
7070
// This should be safe, because we don't match on it unless it's fully formed,
7171
// and it doesn't have a destructor.
72-
#[allow(deprecated)]
73-
let mut dest: MyEnum = mem::uninitialized();
72+
//
73+
// MyEnum is repr(C, u8) so it is guaranteed to have a separate discriminant and each
74+
// variant can be zero initialized.
75+
let mut dest: MyEnum = mem::zeroed();
7476
while buf.len() > 0 {
7577
match parse_my_enum(&mut dest, &mut buf) {
7678
Ok(()) => output.push(Ok(dest)),

0 commit comments

Comments
 (0)