1
Fork 0

Make invalid-value trigger on uninit primitives

This commit is contained in:
5225225 2022-07-04 23:57:41 +01:00
parent 450e99f937
commit 3e567bcd4f
3 changed files with 32 additions and 10 deletions

View file

@ -2475,6 +2475,15 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
Char if init == InitKind::Uninit => { Char if init == InitKind::Uninit => {
Some(("characters must be a valid Unicode codepoint".to_string(), None)) Some(("characters must be a valid Unicode codepoint".to_string(), None))
} }
Int(_) | Uint(_) if init == InitKind::Uninit => {
Some(("integers must not be uninitialized".to_string(), None))
}
Float(_) if init == InitKind::Uninit => {
Some(("floats must not be uninitialized".to_string(), None))
}
RawPtr(_) if init == InitKind::Uninit => {
Some(("raw pointers must not be uninitialized".to_string(), None))
}
// Recurse and checks for some compound types. // Recurse and checks for some compound types.
Adt(adt_def, substs) if !adt_def.is_union() => { Adt(adt_def, substs) if !adt_def.is_union() => {
// First check if this ADT has a layout attribute (like `NonNull` and friends). // First check if this ADT has a layout attribute (like `NonNull` and friends).

View file

@ -100,6 +100,9 @@ fn main() {
let _val: [bool; 2] = mem::zeroed(); let _val: [bool; 2] = mem::zeroed();
let _val: [bool; 2] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized let _val: [bool; 2] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
let _val: i32 = mem::zeroed();
let _val: i32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
// Transmute-from-0 // Transmute-from-0
let _val: &'static i32 = mem::transmute(0usize); //~ ERROR: does not permit zero-initialization let _val: &'static i32 = mem::transmute(0usize); //~ ERROR: does not permit zero-initialization
let _val: &'static [i32] = mem::transmute((0usize, 0usize)); //~ ERROR: does not permit zero-initialization let _val: &'static [i32] = mem::transmute((0usize, 0usize)); //~ ERROR: does not permit zero-initialization
@ -114,13 +117,12 @@ fn main() {
let _val: Option<&'static i32> = mem::zeroed(); let _val: Option<&'static i32> = mem::zeroed();
let _val: Option<fn()> = mem::zeroed(); let _val: Option<fn()> = mem::zeroed();
let _val: MaybeUninit<&'static i32> = mem::zeroed(); let _val: MaybeUninit<&'static i32> = mem::zeroed();
let _val: i32 = mem::zeroed();
let _val: bool = MaybeUninit::zeroed().assume_init(); let _val: bool = MaybeUninit::zeroed().assume_init();
let _val: [bool; 0] = MaybeUninit::uninit().assume_init(); let _val: [bool; 0] = MaybeUninit::uninit().assume_init();
let _val: [!; 0] = MaybeUninit::zeroed().assume_init(); let _val: [!; 0] = MaybeUninit::zeroed().assume_init();
// Some things that happen to work due to rustc implementation details, // Some things that happen to work due to rustc implementation details,
// but are not guaranteed to keep working. // but are not guaranteed to keep working.
let _val: i32 = mem::uninitialized();
let _val: OneFruit = mem::uninitialized(); let _val: OneFruit = mem::uninitialized();
} }
} }

View file

@ -97,7 +97,7 @@ LL | let _val: (i32, !) = mem::uninitialized();
| this code causes undefined behavior when executed | this code causes undefined behavior when executed
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done | help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
| |
= note: the `!` type has no valid value = note: integers must not be uninitialized
error: the type `Void` does not permit zero-initialization error: the type `Void` does not permit zero-initialization
--> $DIR/uninitialized-zeroed.rs:57:26 --> $DIR/uninitialized-zeroed.rs:57:26
@ -414,8 +414,19 @@ LL | let _val: [bool; 2] = mem::uninitialized();
| |
= note: booleans must be either `true` or `false` = note: booleans must be either `true` or `false`
error: the type `i32` does not permit being left uninitialized
--> $DIR/uninitialized-zeroed.rs:104:25
|
LL | let _val: i32 = mem::uninitialized();
| ^^^^^^^^^^^^^^^^^^^^
| |
| this code causes undefined behavior when executed
| help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
|
= note: integers must not be uninitialized
error: the type `&i32` does not permit zero-initialization error: the type `&i32` does not permit zero-initialization
--> $DIR/uninitialized-zeroed.rs:104:34 --> $DIR/uninitialized-zeroed.rs:107:34
| |
LL | let _val: &'static i32 = mem::transmute(0usize); LL | let _val: &'static i32 = mem::transmute(0usize);
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
@ -426,7 +437,7 @@ LL | let _val: &'static i32 = mem::transmute(0usize);
= note: references must be non-null = note: references must be non-null
error: the type `&[i32]` does not permit zero-initialization error: the type `&[i32]` does not permit zero-initialization
--> $DIR/uninitialized-zeroed.rs:105:36 --> $DIR/uninitialized-zeroed.rs:108:36
| |
LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize)); LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -437,7 +448,7 @@ LL | let _val: &'static [i32] = mem::transmute((0usize, 0usize));
= note: references must be non-null = note: references must be non-null
error: the type `NonZeroU32` does not permit zero-initialization error: the type `NonZeroU32` does not permit zero-initialization
--> $DIR/uninitialized-zeroed.rs:106:32 --> $DIR/uninitialized-zeroed.rs:109:32
| |
LL | let _val: NonZeroU32 = mem::transmute(0); LL | let _val: NonZeroU32 = mem::transmute(0);
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
@ -448,7 +459,7 @@ LL | let _val: NonZeroU32 = mem::transmute(0);
= note: `std::num::NonZeroU32` must be non-null = note: `std::num::NonZeroU32` must be non-null
error: the type `NonNull<i32>` does not permit zero-initialization error: the type `NonNull<i32>` does not permit zero-initialization
--> $DIR/uninitialized-zeroed.rs:109:34 --> $DIR/uninitialized-zeroed.rs:112:34
| |
LL | let _val: NonNull<i32> = MaybeUninit::zeroed().assume_init(); LL | let _val: NonNull<i32> = MaybeUninit::zeroed().assume_init();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -459,7 +470,7 @@ LL | let _val: NonNull<i32> = MaybeUninit::zeroed().assume_init();
= note: `std::ptr::NonNull<i32>` must be non-null = note: `std::ptr::NonNull<i32>` must be non-null
error: the type `NonNull<i32>` does not permit being left uninitialized error: the type `NonNull<i32>` does not permit being left uninitialized
--> $DIR/uninitialized-zeroed.rs:110:34 --> $DIR/uninitialized-zeroed.rs:113:34
| |
LL | let _val: NonNull<i32> = MaybeUninit::uninit().assume_init(); LL | let _val: NonNull<i32> = MaybeUninit::uninit().assume_init();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -470,7 +481,7 @@ LL | let _val: NonNull<i32> = MaybeUninit::uninit().assume_init();
= note: `std::ptr::NonNull<i32>` must be non-null = note: `std::ptr::NonNull<i32>` must be non-null
error: the type `bool` does not permit being left uninitialized error: the type `bool` does not permit being left uninitialized
--> $DIR/uninitialized-zeroed.rs:111:26 --> $DIR/uninitialized-zeroed.rs:114:26
| |
LL | let _val: bool = MaybeUninit::uninit().assume_init(); LL | let _val: bool = MaybeUninit::uninit().assume_init();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -480,5 +491,5 @@ LL | let _val: bool = MaybeUninit::uninit().assume_init();
| |
= note: booleans must be either `true` or `false` = note: booleans must be either `true` or `false`
error: aborting due to 39 previous errors error: aborting due to 40 previous errors