Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/fail/validity/uninhabited_variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(never_type)]

#[repr(C)]
#[allow(dead_code)]
enum E {
V1, // discriminant: 0
V2(!), // 1
}

fn main() {
assert_eq!(std::mem::size_of::<E>(), 4);

let val = 1u32;
let ptr = (&raw const val).cast::<E>();
unsafe { ptr.read() }; //~ ERROR: encountered an uninhabited enum variant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simple transmute from 1u32 should have the same result, right? That would even take care of the size check.

}
13 changes: 13 additions & 0 deletions tests/fail/validity/uninhabited_variant.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: Undefined Behavior: constructing invalid value at .<enum-tag>: encountered an uninhabited enum variant
--> tests/fail/validity/uninhabited_variant.rs:LL:CC
|
LL | unsafe { ptr.read() };
| ^^^^^^^^^^ Undefined Behavior occurred here
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error