Skip to content

feat: add --opaque-type-alias-as-c-void option - #3434

Open
dhmztr wants to merge 1 commit into
rust-lang:mainfrom
dhmztr:feature/opaque-as-c-void
Open

feat: add --opaque-type-alias-as-c-void option#3434
dhmztr wants to merge 1 commit into
rust-lang:mainfrom
dhmztr:feature/opaque-as-c-void

Conversation

@dhmztr

@dhmztr dhmztr commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1874.

Adds an option to emit opaque type aliases as c_void instead of a sized byte blob, e.g.:

// before
pub type WINDOW = [u64; 11usize];

// after, with --opaque-type-alias-as-c-void
pub type WINDOW = ::core::ffi::c_void;

This is useful for types that are only ever handled behind a pointer (e.g. WINDOW in curses), where communicating "this can't be read from or written to directly" is more valuable than preserving the type's size and alignment through a same-sized blob.

Implementation

  • New bool option opaque_type_alias_as_c_void, following the exact same shape as the existing impl_debug option (bindgen/options/mod.rs), including CLI wiring in bindgen/options/cli.rs (--opaque-type-alias-as-c-void).
  • Wired into the opaque type-alias codegen branch in bindgen/codegen/mod.rs (Type::codegen's TypeKind::Alias/TemplateAlias handling): when the option is set and the alias target is opaque, emits helpers::ast_ty::c_void(ctx) instead of the usual self.to_opaque(ctx, item) blob.
  • Reuses the pre-existing helpers::ast_ty::c_void helper (already used elsewhere for TypeKind::Void/TypeKind::NullPtr), so --ctypes-prefix and --use-core are automatically respected with no extra code.

Scope

This only affects the top-level opaque type-alias path (typedef struct Foo Foo;-style declarations), matching the issue's own example exactly. It does not affect:

  • Opaque struct/union declared without a typedef (a separate codegen path in CompInfo::codegen).
  • Opaque struct fields (which go through the unconditional ToOpaque blanket impl used for padding/layout elsewhere, and must stay sized for the containing type's layout to be correct).

Test plan

  • New regression test issue-1874-opaque-type-alias-as-c-void.h/.rs, using a WINDOW-style opaque typedef (the issue's own motivating example).
  • Manually confirmed via the built CLI: flag off → unchanged pub type WINDOW = u8; blob output (no regression); flag on → pub type WINDOW = ::std::os::raw::c_void;; flag on + --use-corepub type WINDOW = ::core::ffi::c_void;.
  • Confirmed the generated output actually compiles (rustc --crate-type lib).
  • Full bindgen-tests corpus passes aside from 2 pre-existing, unrelated failures also present on unmodified main (a toolchain-version-dependent const-generics codegen difference, and a dump_preprocessed_input test needing the clang CLI binary rather than just libclang).
  • cargo clippy -p bindgen -p bindgen-cli --all-targets -- -D warnings clean.

Adds an option to emit opaque type aliases as `c_void` instead of a
sized byte blob (e.g. `[u64; 11usize]`).

This is useful for types that are only ever handled behind a pointer
(e.g. `WINDOW` in curses), where communicating "this can't be read
from or written to directly" is more valuable than preserving the
type's size and alignment through a same-sized blob.

Reuses the existing `helpers::ast_ty::c_void` helper (already used
for `TypeKind::Void`/`TypeKind::NullPtr`), which correctly respects
`--ctypes-prefix` and `--use-core`.

Scoped to the top-level opaque type-alias codegen path only (i.e.
`typedef struct Foo Foo;`-style declarations) - does not affect
opaque struct/union declarations without a typedef, or opaque struct
fields, which go through a separate codegen path and must remain
sized.

Fixes rust-lang#1874
Copilot AI lite review requested due to automatic review settings August 19, 2026 10:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new opt-in bindgen configuration/CLI flag to render opaque type aliases (typedef-style opaques) as c_void rather than a sized opaque byte-blob, aligning with the use case where the type is only intended to be handled behind pointers.

Changes:

  • Introduces new Builder/options boolean: opaque_type_alias_as_c_void and wires it to CLI as --opaque-type-alias-as-c-void.
  • Updates alias codegen so that when an alias is considered opaque and the option is enabled, it emits helpers::ast_ty::c_void(ctx) instead of to_opaque(...).
  • Adds a regression test case for issue #1874 ensuring the alias becomes c_void under the new flag.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
bindgen/options/mod.rs Adds a new boolean option and builder method for opaque_type_alias_as_c_void.
bindgen/options/cli.rs Wires the new option into the CLI and applies it onto the Builder.
bindgen/codegen/mod.rs Switches opaque type-alias emission to c_void when the option is enabled.
bindgen-tests/tests/headers/issue-1874-opaque-type-alias-as-c-void.h Adds a header-based regression test using the new flag.
bindgen-tests/tests/expectations/tests/issue-1874-opaque-type-alias-as-c-void.rs Adds expected generated output validating WINDOW aliases to c_void.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

Option to output opaque type as c_void

2 participants