Skip to content

Bounds check are added for array when they should not (most likely) #103528

Closed as not planned
@SRaimondi

Description

@SRaimondi

Code

I tried this code:

#[derive(Copy, Clone)]
pub enum Test {
    A = 0,
    B = 1,
    C = 2,
}

pub fn test(arr: [u32; 3], t: Test) -> u32 {
    arr[t as usize]
}

and checked the generated code for Rust 1.64 and 1.63, see here.

I expected the compiler to omit the bounds check on the array since the values of the enum are known and it's not possible to have a value outside of the allowed range of indices (unless you use transmute but that would be unsafe).

This is generated as expected for Rust 1.63.0 but the bounds check is added for Rust 1.64.0.

Is this expected behaviour?

Version it worked on

It most recently worked on: 1.63.0

Version with regression

rustc --version --verbose:

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-unknown-linux-gnu
release: 1.64.0
LLVM version: 14.0.6
Compiler returned: 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.regression-untriagedUntriaged performance or correctness regression.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions