Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SRaimondi opened this issue Oct 25, 2022 · 1 comment
Closed

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

SRaimondi opened this issue Oct 25, 2022 · 1 comment
Labels
C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression.

Comments

@SRaimondi
Copy link

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
@SRaimondi SRaimondi added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Oct 25, 2022
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 25, 2022
@saethlin
Copy link
Member

I think this is a duplicate of #102303

@SRaimondi SRaimondi closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2022
@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression.
Projects
None yet
Development

No branches or pull requests

4 participants