Skip to content

Commit cb599cc

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 cb599cc

15 files changed

+49
-39
lines changed

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

+2
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
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+
}
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

+2-3
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

+2-3
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+
1717
use std::process::Command;
1818
use std::thread;
1919
use std::env;
@@ -50,8 +50,7 @@ fn main() {
5050
#[allow(unconditional_recursion)]
5151
fn recurse(array: &[u64]) {
5252
unsafe { black_box(array.as_ptr() as u64); }
53-
#[allow(deprecated)]
54-
let local: [_; 1024] = unsafe { mem::uninitialized() };
53+
let local: [u64; 1024] = [0; 1024];
5554
recurse(&local);
5655
}
5756

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::mem;
77

88
fn foo<const SIZE: usize>() {
99
let arr: [u8; SIZE] = unsafe {
10-
#[allow(deprecated)]
11-
let array: [u8; SIZE] = mem::uninitialized();
10+
11+
let array: [u8; SIZE] = mem::zeroed();
1212
array
1313
};
1414
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
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+
unsafe { std::mem::zeroed() }
76
}
87

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

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

+2-2
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() {
@@ -21,7 +21,7 @@ pub fn b() {
2121
//~^ NOTE
2222
}
2323

24-
let () = ::std::mem::uninitialized();
24+
let () = ::std::mem::zeroed();
2525

2626
inner()
2727
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// run-pass
1+
// check-pass
22

33
trait FromUnchecked {
44
unsafe fn from_unchecked();
55
}
66

77
impl FromUnchecked for [u8; 1] {
88
unsafe fn from_unchecked() {
9-
#[allow(deprecated)]
10-
let mut array: Self = std::mem::uninitialized();
9+
let mut array: Self = std::mem::zeroed();
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

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ 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+
let mut dest: MyEnum = mem::zeroed();
7473
while buf.len() > 0 {
7574
match parse_my_enum(&mut dest, &mut buf) {
7675
Ok(()) => output.push(Ok(dest)),

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ 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+
let mut dest: MyEnum = mem::zeroed();
7473
while buf.len() > 0 {
7574
match parse_my_enum(&mut dest, &mut buf) {
7675
Ok(()) => output.push(Ok(dest)),

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ fn main() {
6565
unsafe {
6666
// This should be safe, because we don't match on it unless it's fully formed,
6767
// and it doesn't have a destructor.
68-
#[allow(deprecated)]
69-
let mut dest: MyEnum = mem::uninitialized();
68+
let mut dest: MyEnum = mem::zeroed();
7069
while buf.len() > 0 {
7170
match parse_my_enum(&mut dest, &mut buf) {
7271
Ok(()) => output.push(Ok(dest)),

src/test/ui/uninhabited/uninhabited-matches-feature-gated.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(deprecated)]
1+
use std::mem::zeroed;
22

33
enum Void {}
44

@@ -8,21 +8,25 @@ fn main() {
88
Ok(n) => n,
99
};
1010

11-
let x: &Void = unsafe { std::mem::uninitialized() };
11+
// This is pretty much instant UB. However, we have no choice -- we need to
12+
// test matching on a reference to `&Void`; we cannot do anything other than
13+
// just accept the fact that this is UB if `main` did run, but it doesn't;
14+
// this test only checks that these are feature-gated.
15+
let x: &Void = unsafe { zeroed() };
1216
let _ = match x {}; //~ ERROR non-exhaustive
1317

14-
let x: (Void,) = unsafe { std::mem::uninitialized() };
18+
let x: (Void,) = unsafe { zeroed() };
1519
let _ = match x {}; //~ ERROR non-exhaustive
1620

17-
let x: [Void; 1] = unsafe { std::mem::uninitialized() };
21+
let x: [Void; 1] = unsafe { zeroed() };
1822
let _ = match x {}; //~ ERROR non-exhaustive
1923

20-
let x: &[Void] = unsafe { std::mem::uninitialized() };
24+
let x: &[Void] = unsafe { zeroed() };
2125
let _ = match x { //~ ERROR non-exhaustive
2226
&[] => (),
2327
};
2428

25-
let x: Void = unsafe { std::mem::uninitialized() };
29+
let x: Void = unsafe { zeroed() };
2630
let _ = match x {}; // okay
2731

2832
let x: Result<u32, Void> = Ok(23);

src/test/ui/uninhabited/uninhabited-matches-feature-gated.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let _ = match x {
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

99
error[E0004]: non-exhaustive patterns: type `&Void` is non-empty
10-
--> $DIR/uninhabited-matches-feature-gated.rs:12:19
10+
--> $DIR/uninhabited-matches-feature-gated.rs:15:19
1111
|
1212
LL | enum Void {}
1313
| ------------ `Void` defined here
@@ -18,39 +18,39 @@ LL | let _ = match x {};
1818
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1919

2020
error[E0004]: non-exhaustive patterns: type `(Void,)` is non-empty
21-
--> $DIR/uninhabited-matches-feature-gated.rs:15:19
21+
--> $DIR/uninhabited-matches-feature-gated.rs:18:19
2222
|
2323
LL | let _ = match x {};
2424
| ^
2525
|
2626
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2727

2828
error[E0004]: non-exhaustive patterns: type `[Void; 1]` is non-empty
29-
--> $DIR/uninhabited-matches-feature-gated.rs:18:19
29+
--> $DIR/uninhabited-matches-feature-gated.rs:21:19
3030
|
3131
LL | let _ = match x {};
3232
| ^
3333
|
3434
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3535

3636
error[E0004]: non-exhaustive patterns: `&[_, ..]` not covered
37-
--> $DIR/uninhabited-matches-feature-gated.rs:21:19
37+
--> $DIR/uninhabited-matches-feature-gated.rs:24:19
3838
|
3939
LL | let _ = match x {
4040
| ^ pattern `&[_, ..]` not covered
4141
|
4242
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4343

4444
error[E0004]: non-exhaustive patterns: `Err(_)` not covered
45-
--> $DIR/uninhabited-matches-feature-gated.rs:29:19
45+
--> $DIR/uninhabited-matches-feature-gated.rs:32:19
4646
|
4747
LL | let _ = match x {
4848
| ^ pattern `Err(_)` not covered
4949
|
5050
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
5151

5252
error[E0005]: refutable pattern in local binding: `Err(_)` not covered
53-
--> $DIR/uninhabited-matches-feature-gated.rs:34:9
53+
--> $DIR/uninhabited-matches-feature-gated.rs:37:9
5454
|
5555
LL | let Ok(x) = x;
5656
| ^^^^^ pattern `Err(_)` not covered

src/test/ui/uninit-empty-types.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33

44
// pretty-expanded FIXME #23616
55

6-
use std::mem;
6+
use std::mem::MaybeUninit;
77

88
#[derive(Clone)]
99
struct Foo;
1010

1111
#[allow(deprecated)]
1212
pub fn main() {
1313
unsafe {
14-
let _x: Foo = mem::uninitialized();
15-
let _x: [Foo; 2] = mem::uninitialized();
14+
// `Foo` and `[Foo; 2]` are both zero sized so this is safe.
15+
let _x: Foo = MaybeUninit::uninit().assume_init();
16+
let _x: [Foo; 2] = MaybeUninit::uninit().assume_init();
17+
let _x: Foo = std::mem::uninitialized();
18+
let _x: [Foo; 2] = std::mem::uninitialized();
1619
}
1720
}

0 commit comments

Comments
 (0)