You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
match on an enum with a single inhabited variant does not read discriminant #4778
Examples by @meithecatte; Cc @Nadrieril
The following code should likely trigger UB, but currently Miri raises no error:
#![feature(never_type)]#[repr(C)]#[allow(dead_code)]enumE{V1,// discriminant: 0V2(!),// 1}fnmain(){assert_eq!(std::mem::size_of::<E>(),4);let val = 1u32;let ptr = (&rawconst val).cast::<E>();let r = unsafe{&*ptr };match r {//~ we'd probably want ERROR: read discriminant of an uninhabited enum variantE::V1 => {}E::V2(_) => {}}}
The same goes for this variant that adds a closure (to check interactions with rust-lang/rust#138961):
#![feature(never_type)]#[repr(C)]#[allow(dead_code)]enumE{V0,// discriminant: 0V2(!),// 2}fnmain(){assert_eq!(std::mem::size_of::<E>(),4);let val = 2u32;let ptr = (&rawconst val).cast::<E>();let r = unsafe{&*ptr };let f = || {// After rust-lang/rust#138961, constructing the closure performs a reborrow of r.// Nevertheless, the discriminant is only actually inspected when the closure// is called.match r {//~ ERROR: read discriminant of an uninhabited enum variantE::V0 => {}E::V2(_) => {}}};f();}
Activity
theemathas commentedon Dec 19, 2025
This is similar to rust-lang/rust#142394
RalfJung commentedon Dec 19, 2025
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…
Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBr…