Skip to content

Strange "Discriminant value outside specified type" with unsigned C-like enums #20600

Closed
@Marwes

Description

@Marwes

This code fails with a compile error when using the smaller unsigned types (u8, u16, u32) as a representation for the enum. For signed types or u64 the same code compiles fine so it seems there is some issue with truncation for unsigned types.

As a workaround it is possible to use an explicit cast (!0 as u8).

#[repr(u8)]
enum Test {
    A = 0,
    B = !0
}

fn main() {
}
error.rs:6:5: 6:11 error: discriminant value outside specified type [E0082]
error.rs:6     B = !0
               ^~~~~~
error.rs:3:8: 3:11 note: discriminant type specified here
error.rs:3 #[repr(u8)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions