Skip to content

SimplifyBranchSame assumes discriminant is the same as variant index. #89485

Closed
@tmiasko

Description

@tmiasko
$ cat a.rs 
#[derive(Debug, Eq, PartialEq)]
pub enum Type {
    A = 1,
    B = 2,
}
pub fn encode(v: Type) -> Type {
    match v {
        Type::A => Type::B,
        _ => v,
    }
}
fn main() {
  assert_eq!(Type::B, encode(Type::A));
}
$ rustc -Zmir-opt-level=0 a.rs && ./a
$ rustc a.rs && ./a
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `B`,
 right: `A`', a.rs:13:3
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Originally found by @bonega when experimenting with discriminants starting from 1 in #88984 (comment).

@rustbot modify labels: +regression-from-stable-to-stable +I-unsound

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-criticalCritical priorityregression-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