Skip to content

Warn when exporting a non-extern-C function pointer #1125

@jrose-signal

Description

@jrose-signal
#[repr(C)]
pub struct Payload {
  data: [u8; 128],
}

#[repr(C)]
pub struct Example {
  callback: /* extern "C" */ fn(a: Payload, b: Payload),
}

#[unsafe(no_mangle)]
pub extern "C" fn test(input: Example) {}

cbindgen-ing this code (for C) produces

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct Payload {
  uint8_t data[128];
} Payload;

typedef struct Example {
  void (*callback)(struct Payload a, struct Payload b);
} Example;

void test(struct Example input);

with no warnings or errors. However, the ABI for callback isn't necessarily the same in C and Rust without the extern "C". (I recently ran into this, and was confused why my structs-passed-by-value had garbage in them until I tracked down my mistake.)

Related: #24, #55, #132.

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