-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
add a compile error for using @bitCast to convert an enum to an integer #3647
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
This seems like a totally arbitrary compile failure. A rule like this can cause unnecessary complexity in generic code. I don't really see a good reason why |
Struct layout for non |
That's a good point, but the struct cast is the one that compiles. This issue would make the enum cast invalid. On a related note though, does that mean this is UB?
|
Wait a minute... Struct layout is not guaranteed to be in any consistent order, but it must be defined. Otherwise |
Ah, nevermind. I misunderstood the intent of bitCast. Sorry for the fuss. |
This would force any generic function dealing with bit casts to add a special case for enums. If anything I think |
Does this conflict with #1467? |
If I access microcontroller peripheral I use packed structs with enums inside for register access. E.g. STM32:
How can I do this without enums if |
Compile error has been implemented test.zig:10:23: error: cannot @bitCast from 'z.Number'
_ = @bitCast(u32, x);
^
test.zig:10:23: note: use @enumToInt to cast to 'u32' |
There's even a test. |
The proper cast is
@enumToInt
:The text was updated successfully, but these errors were encountered: