rust-lang / rust-lang/rust-bindgen

Silently wrong code generated for enums in templated classes

Open
#2,871 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Input C/C++ Header
template <typename T>
class Test {
  enum class Enum { One, Two };
};
Bindgen Invocation

Tested both with the current release and current main:

$ bindgen input.h -- -x c++ -std=c++11
Actual Results
/* automatically generated by rust-bindgen 0.69.4 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Test {
    pub _address: u8,
}
pub const Test_Enum_One: Test_Enum = 0;
pub const Test_Enum_Two: Test_Enum = 0;
pub type Test_Enum = ::std::os::raw::c_int;
Expected Results

I would expect to see the same enum generated as for a non-templated class. Removing the first line of input.h results in this generated code:

/* automatically generated by rust-bindgen 0.69.4 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Test {
    pub _address: u8,
}
pub const Test_Enum_One: Test_Enum = 0;
pub const Test_Enum_Two: Test_Enum = 1;
pub type Test_Enum = ::std::os::raw::c_int;
#[test]
fn bindgen_test_layout_Test() {
    assert_eq!(
        ::std::mem::size_of::<Test>(),
        1usize,
        concat!("Size of: ", stringify!(Test))
    );
    assert_eq!(
        ::std::mem::align_of::<Test>(),
        1usize,
        concat!("Alignment of ", stringify!(Test))
    );
}

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 issue with input.h using the shown bindgen invocation, comparing the templated and non-templated class outputs. Trace how the C++ enum values are handled during generation, and consider the issue resolved when Test_Enum_Two is generated as 1 rather than 0 in the templated case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.