rust-lang / rust-lang/rust

Nested pattern matching on an enum with niche optimization generates multiple switches

Open
#149,333 1 comment 0 reactions 1 assignee View on GitHub

@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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.