@sizeOf() packed struct changes depending on relative sizes of members #10104
Labels
bug
Observed behavior contradicts documented or intended behavior
miscompilation
The compiler reports success but produces semantically incorrect code.
stage1
The process of building from source via WebAssembly and the C backend.
Milestone
Remember to search before filing a new report
Zig Version
0.9.0 (built with Nix from master commit e97feb9)
Steps to Reproduce
Run
zig test
on the above.Expected Behavior
@bitSizeOf(txdata)
and@bitSizeOf(txdata_passes)
should be 32, and@sizeOf(txdata)
and@sizeOf(txdata_passes)
should be 4.Actual Behavior
@sizeOf(txdata)
is 5.This came up when implementing MMIO registers here: https://github.com/mirrexagon/luxos/blob/a0d46e992ac256685ee36a85f9f255299f0fbe7d/src/target/soc/fe310-g002/uart.zig#L50
Trying to
@bitCast()
betweentxdata
andusize
(32 bits onriscv32
) failed due to this. I can work around this by increasing the size of thedata
member fromu8
tou9
, and decreasing the size of the next member by one bit (u23
tou22
) - doing that makes the sizes match up.Also affects this register: https://github.com/mirrexagon/luxos/blob/a0d46e992ac256685ee36a85f9f255299f0fbe7d/src/target/arch/riscv/mcsr.zig#L17
Is this something that should work? My understanding is that packed structs that fit exactly into a whole number of bytes should not have any padding.
I'm unsure if this is related to or would be fixed by #8102.
The text was updated successfully, but these errors were encountered: