Add const eval tests ensuring padding gets correctly marked as deinit on deaggregation

This commit is contained in:
Jakob Degen 2022-04-05 19:12:09 -04:00
parent 2a040284a5
commit f7ca97a209
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,17 @@
#![feature(const_mut_refs)]
enum E {
A(u8),
B,
}
const _: u8 = {
//~^ ERROR is undefined behavior
let mut e = E::A(1);
let p = if let E::A(x) = &mut e { x as *mut u8 } else { unreachable!() };
// Make sure overwriting `e` uninitializes other bytes
e = E::B;
unsafe { *p }
};
fn main() {}

View File

@ -0,0 +1,20 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-enum-overwrite.rs:8:1
|
LL | / const _: u8 = {
LL | |
LL | | let mut e = E::A(1);
LL | | let p = if let E::A(x) = &mut e { x as *mut u8 } else { unreachable!() };
... |
LL | | unsafe { *p }
LL | | };
| |__^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
= note: the raw bytes of the constant (size: 1, align: 1) {
__ │ ░
}
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.