Skip to content

Redundant bounds check when indexing array with enum in Rust 1.90 #147831

@ictrobot

Description

@ictrobot

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 b56aaec / #144389 using cargo-bisect-rustc

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-prioritizeIssue: Indicates that prioritization has been requested for this issue.T-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