rust-lang / rust-lang/rust-bindgen

C++ std::function types are translated into [u128; 3usize]

Open
#1,724 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-templates bug
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

header.hpp:

using example_type = std::function<void()>;

bindgen invocation:

let bindings = bindgen::Builder::default()
        .header("header.hpp")
        .opaque_type("std::.*")
        .whitelist_type("example_type")
        .rust_target(bindgen::RustTarget::Nightly)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");

Output binding:

pub type example_type = [u128; 3usize];

With just a plain typedef like this all is well, but in many situations bindgen also needs to use this type (possibly indirectly) in an extern "C" {...} block, resulting in warnings like this:

warning: `extern` block uses type `[u128; 3usize]`, which is not FFI-safe
    --> src/../bindings.rs:9772:18
     |
9772 |             root::example_type;
     |             ^^^^^^^^^^^^^^^^^^ not FFI-safe
     |
     = help: consider passing a pointer to the array
     = note: passing raw arrays by value is not FFI-safe

warning: `extern` block uses type `u128`, which is not FFI-safe
    --> src/../bindings.rs:1230:27
     |
1230 |                     this: *const root::example_type,
     |                           ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
     |
     = note: 128-bit integers don't currently have a known stable ABI

While not strictly an error, it makes me uneasy that function pointers may or may not be interpreted correctly between the C++ library I need to use and my own code.

Is there a way to get bindgen to generate, say, &[u64; 6usize] or something for std::functions instead? Is this intended behavior or unexpected?

I have a feeling this could be related to the use of .opaque_type("std::.*"), but whitelisting std::function doesn't seem to change anything and I'd greatly prefer not to generate bindings for all of std.

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.

Research direction

Reproduce the report with the shown header.hpp and bindgen::Builder invocation, then inspect the generated example_type alias and the resulting FFI warnings. Review how bindgen translates std::function and decide whether the output is intentional or needs a supported representation. Done means the expected behavior is established and documented or demonstrated by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.