Skip to content

Enums are no longer considered bounded #107418

New issue

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

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

Already on GitHub? # to your account

Closed
VorfeedCanal opened this issue Jan 28, 2023 · 2 comments
Closed

Enums are no longer considered bounded #107418

VorfeedCanal opened this issue Jan 28, 2023 · 2 comments
Labels
C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression.

Comments

@VorfeedCanal
Copy link

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!).

@VorfeedCanal VorfeedCanal added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Jan 28, 2023
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jan 28, 2023
@lukas-code
Copy link
Member

This is fixed in 1.67.0 (#103584), which was released a few days ago. Godbolt just isn't updated yet.

@scottmcm
Copy link
Member

Playground confirms this is fixed on stable (1.67.0): https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=d7b5d65a8e0c13005d2b1ce4fa773801

playground::reg_name:
	movzx	ecx, dil
	shl	rcx, 4
	lea	rdx, [rip + .L__unnamed_1]
	mov	rax, qword ptr [rcx + rdx]
	mov	rdx, qword ptr [rcx + rdx + 8]
	ret

(And it's still fixed in nightly (2023-01-27) too.)

@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jan 31, 2023
# for free to join this conversation on GitHub. Already have an account? # 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

5 participants