-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Strange "Discriminant value outside specified type" with unsigned C-like enums #20600
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
Comments
Strange, I thought it would be because it's "0" as opposed to "0u8" and so the compiler was inferring a larger integer type, but even with "!0u8" it still complains, even though typeof(!0u8) == u8, and "!0u8 as u8" works fine.
|
I believe I figured out what is causing this behavior though I don't know how it should be fixed really. The problem seems to come from the constant evaluator which only deals with 64-bit numbers. !0u8 is basically treated as !0u64 making a number that is to large when the bounds check is done. Might be that the constant evaluator need to take types into account. |
Related #22531 |
cc #23897 |
This is fixed in nightly. |
Confirmed, thanks @eefriedman ! |
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).
The text was updated successfully, but these errors were encountered: