feat: add --opaque-type-alias-as-c-void option - #3434
Open
dhmztr wants to merge 1 commit into
Open
Conversation
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
There was a problem hiding this comment.
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_voidand 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 ofto_opaque(...). - Adds a regression test case for issue #1874 ensuring the alias becomes
c_voidunder 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1874.
Adds an option to emit opaque type aliases as
c_voidinstead of a sized byte blob, e.g.:This is useful for types that are only ever handled behind a pointer (e.g.
WINDOWin 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
booloptionopaque_type_alias_as_c_void, following the exact same shape as the existingimpl_debugoption (bindgen/options/mod.rs), including CLI wiring inbindgen/options/cli.rs(--opaque-type-alias-as-c-void).bindgen/codegen/mod.rs(Type::codegen'sTypeKind::Alias/TemplateAliashandling): when the option is set and the alias target is opaque, emitshelpers::ast_ty::c_void(ctx)instead of the usualself.to_opaque(ctx, item)blob.helpers::ast_ty::c_voidhelper (already used elsewhere forTypeKind::Void/TypeKind::NullPtr), so--ctypes-prefixand--use-coreare 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:struct/uniondeclared without a typedef (a separate codegen path inCompInfo::codegen).ToOpaqueblanket impl used for padding/layout elsewhere, and must stay sized for the containing type's layout to be correct).Test plan
issue-1874-opaque-type-alias-as-c-void.h/.rs, using aWINDOW-style opaque typedef (the issue's own motivating example).pub type WINDOW = u8;blob output (no regression); flag on →pub type WINDOW = ::std::os::raw::c_void;; flag on +--use-core→pub type WINDOW = ::core::ffi::c_void;.rustc --crate-type lib).bindgen-testscorpus passes aside from 2 pre-existing, unrelated failures also present on unmodifiedmain(a toolchain-version-dependent const-generics codegen difference, and adump_preprocessed_inputtest needing theclangCLI binary rather than justlibclang).cargo clippy -p bindgen -p bindgen-cli --all-targets -- -D warningsclean.