Skip to content

! return type function in struct was not emited and support ! type completely. #779

@XiangYyang

Description

@XiangYyang

I noted that #548 and have a similar issue.

Create a library crate firstly:

cargo new --lib example

Then, add them in lib.rs:

#[repr(C)]
pub struct Example {
    pub f: extern "C" fn(usize, usize) -> !,
}

#[no_mangle]
pub extern "C" fn add_itr(f: *const Example, a: usize, b: usize) -> ! {
    unsafe {
        ((*f).f)(a, b)
    }
}

#[no_mangle]
pub extern "C" fn add() -> ! {
    loop{}
}

And run this command in your terminal:

cbindgen --crate example --output example.h

Now, please see example.h, the output is that:

#include  ...

// notes here!!
struct Example;

extern "C" {

void add_itr(const Example *f, uintptr_t a, uintptr_t b);

void add();

} // extern "C"

The function which has ! return type was be ignored, but the other one add was emited successfully.

I think that we can add similar behavior to the structure, like emitting to void(*f)(size_t, size_t). And for the Nerver type, may be believe that C functions void(*)( ... some params ...) will never return is a good idea.

For more cases, about a C function pointer with Never return type, I noted that the following code can not be emited to C header:

// lib.rs
#[no_mangle]
pub extern "C" fn add(f: extern "C" fn() -> !) -> ! {
    f()
}

I think it's the key of this issue.

Could we possibly solve it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions