Closed
Description
I tried this code:
#[derive(Copy, Clone, Debug)]
enum Fruit {
Apple,
_Banana,
}
fn foo() -> Fruit {
unsafe {
let mut r = mem::uninitialized();
ptr::write(&mut r as *mut Fruit, Fruit::Apple);
r
}
}
I expected to see this happen: It should warn about Fruit
not working with mem::uninitialized
.
Instead, this happened: There is no diagnostic.
The problem here is that not all enums should cause the warning. If the enum only has one variant, being uninitialized could actually be fine (but variants that are uninhabited and ZST do not count). This is easy to check at run-time where we have the full layout (so we panic), but statically we just have the type and that makes it harder.
As a start, we could count the fieldless variants; if there are at least 2 then the enum definitely needs a tag and thus may not remain uninitialized.
Cc @stepancheg