Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix template union forward declaration, close #1768 #2423

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions bindgen-tests/tests/headers/union_template_forward_decl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// bindgen-flags: --with-derive-hash --with-derive-partialeq --with-derive-eq
//
template <typename value_t>
union declare_union; // Primary template declared, but never defined.

template <typename value_t> union declare_union<value_t *> {
declare_union() {}
declare_union(value_t *a_value) : value(a_value) {}
value_t *value;
};

template <typename value_t> union define_union {
define_union() {}
define_union(value_t *a_value) : value(a_value) {}
value_t *value;
int dummy;
};
3 changes: 3 additions & 0 deletions bindgen/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,9 @@ impl CompInfo {
CXCursor_ParmDecl => true,
CXCursor_StructDecl | CXCursor_UnionDecl |
CXCursor_ClassDecl => !cur.is_definition(),
CXCursor_ClassTemplate => {
kind == CompKind::Union && !cur.is_definition()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not an issue for structs?

Copy link

@aleb aleb Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below is a sample union XXX item failing. The panic happens because no layout can be obtained from the item. This happens because layout: None and (in case of Unions) fields: []. In case of struct XXX, the codepath is different, the layout is simply not mandatory. See audocxx-bindgen/.../codegen/mod.rs:

impl CodeGenerator for CompInfo {

    fn codegen(...) {
        ...
        if is_opaque {
            ...
        } else if !is_union && !zero_sized {
            ... struct handled here, basically nothing is done without a `layout`
        } else if is_union && !forward_decl {
            // TODO(emilio): It'd be nice to unify this with the struct path
            // above somehow.
            let layout = layout.expect("Unable to get layout information?");
            ...
        }

        ...
    }
  cargo:warning=MESSAGE:<CompInfo as CodeGenerator>::codegen: item = Item { id: ItemId(23), local_id: OnceCell(<uninit>), next_child_local_id: Cell { value: 1 }, canonical_name: OnceCell(<uninit>), path_for_allowlisting: OnceCell(["root", "Sandwich"]), comment: None, annotations: Annotations { opaque: false, hide: false, use_instead_of: None, disallow_copy: false, disallow_debug: false, disallow_default: false, must_use_type: false, visibility_kind: None, accessor_kind: None, constify_enum_variant: false, derives: [] }, parent_id: ItemId(0), kind: Type(Type { name: Some("Sandwich"), layout: None, kind: Comp(CompInfo { kind: Union, visibility: Public, fields: After { fields: [], has_bitfield_units: false }, template_params: [TypeId(ItemId(24))], methods: [], constructors: [], destructor: None, base_members: [], inner_types: [], inner_vars: [], has_own_virtual_method: false, has_destructor: false, has_nonempty_base: false, has_non_type_template_params: false, has_unevaluable_bit_field_width: false, packed_attr: false, found_unknown_attr: false, is_forward_declaration: false }), is_const: false }), location: Some(include/aaa.hh:3:7) }
  cargo:warning=MESSAGE:Offset: _address: 0 -> 1
  thread 'main' panicked at /home/minion/.cargo/registry/src/index.crates.io-6f17d22bba15001f/autocxx-bindgen-0.69.5/codegen/mod.rs:2291:33:
  Unable to get layout information?

(obtained from https://github.com/aleb/autocxx-test/tree/v1 with RUST_LOG=debug RUST_BACKTRACE=full cargo build -r with this include.h)

}
_ => false,
});

Expand Down