rust-lang / rust-lang/rust-bindgen

`Not an item` panic for incomplete union

Open
#2,160 0 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
namespace {
namespace {
template <typename> union a { a b; };
} // namespace
} // namespace

This is nonsensical but results from reduction of a larger real codebase in https://github.com/google/autocxx/issues/779, so is presumably this is the net result of some complex arrangement of templates.

It does compile OK with clang++:

$ cat <<HERE > test.cc
> namespace {
namespace {
template <typename> union a { a b; };
} // namespace
} // namespace
> HERE
$ clang++ -c test.cc 
$ clang++ --version
Debian clang version 13.0.1-+rc1-1~exp4
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Bindgen Invocation
$ git checkout ca78ae9c41998713db0c6fa70cd21089cdb2af2c # current master
$ cargo run -- test.hpp
Actual Results
thread 'main' panicked at 'Not an item: ItemId(3)', src/ir/context.rs:1464:21
stack backtrace:
   0: rust_begin_unwind
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/core/src/panicking.rs:107:14
   2: bindgen::ir::context::BindgenContext::resolve_item
             at ./src/ir/context.rs:1464:21
   3: bindgen::ir::context::BindgenContext::resolve_type
             at ./src/ir/context.rs:1428:9
   4: bindgen::ir::ty::Type::layout::{{closure}}
             at ./src/ir/ty.rs:232:21
   5: core::option::Option<T>::or_else
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/core/src/option.rs:1147:21
   6: bindgen::ir::ty::Type::layout
             at ./src/ir/ty.rs:219:9
   7: bindgen::ir::ty::Type::layout::{{closure}}
             at ./src/ir/ty.rs:232:21
   8: core::option::Option<T>::or_else
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/core/src/option.rs:1147:21
   9: bindgen::ir::ty::Type::layout
             at ./src/ir/ty.rs:219:9
  10: bindgen::ir::comp::CompInfo::each_known_field_layout
             at ./src/ir/comp.rs:1159:43
  11: bindgen::ir::comp::CompInfo::layout
             at ./src/ir/comp.rs:1115:9
  12: bindgen::ir::ty::Type::layout::{{closure}}
             at ./src/ir/ty.rs:221:43
  13: core::option::Option<T>::or_else
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/core/src/option.rs:1147:21
  14: bindgen::ir::ty::Type::layout
             at ./src/ir/ty.rs:219:9
  15: bindgen::ir::context::BindgenContext::compute_bitfield_units::{{closure}}
             at ./src/ir/context.rs:976:30
  16: bindgen::ir::context::BindgenContext::with_loaned_item
             at ./src/ir/context.rs:956:22
  17: bindgen::ir::context::BindgenContext::compute_bitfield_units
             at ./src/ir/context.rs:974:13
  18: bindgen::ir::context::BindgenContext::gen
             at ./src/ir/context.rs:1150:9
  19: bindgen::codegen::codegen
             at ./src/codegen/mod.rs:4289:5
  20: bindgen::Bindings::generate
             at ./src/lib.rs:2401:32
  21: bindgen::Builder::generate
             at ./src/lib.rs:1478:9
  22: bindgen::main::{{closure}}
             at ./src/main.rs:52:17
  23: std::panicking::try::do_call
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/std/src/panicking.rs:406:40
  24: __rust_try
  25: std::panicking::try
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/std/src/panicking.rs:370:19
  26: std::panic::catch_unwind
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/std/src/panic.rs:133:14
  27: bindgen::main
             at ./src/main.rs:51:34
  28: core::ops::function::FnOnce::call_once
             at /rustc/65f3f8b220f020e562c5dd848ff7319257a7ba45/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Expected Results

It probably doesn't make much sense to generate any bindings here, but this is currently preventing generation of other things in the same codebase.

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 C++ header and cargo run -- test.hpp. Trace the reported path through src/ir/context.rs, src/ir/ty.rs, and src/ir/comp.rs, starting at BindgenContext::resolve_item and layout computation. Done means this incomplete union no longer panics while other bindings in the same input can still be generated.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.