Skip to content
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

Prevent default derives for Forward declared types. #3110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 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.

2 changes: 1 addition & 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.

2 changes: 1 addition & 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.

2 changes: 1 addition & 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.

2 changes: 1 addition & 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.

2 changes: 1 addition & 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.

2 changes: 1 addition & 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;
12 changes: 11 additions & 1 deletion bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2444,7 +2444,17 @@ 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 can derive for forward declared types is `Debug`,
// since we don't know anything about the layout or type.
let mut derivable_traits = DerivableTraits::empty();
if !item.annotations().disallow_debug() {
derivable_traits |= DerivableTraits::DEBUG;
};
derivable_traits
} 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
2 changes: 1 addition & 1 deletion bindgen/ir/analysis/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl DeriveTrait {
}

fn can_derive_compound_forward_decl(self) -> bool {
matches!(self, DeriveTrait::Copy | DeriveTrait::Debug)
matches!(self, DeriveTrait::Debug)
}

fn can_derive_incomplete_array(self) -> bool {
Expand Down