Skip to content

Derive from any other trait only when deriving from Copy #2200

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 1 commit into from
May 7, 2022
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
35 changes: 23 additions & 12 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,13 @@ bitflags! {
}
}

fn derives_of_item(item: &Item, ctx: &BindgenContext) -> DerivableTraits {
fn derives_of_item(
item: &Item,
ctx: &BindgenContext,
packed: bool,
) -> DerivableTraits {
let mut derivable_traits = DerivableTraits::empty();

if item.can_derive_debug(ctx) && !item.annotations().disallow_debug() {
derivable_traits |= DerivableTraits::DEBUG;
}

if item.can_derive_default(ctx) && !item.annotations().disallow_default() {
derivable_traits |= DerivableTraits::DEFAULT;
}

let all_template_params = item.all_template_params(ctx);

if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() {
Expand All @@ -137,6 +133,18 @@ fn derives_of_item(item: &Item, ctx: &BindgenContext) -> DerivableTraits {
// It's not hard to fix though.
derivable_traits |= DerivableTraits::CLONE;
}
} else if packed {
// If the struct or union is packed, deriving from Copy is required for
// deriving from any other trait.
return derivable_traits;
}

if item.can_derive_debug(ctx) && !item.annotations().disallow_debug() {
derivable_traits |= DerivableTraits::DEBUG;
}

if item.can_derive_default(ctx) && !item.annotations().disallow_default() {
derivable_traits |= DerivableTraits::DEFAULT;
}

if item.can_derive_hash(ctx) {
Expand Down Expand Up @@ -926,7 +934,9 @@ impl CodeGenerator for Type {

let mut attributes =
vec![attributes::repr("transparent")];
let derivable_traits = derives_of_item(item, ctx);
let packed = false; // Types can't be packed in Rust.
let derivable_traits =
derives_of_item(item, ctx, packed);
if !derivable_traits.is_empty() {
let derives: Vec<_> = derivable_traits.into();
attributes.push(attributes::derives(&derives))
Expand Down Expand Up @@ -2026,7 +2036,7 @@ impl CodeGenerator for CompInfo {
}
}

let derivable_traits = derives_of_item(item, ctx);
let derivable_traits = derives_of_item(item, ctx, packed);
if !derivable_traits.contains(DerivableTraits::DEBUG) {
needs_debug_impl = ctx.options().derive_debug &&
ctx.options().impl_debug &&
Expand Down Expand Up @@ -3048,7 +3058,8 @@ impl CodeGenerator for Enum {
}

if !variation.is_const() {
let mut derives = derives_of_item(item, ctx);
let packed = false; // Enums can't be packed in Rust.
let mut derives = derives_of_item(item, ctx, packed);
// For backwards compat, enums always derive
// Clone/Eq/PartialEq/Hash, even if we don't generate those by
// default.
Expand Down
1 change: 0 additions & 1 deletion tests/expectations/tests/packed-vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#[repr(C)]
pub struct PackedVtable__bindgen_vtable(::std::os::raw::c_void);
#[repr(C, packed)]
#[derive(Debug)]
pub struct PackedVtable {
pub vtable_: *const PackedVtable__bindgen_vtable,
}
Expand Down