Skip to content

Commit

Permalink
Add const eval tests ensuring padding gets correctly marked as deinit…
Browse files Browse the repository at this point in the history
… on deaggregation
  • Loading branch information
JakobDegen committed Apr 11, 2022
1 parent 2a04028 commit f7ca97a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/test/ui/consts/const-eval/ub-enum-overwrite.rs
Original file line number Diff line number Diff line change
@@ -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() {}
20 changes: 20 additions & 0 deletions src/test/ui/consts/const-eval/ub-enum-overwrite.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit f7ca97a

Please sign in to comment.