rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

An attribute on C callback function leads to wrong bindings

Open buybackoff opened this issue 5 years ago • 6 comments

If in C code I have this line:

typedef void (mi_cdecl mi_output_fun)(const char* msg, void* arg);

and this function:

mi_decl_export void mi_stats_print_out(mi_output_fun* out, void* arg) mi_attr_noexcept;

then the generated binding is:

extern "C" {
    pub fn mi_stats_print_out(
        out: ::core::option::Option<unsafe extern "C" fn()>,
        arg: *mut libc::c_void,
    );
}

All arguments are lost.

To fix this, I need to remove mi_cdecl from the callback function:

typedef void mi_output_fun(const char* msg, void* arg);

and then generated bindings are reasonable:

pub type mi_output_fun =
    ::core::option::Option<unsafe extern "C" fn(msg: *const libc::c_char, arg: *mut libc::c_void)>;

extern "C" {
    pub fn mi_stats_print_out(out: mi_output_fun, arg: *mut libc::c_void);
}

The code is here.

buybackoff avatar Feb 16 '20 09:02 buybackoff

If there's a chance to come up with a standalone test-case that doesn't involve the whole allocator it'd be really appreciated. I think it shouldn't be hard, I can try to give it a shot later, but I'm pretty swamped this week...

emilio avatar Feb 17 '20 07:02 emilio

Also clang version involved would be helpful.

emilio avatar Feb 17 '20 07:02 emilio

Clang 9.0 on Windows. (On Linux bindgen fails with "cannot find libclang.so..." after installing LLVM with default .sh, but that is a separate issue).

Only this wrapper and build script part are needed to reproduce.

buybackoff avatar Feb 17 '20 08:02 buybackoff

A wrapper is not great because it depends on a bunch of other stuff. https://github.com/rust-lang/rust-bindgen/blob/master/CONTRIBUTING.md#using-creduce-to-minimize-test-cases has some instructions to get a more reduced test-case but I can give it a shot later.

emilio avatar Feb 17 '20 18:02 emilio

@buybackoff have you had time to find a minimal example?

pvdrz avatar Sep 21 '22 20:09 pvdrz

@buybackoff have you had time to find a minimal example?

No, I fixed my issue as described and it's no longer an issue for me.

buybackoff avatar Sep 21 '22 20:09 buybackoff