diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-12-20 17:07:40 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2020-12-20 21:29:47 +0100 |
commit | 669dc1b628b16da33cc382142c53f6377120a3b8 (patch) | |
tree | f08354425b49284f14abf02db51e817fc52b2d06 /src/codegen/mod.rs | |
parent | 98841b32ed51e4474377712aa636251d9a4f7215 (diff) |
comp: Fix bitfields to allow underaligned fields after them to take padding space.
Fixes #1947.
There are two separate issues here: First, the change in comp.rs ensures
that we don't round up the amount of storage to the alignment of the
bitfield. That generates the "expected" output in #1947
(`__BindgenBitfieldUnit<[u8; 3], u16>`).
But that's still not enough to fix that test-case because
__BindgenBitfieldUnit would be aligned and have padding, and Rust won't
put the extra field in the padding.
In order to ensure the bitfield starts at the right alignment, but that
Rust can put stuff in the extra field, we need to make a breaking change
and split the generated fields in two: One preceding that guarantees
alignment, and the actual storage, bit-aligned.
This keeps the existing behavior while fixing that test-case.
Diffstat (limited to 'src/codegen/mod.rs')
-rw-r--r-- | src/codegen/mod.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 0d93c491..194a461c 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1437,6 +1437,21 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit { } }; + { + let align_field_name = format!("_bitfield_align_{}", self.nth()); + let align_field_ident = ctx.rust_ident(&align_field_name); + let align_ty = match self.layout().align { + n if n >= 8 => quote! { u64 }, + 4 => quote! { u32 }, + 2 => quote! { u16 }, + _ => quote! { u8 }, + }; + let align_field = quote! { + pub #align_field_ident: [#align_ty; 0], + }; + fields.extend(Some(align_field)); + } + let unit_field_name = format!("_bitfield_{}", self.nth()); let unit_field_ident = ctx.rust_ident(&unit_field_name); |