rust-lang / rust-lang/rust-bindgen

C++ method declared via a function typedef panics (How in the world?); virtual form drops this

Open
#3,486 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Input C/C++ Header
typedef int F(int);
struct S {
  F take;
  int x;
};
Bindgen Invocation
$ bindgen input.h -- -x c++ -std=c++17
Actual Results

bindgen aborts:

panicked at bindgen/codegen/mod.rs:3189:13:
How in the world?

This is a normal C++ method (int S::take(int)). clang accepts it. Function::parse stores cursor.cur_type(), which here is the typedef F (TypeKind::Alias). Method codegen then requires TypeKind::Function and panics. File-scope F take; does not panic because Function::codegen calls canonical_type().

A virtual method with the same typedef does not panic (if self.is_virtual() { return; }) but the emitted free function is missing this:

typedef int F(int);
struct S {
  virtual F take;
};
#[link_name = "\u{1}__ZN1S4takeEi"]
pub fn S_take(arg1: ::std::os::raw::c_int) -> ::std::os::raw::c_int;

The Itanium symbol is _ZN1S4takeEi with ABI (S*, int). Control virtual int take(int t); emits S_take(this: *mut c_void, t: c_int).

--wrap-static-fns on a C static declared via the same typedef hits unreachable! in serialize.rs (signature is still Alias):

typedef int F(int);
static F take;
static int take(int t) { return t + 10; }
panicked at bindgen/codegen/serialize.rs:77:13:
internal error: entered unreachable code
Expected Results

Parse the canonical function type (and inject this for CXXMethod) when the declaration uses a function typedef. Do not abort; for virtual methods emit S_take(this, t) like the ordinary virtual control.

Environment
bindgen: 0.73.1 (rust-lang/rust-bindgen 77cbc723)
clang/libclang: Homebrew clang 21.1.8
rustc: 1.97.1
target: aarch64-apple-darwin
OS: macOS

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 three headers with the bindgen invocation, then read Function::parse and method codegen in bindgen/codegen/mod.rs around line 3189. Also inspect bindgen/codegen/serialize.rs around line 77 for the static typedef case. Done means canonical function typedefs no longer panic, CXXMethod bindings include this, and wrapped static functions serialize successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.