Nested pattern matching on an enum with niche optimization generates multiple switches
Open
@roife is already working on this.
Since Jan 29, 2026.
C-optimization
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
enum E1 {
A,
B,
C,
D,
E,
}
enum E2 {
E1(E1),
AA,
BB,
}
#[unsafe(no_mangle)]
pub fn xx(e2: E2) -> i32 {
match e2 {
E2::E1(E1::A) => 999,
E2::E1(E1::B) => 888,
E2::E1(E1::C) => 422,
E2::E1(E1::D) => 483,
E2::E1(_) => std::hint::black_box(213),
E2::AA => 123,
E2::BB => 456,
}
}
rustc generates 2 switch instructions, first on tag - 4, then on tag. LLVM fails to merge the 2 switches. Ideally there should be only 1 single switch:
switch i8 %0, label %bb1 [
i8 0, label %bb10
i8 1, label %bb8
i8 2, label %bb7
i8 3, label %bb6
i8 4, label %bb5
i8 5, label %bb2
i8 6, label %bb10
i8 7, label %bb3
]
Godbolt link: https://godbolt.org/z/jzxa3osGo.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.