rust-lang / rust-lang/rust-bindgen

--represent-cxx-operators panics: "S_operator()" is not a valid Ident

Open
#3,482 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
struct S {
    int x;
    int operator()(int t) const;
};
Bindgen Invocation
$ bindgen input.h --represent-cxx-operators -- -x c++ -std=c++17
Actual Results

bindgen aborts:

panicked at bindgen/ir/context.rs:903:9:
"S_operator()" is not a valid Ident

Without the flag, operators are skipped. With --represent-cxx-operators, FunctionSig::from_ty keeps operator(), then codegen does rust_ident("S_operator()"). rust_mangle only rewrites @, ?, $, and keywords, so the parentheses reach Ident::new.

Same panic for operator[], operator+, operator==. operator int does not panic (that spelling is already a valid Ident).

The rustdoc for the flag says the names are not valid Rust and you need ParseCallbacks::generated_name_override. The CLI has no callback, so bindgen panics instead of emitting something or skipping. #3142 added the flag for post-processors that rename; it did not document an abort.

rust_mangle has the same hole for clang-accepted non-XID names, e.g. gnu11 struct 🐶 { int x; };"🐶" is not a valid Ident. CJK struct 测试 is fine (XID).

Expected Results

Do not abort. Mangle operator() to a valid Ident (or skip with a diagnostic). Callers can still rename via generated_name_override.

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 panic with the shown bindgen command, then inspect bindgen/ir/context.rs around the reported line, FunctionSig::from_ty, codegen, and rust_mangle. Check how operator() and other clang-accepted names reach Ident::new. Done means the command no longer aborts: invalid operator names are mangled to valid identifiers or skipped with a diagnostic, while generated_name_override can still rename them.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.