Skip to content

Commit

Permalink
const validation: add test for uninit bool
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jul 5, 2020
1 parent c3fc4f0 commit 751b594
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/test/ui/consts/const-eval/union-ub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#[repr(C)]
union DummyUnion {
unit: (),
u8: u8,
bool: bool,
}
Expand Down Expand Up @@ -30,6 +31,8 @@ union Bar {
// the value is not valid for bools
const BAD_BOOL: bool = unsafe { DummyUnion { u8: 42 }.bool};
//~^ ERROR it is undefined behavior to use this value
const UNINIT_BOOL: bool = unsafe { DummyUnion { unit: () }.bool};
//~^ ERROR it is undefined behavior to use this value

// The value is not valid for any union variant, but that's fine
// unions are just a convenient way to transmute bits around
Expand Down
12 changes: 10 additions & 2 deletions src/test/ui/consts/const-eval/union-ub.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
error[E0080]: it is undefined behavior to use this value
--> $DIR/union-ub.rs:31:1
--> $DIR/union-ub.rs:32:1
|
LL | const BAD_BOOL: bool = unsafe { DummyUnion { u8: 42 }.bool};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x2a, but expected a boolean
|
= 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.

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

error: aborting due to 2 previous errors

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

0 comments on commit 751b594

Please sign in to comment.