Skip to content

Conversation

jsgf
Copy link
Contributor

@jsgf jsgf commented Oct 22, 2025

Add support for structs where the last field is itself a struct containing a flexible array member (FAM). For example:

struct Name {
    int count;
    char name[];
};

struct Field {
    int id;
    struct Name name;  // Last field has a FAM
};

The Name struct is generated with a generic FAM parameter:

pub struct Name<FAM: ?Sized = [c_char; 0]> {
    pub count: c_int,
    pub name: FAM,
}

And Field propagates this parameter to its nested field:

pub struct Field<FAM: ?Sized = [c_char; 0]> {
    pub id: c_int,
    pub name: Name<FAM>,
}

For packed structs, the nested FAM field is wrapped in ManuallyDrop since packed structs cannot directly contain DST fields:

#[repr(C, packed)]
pub struct FieldPacked<FAM: ?Sized = [c_char; 0]> {
    pub id: c_int,
    pub name: ::std::mem::ManuallyDrop<Name<FAM>>,
}

Implementation:

  • Enhanced flex_array_member() in ir/comp.rs to recursively detect when a struct's last field is a compound type with its own FAM
  • Updated field code generation to parameterize nested FAM field types with and wrap in ManuallyDrop for packed structs
  • Added test cases for nested FAMs in both regular and packed structs

Related to #2936

Add support for structs where the last field is itself a struct
containing a flexible array member (FAM). For example:

    struct Name {
        int count;
        char name[];
    };

    struct Field {
        int id;
        struct Name name;  // Last field has a FAM
    };

The Name struct is generated with a generic FAM parameter:

    pub struct Name<FAM: ?Sized = [c_char; 0]> {
        pub count: c_int,
        pub name: FAM,
    }

And Field propagates this parameter to its nested field:

    pub struct Field<FAM: ?Sized = [c_char; 0]> {
        pub id: c_int,
        pub name: Name<FAM>,
    }

For packed structs, the nested FAM field is wrapped in ManuallyDrop
since packed structs cannot directly contain DST fields:

    #[repr(C, packed)]
    pub struct FieldPacked<FAM: ?Sized = [c_char; 0]> {
        pub id: c_int,
        pub name: ::std::mem::ManuallyDrop<Name<FAM>>,
    }

Implementation:
- Enhanced flex_array_member() in ir/comp.rs to recursively detect
  when a struct's last field is a compound type with its own FAM
- Updated field code generation to parameterize nested FAM field
  types with <FAM> and wrap in ManuallyDrop for packed structs
- Added test cases for nested FAMs in both regular and packed structs

Related to rust-lang#2936
meta-codesync bot pushed a commit to facebook/sapling that referenced this pull request Oct 23, 2025
Summary:
This includes 3 recent PRs:
- rust-lang/rust-bindgen#3305 - Fix packed structs with flexible array members (merged)
- rust-lang/rust-bindgen#3306 - Support nested flexible array members
- rust-lang/rust-bindgen#3307 - Add support for custom per-field attributes

The last one is specifically intended to allow per-field attributes from proc-macros to be applied.

These have been cherry-picked onto the v0.72.1 release branch, as the main branch is still listed as v0.72.0 and I didn't want a backwards version step. The upstream branch is https://github.com/jsgf/rust-bindgen/tree/PR-merged

Reviewed By: dtolnay

Differential Revision: D85273915

fbshipit-source-id: 211916b9611cbfa409296a6854b4a94bba2c5195
meta-codesync bot pushed a commit to facebookexperimental/rust-shed that referenced this pull request Oct 23, 2025
Summary:
This includes 3 recent PRs:
- rust-lang/rust-bindgen#3305 - Fix packed structs with flexible array members (merged)
- rust-lang/rust-bindgen#3306 - Support nested flexible array members
- rust-lang/rust-bindgen#3307 - Add support for custom per-field attributes

The last one is specifically intended to allow per-field attributes from proc-macros to be applied.

These have been cherry-picked onto the v0.72.1 release branch, as the main branch is still listed as v0.72.0 and I didn't want a backwards version step. The upstream branch is https://github.com/jsgf/rust-bindgen/tree/PR-merged

Reviewed By: dtolnay

Differential Revision: D85273915

fbshipit-source-id: 211916b9611cbfa409296a6854b4a94bba2c5195
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant