rust-lang / rust-lang/rust-bindgen

allowlist with opaque_type on `typedef struct A {} A;` generates invalid code

Open
#2,106 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header
typedef struct Foo {} Foo;
Foo *foo_new(void);
Bindgen Invocation
bindgen input.h --no-layout-tests --allowlist-function foo_new --opaque-type Foo
Actual Results
/* automatically generated by rust-bindgen 0.59.1 */

extern "C" {
    pub fn foo_new() -> *mut Foo;
}
Expected Results

Without the opaque-type argument, this code is generated:

/* automatically generated by rust-bindgen 0.59.1 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {}
extern "C" {
    pub fn foo_new() -> *mut Foo;
}

If --allowlist-type Foo is added as well, then the codegen is ok:

/* automatically generated by rust-bindgen 0.59.1 */

#[repr(C)]
#[repr(align(1))]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
    pub _bindgen_opaque_blob: [u8; 0usize],
}
extern "C" {
    pub fn foo_new() -> *mut Foo;
}

It appears to be a bug with a typedef of a struct using the same name as the struct, because with this input:

typedef struct _Foo {} Foo;
Foo *foo_new(void);

Then all those commands work well:

bindgen input.h --no-layout-tests --allowlist-function 'foo_new' --opaque-type Foo
bindgen input.h --no-layout-tests --allowlist-function 'foo_new' --opaque-type _Foo
bindgen input.h --no-layout-tests --allowlist-function 'foo_new' --opaque-type Foo --opaque-type _Foo

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

Start by reproducing the issue with the shown C++ header and bindgen invocation, then trace the opaque-type handling and generated declaration for the same-name typedef. Done means the command emits a valid Rust definition for Foo while preserving the foo_new() binding.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.