Skip to content

codegen: Avoid crashing on variadic unions without layout information. #3220

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

Merged
merged 2 commits into from
Jun 8, 2025
Merged
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
14 changes: 14 additions & 0 deletions bindgen-tests/tests/expectations/tests/variadic-union.rs

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

3 changes: 3 additions & 0 deletions bindgen-tests/tests/headers/variadic-union.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
template <typename... _Types> union _Variadic_union {
_Variadic_union() = default;
};
4 changes: 2 additions & 2 deletions bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ impl Cursor {
}

/// Gets the tokens that correspond to that cursor.
pub(crate) fn tokens(&self) -> RawTokens {
pub(crate) fn tokens(&self) -> RawTokens<'_> {
RawTokens::new(self)
}

Expand Down Expand Up @@ -1006,7 +1006,7 @@ impl<'a> RawTokens<'a> {
}

/// Get an iterator over these tokens.
pub(crate) fn iter(&self) -> ClangTokenIterator {
pub(crate) fn iter(&self) -> ClangTokenIterator<'_> {
ClangTokenIterator {
tu: self.tu,
raw: self.as_slice().iter(),
Expand Down
23 changes: 11 additions & 12 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2337,18 +2337,17 @@ impl CodeGenerator for CompInfo {
}
}
} 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?");
if struct_layout.requires_explicit_align(layout) {
explicit_align = Some(layout.align);
}

if !struct_layout.is_rust_union() {
let ty = helpers::blob(ctx, layout, false);
fields.push(quote! {
pub bindgen_union_field: #ty ,
});
if let Some(layout) = layout {
// TODO(emilio): It'd be nice to unify this with the struct path above somehow.
if struct_layout.requires_explicit_align(layout) {
explicit_align = Some(layout.align);
}
if !struct_layout.is_rust_union() {
let ty = helpers::blob(ctx, layout, false);
fields.push(quote! {
pub bindgen_union_field: #ty ,
});
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"

fn assert_no_dangling_item_traversal(
&self,
) -> traversal::AssertNoDanglingItemsTraversal {
) -> traversal::AssertNoDanglingItemsTraversal<'_> {
assert!(self.in_codegen_phase());
assert_eq!(self.current_module, self.root_module);

Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl Type {
}

/// Takes `name`, and returns a suitable identifier representation for it.
fn sanitize_name(name: &str) -> Cow<str> {
fn sanitize_name(name: &str) -> Cow<'_, str> {
if clang::is_valid_identifier(name) {
return Cow::Borrowed(name);
}
Expand Down
2 changes: 1 addition & 1 deletion bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ impl Bindings {
}

/// Gets the rustfmt path to rustfmt the generated bindings.
fn rustfmt_path(&self) -> io::Result<Cow<PathBuf>> {
fn rustfmt_path(&self) -> io::Result<Cow<'_, PathBuf>> {
debug_assert!(matches!(self.options.formatter, Formatter::Rustfmt));
if let Some(ref p) = self.options.rustfmt_path {
return Ok(Cow::Borrowed(p));
Expand Down