Skip to content
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

typedef struct X * X generates invalid Rust code #1252

Closed
ignatenkobrain opened this issue Feb 13, 2018 · 5 comments
Closed

typedef struct X * X generates invalid Rust code #1252

ignatenkobrain opened this issue Feb 13, 2018 · 5 comments

Comments

@ignatenkobrain
Copy link

Input C/C++ Header

struct X {
  int x;
};
typedef struct X * X;

Bindgen Invocation

$ bindgen input.h

Actual Results

/* automatically generated by rust-bindgen */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct X {
    pub x: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_X() {
    assert_eq!(
        ::std::mem::size_of::<X>(),
        4usize,
        concat!("Size of: ", stringify!(X))
    );
    assert_eq!(
        ::std::mem::align_of::<X>(),
        4usize,
        concat!("Alignment of ", stringify!(X))
    );
    assert_eq!(
        unsafe { &(*(::std::ptr::null::<X>())).x as *const _ as usize },
        0usize,
        concat!("Offset of field: ", stringify!(X), "::", stringify!(x))
    );
}
pub type X = *mut X;

Expected Results

I'm not sure what exact output is correct, but I think struct X { ... } X; is equivalent to struct X { ... }; typedef struct X * X;, so probably right thing to do is:

-pub type X = *mut X;
+extern "C" {
+    #[link_name = "\u{1}X"]
+    pub static mut X: X;
+}
@ignatenkobrain
Copy link
Author

Or, probably, it should rename original structure.. I'm not really sure what's the right thing to do here.

@emilio
Copy link
Contributor

emilio commented Feb 13, 2018

Yeah, I think renaming it is the right thing, though it's slightly annoying... We may want to support a "C" naming mode, where structs become struct_X, enums become enum_X, etc...

@emilio
Copy link
Contributor

emilio commented Feb 13, 2018

For now the best solution would be to blacklist it, preventing it from getting generated, but that's annoying.

@ignatenkobrain
Copy link
Author

ignatenkobrain commented Feb 13, 2018

@emilio is there some workaround I can use meanwhile? I really need that poptOption (the real version of X mentioned here) because bindings I generate have it as argument to some functions.

@tony-iqlusion
Copy link

@ignatenkobrain FWIW I too just had to deal with bindgen and popt.h.

My "solution" was to have bindgen produce a C++ binding instead of a C one, which triggers some #ifdef __cplusplus gating inside of popt.h which disables these typedefs.

@emilio emilio closed this as completed in 4943058 Apr 30, 2021
LoganBarnett pushed a commit to LoganBarnett/rust-bindgen that referenced this issue Dec 2, 2023
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

No branches or pull requests

3 participants