Skip to content

Commit

Permalink
Prevent default derives for Forward declared types.
Browse files Browse the repository at this point in the history
We do not know the layout for forward declared types.
Deriving Copy, Clone, Hash, PartialEq etc. is simply wrong.
Deriving Debug is debatable. The default derive might give a wrong
impression, since it prints out the inner unused field.
The opt-in manual implementation would print opaque, which seems
friendlier.

Changed tests:
- func-return-must-use: Don't use forward declared structs in the
  test, since that would result in wrong bindings!
- issue-1238-fwd-no-copy test no longer requires an annotation,
  since Forward declared types should always be no copy
  • Loading branch information
jschwe committed Feb 2, 2025
1 parent 03d49b6 commit c196db6
Show file tree
Hide file tree
Showing 21 changed files with 41 additions and 37 deletions.
2 changes: 0 additions & 2 deletions bindgen-tests/tests/expectations/tests/anon_union.rs

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

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

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

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

32 changes: 24 additions & 8 deletions bindgen-tests/tests/expectations/tests/func_return_must_use.rs

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

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

1 change: 0 additions & 1 deletion bindgen-tests/tests/expectations/tests/issue-1443.rs

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

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

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

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

1 change: 0 additions & 1 deletion bindgen-tests/tests/expectations/tests/layout_array.rs

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

1 change: 0 additions & 1 deletion bindgen-tests/tests/expectations/tests/operator.rs

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

1 change: 0 additions & 1 deletion bindgen-tests/tests/expectations/tests/parm-union.rs

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

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

1 change: 0 additions & 1 deletion bindgen-tests/tests/expectations/tests/template.rs

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

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

1 change: 0 additions & 1 deletion bindgen-tests/tests/expectations/tests/wrap-static-fns.rs

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

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

12 changes: 9 additions & 3 deletions bindgen-tests/tests/headers/func_return_must_use.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ typedef int MustUseInt;

MustUseInt return_int();

struct MustUseStruct;
struct MustUseStruct {
int a;
};

struct MustUseStruct return_struct();

Expand All @@ -20,11 +22,15 @@ int return_plain_int();
/**
* <div rustbindgen mustusetype></div>
*/
struct AnnotatedStruct {};
struct AnnotatedStruct {
int a;
};

struct AnnotatedStruct return_annotated_struct();

struct PlainStruct {};
struct PlainStruct {
int a;
};

/**
* <div rustbindgen mustusetype></div>
Expand Down
1 change: 0 additions & 1 deletion bindgen-tests/tests/headers/issue-1238-fwd-no-copy.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// bindgen-flags: --no-copy MyType

typedef struct MyType MyTypeT;
9 changes: 8 additions & 1 deletion bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2444,7 +2444,14 @@ impl CodeGenerator for CompInfo {
}
}

let derivable_traits = derives_of_item(item, ctx, packed);
let derivable_traits = if self.is_forward_declaration() {
// The only trait we could derive for forward declared types is `Debug`.
// However, since we don't actually know anything about the type, the
// Debug implementation would be useless.
DerivableTraits::empty()
} else {
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

0 comments on commit c196db6

Please sign in to comment.