rust-lang / rust-lang/rust-bindgen

Wrong layout for generic union when not used untagged unions.

Open
#1,503 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Input C/C++ Header
enum class StyleTimingKeyword : unsigned char {
  Foo,
  Bar,
};

enum class StyleStepPosition : unsigned char {
  Baz,
  Bar,
};

template<typename Integer, typename Number>
union StyleTimingFunction {
  enum class Tag : unsigned char {
    Keyword,
    CubicBezier,
    Steps
  };

  struct Keyword_Body {
    Tag tag;
    StyleTimingKeyword _0;
  };

  struct CubicBezier_Body {
    Tag tag;
    Number x1;
    Number y1;
    Number x2;
    Number y2;
  };

  struct Steps_Body {
    Tag tag;
    Integer _0;
    StyleStepPosition _1;
  };

  struct {
    Tag tag;
  };
  Keyword_Body keyword;
  CubicBezier_Body cubic_bezier;
  Steps_Body steps;
};

struct Foo {
  StyleTimingFunction<int, float> f;
};
Bindgen Invocation
$ ./target/debug/bindgen --rust-target 1.0 t.hpp -o out.rs
$ rustc --test out.rs
$ ./out
Actual Results
---- __bindgen_test_layout_StyleTimingFunction_open0_int_float_close0_instantiation stdout ----
thread '__bindgen_test_layout_StyleTimingFunction_open0_int_float_close0_instantiation' panicked at 'assertion failed: `(left == right)`
  left: `0`,
 right: `20`: Size of template specialization: StyleTimingFunction < :: std :: os :: raw :: c_int , f32 >', out.rs:131:5

The union has:

    pub bindgen_union_field: [u8; 0usize],

Because we cannot determine the struct layout at that point.

Expected Results

The test passes. We get this right with untagged unions.

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 using the C++ header shown in t.hpp, then run the bindgen invocation and rustc --test on the generated out.rs. Start with the generated layout assertion for StyleTimingFunction<int, float> and its bindgen_union_field; done means the generated test passes with the expected specialization size.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.