-
Notifications
You must be signed in to change notification settings - Fork 698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replacing native C types (Windows & Mac vs Linux) #1663
Comments
Other code in standard library (or user code, really) may actually be using I would have expected this to work if you run this with So I think the main issue is that the edge from So basically something like this line: Line 1233 in 79b2b10
May be able to check its name and avoid tracing the inner type in that case. I don't know how that works perf-wise, would need some measuring. |
I got most of the way through the new issue template before noticing this seems to be the same issue I was experiencing. So I'm pasting that here. Hopefully the reduced testcase is useful to someone. Input C/C++ Header
typedef unsigned int __uint32_t;
typedef __uint32_t uint32_t;
typedef struct Foo { uint32_t id; } Foo; Bindgen Invocation
Actual Results/* automatically generated by rust-bindgen 0.59.2 */
#[repr(C)]
pub struct Foo {
pub id: u32,
}
// Layout test elided Expected ResultsI expected the struct to have /* automatically generated by rust-bindgen 0.59.2 */
pub type __uint32_t = ::std::os::raw::c_uint;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
pub id: u32,
} These typedefs are based on what was in typedef unsigned int __uint32_t;
typedef __uint32_t uint32_t; This is related to #1215 but following one of the suggestions there to blocklist the undesired types causes |
While bindgen is able to replace the C uint types with the correct Rust types, it still generates typedefs for __uint{8,16,32,64}_t [0]. We don’t need these typedefs, so we add a patch to remove them. [0] rust-lang/rust-bindgen#1663
#2287 made me realize about all this and I wonder if we could fix this from the native types side. What I mean is that we could remove all this logic about special-casing We could also remove redundant aliases from inside the so at the end of the day, bindgen would process these headers typedef unsigned char uint64_t;
typedef unsigned int uint32_t;
uint64_t foo();
uint32_t bar(); and, with the flag disabled, emit this: type uint64_t = c_uchar;
type uint32_t = c_uint;
extern "C" {
pub fn foo() -> uint64_t;
pub fn bar() -> uint32_t;
} with the flag enabled, emit this: type uint64_t = u8;
extern "C" {
pub fn foo() -> uint64_t;
pub fn bar() -> u32;
} instead of the current behaviour which is this: extern "C" {
pub fn foo() -> u64;
pub fn bar() -> u32;
} Maybe I'm being to naive by assuming that the size reported by clang is correct in this context 😅 but we could add extra layout tests for that just in case. |
Bindgen nicely replaces occurrences of native C types with Rust types. For example:
results in:
It works. Lovely! One problem, once we run CI, it turns out that Ubuntu also generates this additional line
Even though bindgen is able to deduce that
uint8_t
is supposed to beu8
, it still adds the__uint8_t
type to the generated bindings.One possible solution that I came up with to resolved this is to blacklist the
__uint8_t
type, but then bindgen stops automatically generating the#[derive(Clone, Copy)]
attributes.Is there a way to prevent the generation of "replaced" types?
The text was updated successfully, but these errors were encountered: