E0587 error on packed and aligned structures from C · Issue #59154 · rust-lang/rust (original) (raw)

When converting C structures that are both packed and aligned using either C2Rust or bindgen, such as:

struct xregs_state { struct fxregs_state i387; struct xstate_header header; u8 extended_state_area[0]; } attribute ((packed, aligned (64)));

the code that either tool produces looks something like:

#[repr(C, packed(64))] #[repr(align(64))] pub struct xregs_state { pub i387: fxregs_state, pub header: xstate_header, pub extended_state_area: __IncompleteArrayField, }

This Rust code fails to compile due to error E0587:

error[E0587]: type has conflicting packed and align representation hints
    --> .../out/bindings.rs:3894:1
     |
3894 | / pub struct xregs_state {
3895 | |     pub i387: fxregs_state,
3896 | |     pub header: xstate_header,
3897 | |     pub extended_state_area: __IncompleteArrayField<u8>,
3898 | | }
     | |_^

We can work around this in C2Rust by emitting an aligned outer/packed inner structure pair (I think bindgen could do the same), but I'm wondering if it would be better to fix this on the Rust language/compiler side.