Skip to content

Commit df5113a

Browse files
committed
Add a validity testcase for uninhabited variants
1 parent 5d149f2 commit df5113a

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(never_type)]
2+
3+
#[repr(C)]
4+
#[allow(dead_code)]
5+
enum E {
6+
V1, // discriminant: 0
7+
V2(!), // 1
8+
}
9+
10+
fn main() {
11+
assert_eq!(std::mem::size_of::<E>(), 4);
12+
13+
let val = 1u32;
14+
let ptr = (&raw const val).cast::<E>();
15+
unsafe { ptr.read() }; //~ ERROR: encountered an uninhabited enum variant
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: Undefined Behavior: constructing invalid value at .<enum-tag>: encountered an uninhabited enum variant
2+
--> tests/fail/validity/uninhabited_variant.rs:LL:CC
3+
|
4+
LL | unsafe { ptr.read() };
5+
| ^^^^^^^^^^ Undefined Behavior occurred here
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
10+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)