Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions bindgen-tests/tests/expectations/tests/packed_flexarray.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions bindgen-tests/tests/headers/packed_flexarray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// bindgen-flags: --rust-target nightly --flexarray-dst --raw-line '#![cfg(feature = "nightly")]' --raw-line '#![feature(ptr_metadata, layout_for_ptr)]'

#pragma pack(1)

typedef struct PackedTest
{
short int Head;
long int Tail[];
}
PackedTest;

#pragma pack()

typedef struct UnpackedTest
{
short int Head;
long int Tail[];
}
UnpackedTest;
14 changes: 13 additions & 1 deletion bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,20 @@ impl FieldCodegen<'_> for FieldData {
} else if let Some(item) = field_ty.is_incomplete_array(ctx) {
// Only FAM if its the last field
if ctx.options().flexarray_dst && last_field {
// Check if parent struct is packed to determine if we need ManuallyDrop
let layout = parent_item.expect_type().layout(ctx);
let is_packed = parent.is_packed(ctx, layout.as_ref());
struct_layout.saw_flexible_array();
syn::parse_quote! { FAM }

// For packed structs, we need to wrap FAM in ManuallyDrop
// because Rust requires that DST fields in packed structs
// don't need Drop to be run.
if is_packed {
let prefix = ctx.trait_prefix();
syn::parse_quote! { ::#prefix::mem::ManuallyDrop<FAM> }
} else {
syn::parse_quote! { FAM }
}
} else {
result.saw_incomplete_array();

Expand Down