rust-lang / rust-lang/rust-bindgen

Renamed C++ enums wrapped in namespace produce incorrect variant types

Open
#2,103 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
namespace MyEnum
{
    enum Type
    {
        VALUE = 0,
        VALUE2
    };
}
Bindgen Invocation
bindgen::Builder::default()
    .header("input.h")
    .clang_args(&["-x", "c++"])
    .parse_callbacks(Box::new(TestCallback))
    .generate()
    .unwrap();

#[derive(Debug)]
struct TestCallback;

impl ParseCallbacks for TestCallback {
    fn item_name(&self, original_item_name: &str) -> Option<String> {
        match original_item_name {
            "MyEnum_Type" => Some("OtherEnum".into()),
            _ => None,
        }
    }
}
Actual Results
pub const OtherEnum_VALUE: MyEnum_Type = 0;
pub const OtherEnum_VALUE2: MyEnum_Type = 1;
pub type OtherEnum = ::std::os::raw::c_uint;
Expected Results

As you can see above, the variants such as OtherEnum_VALUE are renamed correctly, but the const type is using the old name, which no longer exists. They need to be using the new name of the type. This occurs when using newtype enums as well (did not try rustified enums).

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

Start by reproducing the bindgen Builder invocation with the provided C++ header and TestCallback::item_name implementation, then trace the generated enum variant declarations and type aliases. Done means renamed enum constants use OtherEnum rather than the obsolete MyEnum_Type, including for newtype enums.

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
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.