Skip to content

Commit 67c0f4e

Browse files
authored
Rollup merge of rust-lang#67507 - Mark-Simulacrum:purge-uninit, r=Centril
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". Fixes rust-lang#62397
2 parents 1de2705 + c205f6a commit 67c0f4e

15 files changed

+81
-48
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

+8-7
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

+12
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

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

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

+4-5
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

+4-2
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)),

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ 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+
// Furthermore, there are no types within MyEnum which cannot be initialized with zero,
74+
// specifically, though padding and such are present, there are no references or similar
75+
// types.
76+
let mut dest: MyEnum = mem::zeroed();
7477
while buf.len() > 0 {
7578
match parse_my_enum(&mut dest, &mut buf) {
7679
Ok(()) => output.push(Ok(dest)),

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ 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+
//
69+
// MyEnum is repr(u8) so it is guaranteed to have a separate discriminant and each variant
70+
// can be zero initialized.
71+
let mut dest: MyEnum = mem::zeroed();
7072
while buf.len() > 0 {
7173
match parse_my_enum(&mut dest, &mut buf) {
7274
Ok(()) => output.push(Ok(dest)),

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#![allow(deprecated)]
2-
1+
use std::mem::zeroed;
32
enum Void {}
43

54
fn main() {
@@ -8,21 +7,25 @@ fn main() {
87
Ok(n) => n,
98
};
109

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

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

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

20-
let x: &[Void] = unsafe { std::mem::uninitialized() };
23+
let x: &[Void] = unsafe { zeroed() };
2124
let _ = match x { //~ ERROR non-exhaustive
2225
&[] => (),
2326
};
2427

25-
let x: Void = unsafe { std::mem::uninitialized() };
28+
let x: Void = unsafe { zeroed() };
2629
let _ = match x {}; // okay
2730

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

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0004]: non-exhaustive patterns: `Err(_)` not covered
2-
--> $DIR/uninhabited-matches-feature-gated.rs:7:19
2+
--> $DIR/uninhabited-matches-feature-gated.rs:6:19
33
|
44
LL | let _ = match x {
55
| ^ pattern `Err(_)` not covered
66
|
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

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
// run-pass
1+
// build-pass
22
// Test the uninit() construct returning various empty types.
33

44
// pretty-expanded FIXME #23616
55

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

8-
#[derive(Clone)]
98
struct Foo;
109

1110
#[allow(deprecated)]
1211
pub fn main() {
1312
unsafe {
14-
let _x: Foo = mem::uninitialized();
15-
let _x: [Foo; 2] = mem::uninitialized();
13+
// `Foo` and `[Foo; 2]` are both zero sized and inhabited, so this is safe.
14+
let _x: Foo = MaybeUninit::uninit().assume_init();
15+
let _x: [Foo; 2] = MaybeUninit::uninit().assume_init();
16+
let _x: Foo = std::mem::uninitialized();
17+
let _x: [Foo; 2] = std::mem::uninitialized();
1618
}
1719
}

0 commit comments

Comments
 (0)