rust-lang / rust-lang/rust-bindgen

Falls back to BindgenUnion if a field is array of blacklisted type

Open
#1,223 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-unions help wanted I-bogus-codegen
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

When I blacklist a type (because I want to import it from libc, not have it generated in my bindings.) Then bindgen fails to generate a union if a field in the C union has a field being an array of the blacklisted type, but not if the field has the same type, but isn't an array.

Input C/C++ Header

Correctly generats a union:

typedef int __u32;
union my_union {
    __u32       number;
};

Instead generates a BindgenUnion:

typedef int __u32;
union my_union {
    __u32       number[4];
};
Bindgen Invocation
$ bindgen union.h --blacklist-type __u32 --raw-line 'use libc::__u32;'
Actual Results

With first input header (no array):

#[repr(C)]
pub union my_union {
    pub number: __u32,
    _bindgen_union_align: u32,
}

With second input header, having an array in the union.

#[repr(C)]
pub struct my_union {
    pub number: __BindgenUnionField<[__u32; 4usize]>,
    pub bindgen_union_field: [u32; 4usize],
}
Expected Results

Both __u32 and [__u32; 4usize] should implement Copy and thus both should yield a union in the generated code.

EDIT: I'm using bindgen 0.32.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with union.h and the bindgen invocation shown, comparing a union field of __u32 with an array field of [__u32; 4]. Trace union generation and the Copy decision for direct versus array fields. Done means both inputs generate Rust unions rather than a BindgenUnion wrapper, with the blacklisted type preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.