From 47a835118748a29cc6c55eec1abd0cabab13c45d Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Thu, 13 Aug 2026 23:57:18 -0400 Subject: [PATCH] Don't derive or implement traits for aliases to `c_void` --- .../tests/expectations/tests/typedef-void-newtype.rs | 3 +++ bindgen-tests/tests/headers/typedef-void-newtype.h | 2 ++ bindgen/codegen/mod.rs | 10 ++++++++-- bindgen/ir/analysis/derive.rs | 3 +-- 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 bindgen-tests/tests/expectations/tests/typedef-void-newtype.rs create mode 100644 bindgen-tests/tests/headers/typedef-void-newtype.h diff --git a/bindgen-tests/tests/expectations/tests/typedef-void-newtype.rs b/bindgen-tests/tests/expectations/tests/typedef-void-newtype.rs new file mode 100644 index 0000000000..a55254bb99 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/typedef-void-newtype.rs @@ -0,0 +1,3 @@ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#[repr(transparent)] +pub struct myvoid(pub ::std::os::raw::c_void); diff --git a/bindgen-tests/tests/headers/typedef-void-newtype.h b/bindgen-tests/tests/headers/typedef-void-newtype.h new file mode 100644 index 0000000000..bd999e73a9 --- /dev/null +++ b/bindgen-tests/tests/headers/typedef-void-newtype.h @@ -0,0 +1,2 @@ +// bindgen-flags: --new-type-alias myvoid --with-derive-hash --with-derive-partialeq --with-derive-partialord --with-derive-eq --with-derive-ord --impl-debug --impl-partialeq +typedef void myvoid; diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index d61f87c901..9a8615b79c 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -1112,7 +1112,11 @@ impl CodeGenerator for Type { needs_debug_impl = ctx.options().derive_debug && ctx.options().impl_debug && !ctx.no_debug_by_name(item) && - !item.annotations().disallow_debug(); + !item.annotations().disallow_debug() && + !item.as_type().is_some_and(|ty| { + ty.safe_canonical_type(ctx) + .is_some_and(|ty| ty.is_void()) + }); } let mut derives: Vec<_> = derivable_traits.into(); // The custom derives callback may return a list of derive attributes; @@ -1126,7 +1130,9 @@ impl CodeGenerator for Type { }); // In most cases this will be a no-op, since custom_derives will be empty. append_custom_derives(&mut derives, &custom_derives); - attributes.push(attributes::derives(&derives)); + if !derives.is_empty() { + attributes.push(attributes::derives(&derives)); + } let custom_attributes = ctx.options().all_callbacks(|cb| { diff --git a/bindgen/ir/analysis/derive.rs b/bindgen/ir/analysis/derive.rs index e78d2f5fef..b8ac270439 100644 --- a/bindgen/ir/analysis/derive.rs +++ b/bindgen/ir/analysis/derive.rs @@ -189,7 +189,6 @@ impl CannotDerive<'_> { match *ty.kind() { // Handle the simple cases. These can derive traits without further // information. - TypeKind::Void | TypeKind::NullPtr | TypeKind::Int(..) | TypeKind::Complex(..) | @@ -213,6 +212,7 @@ impl CannotDerive<'_> { TypeKind::Function(ref sig) => { self.derive_trait.can_derive_fnptr(sig) } + TypeKind::Void => CanDerive::No, // Complex cases need more information TypeKind::Array(t, len) => { @@ -534,7 +534,6 @@ impl DeriveTrait { // === Default === ( DeriveTrait::Default, - TypeKind::Void | TypeKind::NullPtr | TypeKind::Enum(..) | TypeKind::Reference(..) |