Skip to content

match on an enum with a single inhabited variant does not read discriminant #4778

Closed
rust-lang/rust
#150681

Description

@RalfJung

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)]
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>();
    let r = unsafe { &*ptr };
    match r { //~ we'd probably want ERROR: read discriminant of an uninhabited enum variant
        E::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)]
enum E {
  V0, // discriminant: 0
  V2(!), // 2
}

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

    let val = 2u32;
    let ptr = (&raw const 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 variant
            E::V0 => {}
            E::V2(_) => {}
        }
    };

    f();
}

Activity

  1. added
    A-mirArea: this is about the MIR that we are executing, not about how we are executing it
    I-misses-theoretical-UBImpact: Miri does not report UB when it should, but codegen also "misses" this UB
    C-bugCategory: This is a bug.
    on Dec 19, 2025
  2. theemathas commented on Dec 19, 2025

    @theemathas
    Contributor

    This is similar to rust-lang/rust#142394

  3. RalfJung commented on Dec 19, 2025

    @RalfJung
    MemberAuthor
  4. added a commit that references this issue on Feb 14, 2026
    f846389
  5. added 4 commits that reference this issue on Feb 15, 2026
    4a42d40
    439586b
    7b4dcb5
    886a6db
  6. added 2 commits that reference this issue on Mar 19, 2026
    b14d462
    a4ac794
  7. added a commit that references this issue on Mar 27, 2026
    576fef7
  8. added a commit that references this issue on Mar 31, 2026
    6f02083
  9. added a commit that references this issue on Aug 7, 2026
    eb1a75c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mirArea: this is about the MIR that we are executing, not about how we are executing itC-bugCategory: This is a bug.I-misses-theoretical-UBImpact: Miri does not report UB when it should, but codegen also "misses" this UB

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions