rust-lang / rust-lang/rust

Redundant bounds check when indexing array with enum in Rust 1.90

Open
#147,831 3 comments 2 reactions 1 assignee View on GitHub

@dianqk is already working on this.

Since Nov 3, 2025.

A-LLVM C-optimization P-medium regression-from-stable-to-stable T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

I tried this code:

fn test(instructions: &[Instruction], registers: [u32; 4]) -> u32 {
    if let Some(&instruction) = instructions.get(0) {
        match instruction {
            Instruction::A(a) => registers[a as usize],
            Instruction::B(b) => registers[b as usize],
        }
    } else {
        0
    }
}

#[derive(Copy, Clone)]
enum Instruction {
    A(Register),
    B(Register),
}

#[derive(Copy, Clone)]
enum Register {
    A,
    B,
    C,
    D,
}

I expected to see this happen: No bounds check is emitted

Instead, this happened: There's a bounds check accessing registers

Version it worked on

It most recently worked on: Rust 1.89

Version with regression

rustc --version --verbose:

rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: x86_64-unknown-linux-gnu
release: 1.90.0
LLVM version: 20.1.8

Also checked in nightly:

rustc 1.92.0-nightly (53a741fc4 2025-10-16)
binary: rustc
commit-hash: 53a741fc4b8cf2d8e7b1b2336ed8edf889db84f4
commit-date: 2025-10-16
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.3

Bisected to b56aaec52bc0fa35591a872fb4aac81f606e265c / #144389 using cargo-bisect-rustc

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.