Skip to content

Commit 751b594

Browse files
committed
const validation: add test for uninit bool
1 parent c3fc4f0 commit 751b594

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/test/ui/consts/const-eval/union-ub.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#[repr(C)]
44
union DummyUnion {
5+
unit: (),
56
u8: u8,
67
bool: bool,
78
}
@@ -30,6 +31,8 @@ union Bar {
3031
// the value is not valid for bools
3132
const BAD_BOOL: bool = unsafe { DummyUnion { u8: 42 }.bool};
3233
//~^ ERROR it is undefined behavior to use this value
34+
const UNINIT_BOOL: bool = unsafe { DummyUnion { unit: () }.bool};
35+
//~^ ERROR it is undefined behavior to use this value
3336

3437
// The value is not valid for any union variant, but that's fine
3538
// unions are just a convenient way to transmute bits around
+10-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
error[E0080]: it is undefined behavior to use this value
2-
--> $DIR/union-ub.rs:31:1
2+
--> $DIR/union-ub.rs:32:1
33
|
44
LL | const BAD_BOOL: bool = unsafe { DummyUnion { u8: 42 }.bool};
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x2a, but expected a boolean
66
|
77
= 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.
88

9-
error: aborting due to previous error
9+
error[E0080]: it is undefined behavior to use this value
10+
--> $DIR/union-ub.rs:34:1
11+
|
12+
LL | const UNINIT_BOOL: bool = unsafe { DummyUnion { unit: () }.bool};
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized bytes, but expected a boolean
14+
|
15+
= 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.
16+
17+
error: aborting due to 2 previous errors
1018

1119
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)