Skip to content

Commit f7ca97a

Browse files
committed
Add const eval tests ensuring padding gets correctly marked as deinit on deaggregation
1 parent 2a04028 commit f7ca97a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Diff for: src/test/ui/consts/const-eval/ub-enum-overwrite.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(const_mut_refs)]
2+
3+
enum E {
4+
A(u8),
5+
B,
6+
}
7+
8+
const _: u8 = {
9+
//~^ ERROR is undefined behavior
10+
let mut e = E::A(1);
11+
let p = if let E::A(x) = &mut e { x as *mut u8 } else { unreachable!() };
12+
// Make sure overwriting `e` uninitializes other bytes
13+
e = E::B;
14+
unsafe { *p }
15+
};
16+
17+
fn main() {}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0080]: it is undefined behavior to use this value
2+
--> $DIR/ub-enum-overwrite.rs:8:1
3+
|
4+
LL | / const _: u8 = {
5+
LL | |
6+
LL | | let mut e = E::A(1);
7+
LL | | let p = if let E::A(x) = &mut e { x as *mut u8 } else { unreachable!() };
8+
... |
9+
LL | | unsafe { *p }
10+
LL | | };
11+
| |__^ type validation failed: encountered uninitialized bytes, but expected initialized plain (non-pointer) bytes
12+
|
13+
= 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.
14+
= note: the raw bytes of the constant (size: 1, align: 1) {
15+
__ │ ░
16+
}
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)