Skip to content

Soundness hole in pattern matching on enums with an uninhabited variant #61696

Closed
@strawberry-choco

Description

@strawberry-choco

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7a8a62d7a736d6bd54273422da29d7af

Click to see the example code from above link
#[derive(Debug)]
enum A {
    B(B),
    C(C),
    A1,
    A2,
}

#[derive(Debug)]
enum B {
    B1,
    B2,
    B3,
    B4,
    B5,
}

#[derive(Debug)]
enum C {}

fn main() {
    let a = A::B(B::B5);
    match a {
        A::B(_) => println!("success"),
        _ => println!("error: {:?}", a),
    }
}

Expected output: success

Actual output:

timeout: the monitored command dumped core
/root/entrypoint.sh: line 8:     7 Illegal instruction     timeout --signal=KILL ${timeout} "$@"

Running the above code on both Rust stable and nightly on my machine returns illegal hardware instruction (core dumped) with some number different on each run prefixing the output.

Some experimentations:

  • Implementing variants for enum C solves this issue.
  • Remove variants A1 and A2 solves this issue.
  • Use any variant of B other than B5 works.

I assume this is a bug in the compiler?

Metadata

Metadata

Assignees

Labels

A-codegenArea: Code generationC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions