Warn when exporting a non-extern-C function pointer

Open
#1,125 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
c, rust
Domain
devtools

Research direction

Start by reproducing the provided Rust example and C output with cbindgen, then trace the handling of function-pointer fields and ABI declarations. Done means cbindgen emits a warning when a non-extern-C Rust function pointer is exported, while extern "C" pointers remain accepted.

Written by the indexing model from the issue text.

Description

#[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.

Dominant language
Rust
Stars
3k
Forks
386
Avg merge
1h 43m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mozilla/cbindgen

All issues in mozilla/cbindgen

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.