You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Shouldn't _bindgen_opaque_blob be generated without pub? Making it pub allows the user to directly instantiate an instance of BAR__ which is most likely invalid, which defeats the purpose of it being an opaque type
Other Information
rust-bindgen 0.71.1
clang version 19.1.7
Target: x86_64-pc-windows-msvc
Full Code Sample build.rs:
use std::env;use std::path::PathBuf;fnmain(){
tracing_subscriber::fmt::init();let builder = bindgen::Builder::default().header_contents("input.h",r#"struct Foo;struct BAR__{int unused;};typedef struct BAR__ *BAR;"#,).parse_callbacks(Box::new(bindgen::CargoCallbacks::new())).derive_default(true);let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
builder
.clone().generate().expect("Unable to generate bindings").write_to_file((out_path).join("not-explicit-opaque.rs")).expect("Couldn't write bindings!");
builder
.opaque_type("Foo").opaque_type("BAR__")// Only BAR__ should be marked as opaque. The convenience type BAR should remain as a pointer to the opaque BAR__. .generate().expect("Unable to generate bindings").write_to_file((out_path).join("explicit-opaque.rs")).expect("Couldn't write bindings!");}
Output: not-explicit-opaque.rs:
/* automatically generated by rust-bindgen 0.71.1 */#[repr(C)]#[derive(Debug,Copy,Clone)]pubstructFoo{_unused:[u8;0],}#[repr(C)]#[derive(Debug,Default,Copy,Clone)]pubstructBAR__{pubunused:::std::os::raw::c_int,}#[allow(clippy::unnecessary_operation, clippy::identity_op)]const _:() = {["Size of BAR__"][::std::mem::size_of::<BAR__>() - 4usize];["Alignment of BAR__"][::std::mem::align_of::<BAR__>() - 4usize];["Offset of field: BAR__::unused"][::std::mem::offset_of!(BAR__, unused) - 0usize];};pubtypeBAR = *mutBAR__;
explicit-opaque.rs:
/* automatically generated by rust-bindgen 0.71.1 */#[repr(C)]#[derive(Debug,Default,Copy,Clone)]pubstructFoo{_unused:[u8;0],}#[repr(C)]#[repr(align(4))]#[derive(Debug,Default,Copy,Clone)]pubstructBAR__{pub_bindgen_opaque_blob:u32,}#[allow(clippy::unnecessary_operation, clippy::identity_op)]const _:() = {["Size of BAR__"][::std::mem::size_of::<BAR__>() - 4usize];["Alignment of BAR__"][::std::mem::align_of::<BAR__>() - 4usize];};pubtypeBAR = *mutBAR__;
Output Diff (left is no explicit opaque. right is explicit opaque):
The text was updated successfully, but these errors were encountered:
This is issue may be related to #3096, but I see these as 2 separate problems (#3096: default getting derived on opaque types, #3097: opaque types being generated with a pub member)
It is probably worth noting, that bindgen makes the difference between "Forward declared types" (i.e. layout is unknown) and "opaque". In bindgen terms, opaque implies that the memory layout (size and alignment) of the type is known, just the internals are hidden.
For a type that is explicitly marked opaque via
.opaque_type("Foo")
, bindgen generates a type with a public member:Shouldn't
_bindgen_opaque_blob
be generated withoutpub
? Making it pub allows the user to directly instantiate an instance ofBAR__
which is most likely invalid, which defeats the purpose of it being an opaque typeOther Information
rust-bindgen 0.71.1
clang version 19.1.7
Target: x86_64-pc-windows-msvc
Full Code Sample
build.rs
:Output:
not-explicit-opaque.rs
:explicit-opaque.rs
:Output Diff (left is no explicit opaque. right is explicit opaque):
![Image](https://private-user-images.githubusercontent.com/16629657/406272290-e7b2bf6a-44b3-40de-962f-321d8cc62261.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxNDU1NDksIm5iZiI6MTczOTE0NTI0OSwicGF0aCI6Ii8xNjYyOTY1Ny80MDYyNzIyOTAtZTdiMmJmNmEtNDRiMy00MGRlLTk2MmYtMzIxZDhjYzYyMjYxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA5VDIzNTQwOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWUzNjIwMjVkYThmZDZjNTQ5MjVjOWZiN2I0ZGZiYmNjM2RmZmIzMTk2YzdlYWMzN2E1YzEyNzc1NzEwMTY1MGQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.UKiwf8W_GF2CVfZW-g2iJi7MNXg2UswdrNDXCIYOX3M)
The text was updated successfully, but these errors were encountered: