Skip to content

Commit 930a6ac

Browse files
committed
Prevent default derives for Forward declared types.
We do not know the layout for forward declared types. Deriving Copy, Clone, Hash, PartialEq etc. is simply wrong. Deriving Debug is debatable, since it can't print anything useful. Since we previously would derive Debug, lets just keep it, to avoid needless breaking changes. 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 - forward-declaration-autoptr.rs: Manually implement clone. The inner type is unknown, so requiring clone/copy is not correct.
1 parent 03d49b6 commit 930a6ac

20 files changed

+80
-38
lines changed

bindgen-tests/tests/expectations/tests/anon_union.rs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/forward-declaration-autoptr.rs

+14-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/forward_declared_complex_types.rs

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/forward_declared_opaque.rs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/func_return_must_use.rs

+24-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/issue-1443.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/issue-654-struct-fn-collision.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/issue-710-must-use-type.rs

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/issue-801-opaque-sloppiness.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/layout_array.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/operator.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/parm-union.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/same_struct_name_in_different_namespaces.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/template.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/typedef-pointer-overlap.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/wrap-static-fns.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_anon_union.rs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/headers/func_return_must_use.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ typedef int MustUseInt;
44

55
MustUseInt return_int();
66

7-
struct MustUseStruct;
7+
struct MustUseStruct {
8+
int a;
9+
};
810

911
struct MustUseStruct return_struct();
1012

@@ -20,11 +22,15 @@ int return_plain_int();
2022
/**
2123
* <div rustbindgen mustusetype></div>
2224
*/
23-
struct AnnotatedStruct {};
25+
struct AnnotatedStruct {
26+
int a;
27+
};
2428

2529
struct AnnotatedStruct return_annotated_struct();
2630

27-
struct PlainStruct {};
31+
struct PlainStruct {
32+
int a;
33+
};
2834

2935
/**
3036
* <div rustbindgen mustusetype></div>
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
// bindgen-flags: --no-copy MyType
21

32
typedef struct MyType MyTypeT;

bindgen/codegen/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,17 @@ impl CodeGenerator for CompInfo {
24442444
}
24452445
}
24462446

2447-
let derivable_traits = derives_of_item(item, ctx, packed);
2447+
let derivable_traits = if self.is_forward_declaration() {
2448+
// The only trait we can derive for forward declared types is `Debug`,
2449+
// since we don't know anything about the layout or type.
2450+
let mut derivable_traits = DerivableTraits::empty();
2451+
if !item.annotations().disallow_debug() {
2452+
derivable_traits |= DerivableTraits::DEBUG;
2453+
};
2454+
derivable_traits
2455+
} else {
2456+
derives_of_item(item, ctx, packed)
2457+
};
24482458
if !derivable_traits.contains(DerivableTraits::DEBUG) {
24492459
needs_debug_impl = ctx.options().derive_debug &&
24502460
ctx.options().impl_debug &&

0 commit comments

Comments
 (0)