Skip to content

Commit

Permalink
Add more tests, add docs for is_mem_uninit field
Browse files Browse the repository at this point in the history
  • Loading branch information
5225225 committed Sep 8, 2022
1 parent dc4b6a9 commit 96a2e65
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 30 deletions.
9 changes: 8 additions & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,14 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
#[derive(Debug, Copy, Clone, PartialEq)]
enum InitKind {
Zeroed,
Uninit { is_mem_uninit: bool },
/// `is_mem_uninit` is true *only* if this is a call to `mem::uninitialized()`, not if
/// this is a `MaybeUninit::uninit().assume_init()`.
///
/// This lets us avoid duplicate errors being shown, for code that matches the
/// mem_uninitialized FCW.
Uninit {
is_mem_uninit: bool,
},
}

impl InitKind {
Expand Down
39 changes: 39 additions & 0 deletions src/test/ui/intrinsics/mem-uninitialized-future-compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ struct UninitStruct {
b: char,
}

enum OneVariant {
Hello,
}

enum TwoVariant {
Hello,
Goodbye,
}

enum OneVariantWith<T> {
Hello(T),
}

unsafe fn unknown_type<T, const N: usize>() {
std::mem::uninitialized::<T>();
//~^ ERROR the type `T` is generic, and might not permit being left uninitialized
Expand All @@ -22,6 +35,12 @@ unsafe fn unknown_type<T, const N: usize>() {
std::mem::uninitialized::<[UninitStruct; N]>();
//~^ ERROR the type `[UninitStruct; N]` is generic, and might not permit being left uninitialized

std::mem::uninitialized::<Result<T, !>>();
//~^ ERROR the type `std::result::Result<T, !>` does not permit being left uninitialized

std::mem::uninitialized::<OneVariantWith<T>>();
//~^ ERROR the type `OneVariantWith<T>` is generic, and might not permit being left uninitialized

std::mem::uninitialized::<[T; 0]>();
std::mem::uninitialized::<[char; 0]>();
}
Expand Down Expand Up @@ -58,10 +77,30 @@ fn main() {
std::mem::uninitialized::<(u32, char)>();
//~^ ERROR the type `(u32, char)` does not permit being left uninitialized

std::mem::uninitialized::<TwoVariant>();
//~^ ERROR the type `TwoVariant` does not permit being left uninitialized

std::mem::uninitialized::<Result<!, !>>();
//~^ ERROR the type `std::result::Result<!, !>` does not permit being left uninitialized

std::mem::uninitialized::<Result<!, u32>>();
//~^ ERROR the type `std::result::Result<!, u32>` does not permit being left uninitialized

std::mem::uninitialized::<Option<!>>();
//~^ ERROR the type `std::option::Option<!>` does not permit being left uninitialized

std::mem::uninitialized::<OneVariantWith<char>>();
//~^ ERROR the type `OneVariantWith<char>` does not permit being left uninitialized

std::mem::uninitialized::<OneVariantWith<!>>();
//~^ ERROR the type `OneVariantWith<!>` does not permit being left uninitialized

std::mem::uninitialized::<MaybeUninit<Box<u32>>>();
std::mem::uninitialized::<usize>();
std::mem::uninitialized::<f32>();
std::mem::uninitialized::<*const u8>();
std::mem::uninitialized::<[u8; 64]>();
std::mem::uninitialized::<OneVariant>();
std::mem::uninitialized::<OneVariantWith<u32>>();
}
}
Loading

0 comments on commit 96a2e65

Please sign in to comment.