Skip to content

Enums are no longer considered bounded #107418

Closed
@VorfeedCanal

Description

@VorfeedCanal

Code

I tried this code:

pub fn reg_name(x: RegisterNo) -> &'static str {
    NAME[x as usize]
}

const NAME: [&'static str; 8] = [
    "al", "cl", "dl", "bl",
    "ah", "ch", "dh", "bh"
];

#[repr(i8)]
pub enum RegisterNo {
    AL = 0,
    CL = 1,
    DL = 2,
    BL = 3,
    AH = 4,
    CH = 5,
    DH = 6,
    BH = 7,
}

I expected to see this happen: all range checks would be eliminated and function would be panic-free.

Instead, this happened: range checks are still there and function is no longer panic-free.

Version it worked on

It most recently worked on: 1.63

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

Workaround that currently works is to use NAME[x as usize & 0x7].

But this seems strange, wrong, and shouldn't be necessary (it worked in the past!).

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