Merge pull request #4805 from meithecatte/uninhabited-enum-validity

Add a validity testcase for uninhabited variants
This commit is contained in:
Ralf Jung 2026-02-04 09:01:54 +00:00 committed by GitHub
commit aace5bb00b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View file

@ -0,0 +1,17 @@
// NOTE: this is essentially a smoke-test, with more comprehensive tests living in the rustc
// repository at tests/ui/consts/const-eval/ub-enum.rs
#![feature(never_type)]
#[repr(C)]
#[allow(dead_code)]
enum E {
V1, // discriminant: 0
V2(!), // 1
}
fn main() {
unsafe {
std::mem::transmute::<u32, E>(1);
//~^ ERROR: encountered an uninhabited enum variant
}
}

View file

@ -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 | std::mem::transmute::<u32, E>(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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