Skip to content

Commit 3e567bc

Browse files
committed
Make invalid-value trigger on uninit primitives
1 parent 450e99f commit 3e567bc

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,6 +2475,15 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
24752475
Char if init == InitKind::Uninit => {
24762476
Some(("characters must be a valid Unicode codepoint".to_string(), None))
24772477
}
2478+
Int(_) | Uint(_) if init == InitKind::Uninit => {
2479+
Some(("integers must not be uninitialized".to_string(), None))
2480+
}
2481+
Float(_) if init == InitKind::Uninit => {
2482+
Some(("floats must not be uninitialized".to_string(), None))
2483+
}
2484+
RawPtr(_) if init == InitKind::Uninit => {
2485+
Some(("raw pointers must not be uninitialized".to_string(), None))
2486+
}
24782487
// Recurse and checks for some compound types.
24792488
Adt(adt_def, substs) if !adt_def.is_union() => {
24802489
// First check if this ADT has a layout attribute (like `NonNull` and friends).

src/test/ui/lint/uninitialized-zeroed.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ fn main() {
100100
let _val: [bool; 2] = mem::zeroed();
101101
let _val: [bool; 2] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
102102

103+
let _val: i32 = mem::zeroed();
104+
let _val: i32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
105+
103106
// Transmute-from-0
104107
let _val: &'static i32 = mem::transmute(0usize); //~ ERROR: does not permit zero-initialization
105108
let _val: &'static [i32] = mem::transmute((0usize, 0usize)); //~ ERROR: does not permit zero-initialization
@@ -114,13 +117,12 @@ fn main() {
114117
let _val: Option<&'static i32> = mem::zeroed();
115118
let _val: Option<fn()> = mem::zeroed();
116119
let _val: MaybeUninit<&'static i32> = mem::zeroed();
117-
let _val: i32 = mem::zeroed();
118120
let _val: bool = MaybeUninit::zeroed().assume_init();
119121
let _val: [bool; 0] = MaybeUninit::uninit().assume_init();
120122
let _val: [!; 0] = MaybeUninit::zeroed().assume_init();
123+
121124
// Some things that happen to work due to rustc implementation details,
122125
// but are not guaranteed to keep working.
123-
let _val: i32 = mem::uninitialized();
124126
let _val: OneFruit = mem::uninitialized();
125127
}
126128
}

src/test/ui/lint/uninitialized-zeroed.stderr

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ LL | let _val: (i32, !) = mem::uninitialized();
9797
| this code causes undefined behavior when executed
9898
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
9999
|
100-
= note: the `!` type has no valid value
100+
= note: integers must not be uninitialized
101101

102102
error: the type `Void` does not permit zero-initialization
103103
--> $DIR/uninitialized-zeroed.rs:57:26
@@ -414,8 +414,19 @@ LL | let _val: [bool; 2] = mem::uninitialized();
414414
|
415415
= note: booleans must be either `true` or `false`
416416

417+
error: the type `i32` does not permit being left uninitialized
418+
--> $DIR/uninitialized-zeroed.rs:104:25
419+
|
420+
LL | let _val: i32 = mem::uninitialized();
421+
| ^^^^^^^^^^^^^^^^^^^^
422+
| |
423+
| this code causes undefined behavior when executed
424+
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
425+
|
426+
= note: integers must not be uninitialized
427+
417428
error: the type `&i32` does not permit zero-initialization
418-
--> $DIR/uninitialized-zeroed.rs:104:34
429+
--> $DIR/uninitialized-zeroed.rs:107:34
419430
|
420431
LL | let _val: &'static i32 = mem::transmute(0usize);
421432
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -426,7 +437,7 @@ LL | let _val: &'static i32 = mem::transmute(0usize);
426437
= note: references must be non-null
427438

428439
error: the type `&[i32]` does not permit zero-initialization
429-
--> $DIR/uninitialized-zeroed.rs:105:36
440+
--> $DIR/uninitialized-zeroed.rs:108:36
430441
|
431442
LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize));
432443
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -437,7 +448,7 @@ LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize));
437448
= note: references must be non-null
438449

439450
error: the type `NonZeroU32` does not permit zero-initialization
440-
--> $DIR/uninitialized-zeroed.rs:106:32
451+
--> $DIR/uninitialized-zeroed.rs:109:32
441452
|
442453
LL | let _val: NonZeroU32 = mem::transmute(0);
443454
| ^^^^^^^^^^^^^^^^^
@@ -448,7 +459,7 @@ LL | let _val: NonZeroU32 = mem::transmute(0);
448459
= note: `std::num::NonZeroU32` must be non-null
449460

450461
error: the type `NonNull<i32>` does not permit zero-initialization
451-
--> $DIR/uninitialized-zeroed.rs:109:34
462+
--> $DIR/uninitialized-zeroed.rs:112:34
452463
|
453464
LL | let _val: NonNull<i32> = MaybeUninit::zeroed().assume_init();
454465
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -459,7 +470,7 @@ LL | let _val: NonNull<i32> = MaybeUninit::zeroed().assume_init();
459470
= note: `std::ptr::NonNull<i32>` must be non-null
460471

461472
error: the type `NonNull<i32>` does not permit being left uninitialized
462-
--> $DIR/uninitialized-zeroed.rs:110:34
473+
--> $DIR/uninitialized-zeroed.rs:113:34
463474
|
464475
LL | let _val: NonNull<i32> = MaybeUninit::uninit().assume_init();
465476
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -470,7 +481,7 @@ LL | let _val: NonNull<i32> = MaybeUninit::uninit().assume_init();
470481
= note: `std::ptr::NonNull<i32>` must be non-null
471482

472483
error: the type `bool` does not permit being left uninitialized
473-
--> $DIR/uninitialized-zeroed.rs:111:26
484+
--> $DIR/uninitialized-zeroed.rs:114:26
474485
|
475486
LL | let _val: bool = MaybeUninit::uninit().assume_init();
476487
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -480,5 +491,5 @@ LL | let _val: bool = MaybeUninit::uninit().assume_init();
480491
|
481492
= note: booleans must be either `true` or `false`
482493

483-
error: aborting due to 39 previous errors
494+
error: aborting due to 40 previous errors
484495

0 commit comments

Comments
 (0)