Skip to content

Fix recursive whitelisting and handling of opaque for derive default #889

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
Aug 4, 2017
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
11 changes: 7 additions & 4 deletions src/ir/analysis/derive_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
}
};

if ty.is_opaque(self.ctx, item) {
if item.is_opaque(self.ctx, &()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, is the idea that we missed the opaque_by_name case before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
l.opaque().can_trivially_derive_default()
});
Expand Down Expand Up @@ -265,7 +265,8 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {

let bases_cannot_derive = info.base_members()
.iter()
.any(|base| self.cannot_derive_default.contains(&base.ty));
.any(|base| !self.ctx.whitelisted_items().contains(&base.ty) ||
self.cannot_derive_default.contains(&base.ty));
if bases_cannot_derive {
trace!(" base members cannot derive Default, so we can't \
either");
Expand All @@ -277,12 +278,14 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
.any(|f| {
match *f {
Field::DataMember(ref data) => {
self.cannot_derive_default.contains(&data.ty())
!self.ctx.whitelisted_items().contains(&data.ty()) ||
self.cannot_derive_default.contains(&data.ty())
}
Field::Bitfields(ref bfu) => {
bfu.bitfields()
.iter().any(|b| {
self.cannot_derive_default.contains(&b.ty())
!self.ctx.whitelisted_items().contains(&b.ty()) ||
self.cannot_derive_default.contains(&b.ty())
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/expectations/tests/no-derive-debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/// and replacement for another type that doesn't implement it would prevent it
/// from building if --no-derive-debug didn't work.
#[repr(C)]
#[derive(Default, Copy)]
#[derive(Copy)]
pub struct bar {
pub foo: foo,
pub baz: ::std::os::raw::c_int,
Expand All @@ -34,3 +34,6 @@ fn bindgen_test_layout_bar() {
impl Clone for bar {
fn clone(&self) -> Self { *self }
}
impl Default for bar {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
5 changes: 4 additions & 1 deletion tests/expectations/tests/no-recursive-whitelisting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
pub enum Bar {}

#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Copy)]
pub struct Foo {
pub baz: *mut Bar,
}
Expand All @@ -25,3 +25,6 @@ fn bindgen_test_layout_Foo() {
impl Clone for Foo {
fn clone(&self) -> Self { *self }
}
impl Default for Foo {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
5 changes: 1 addition & 4 deletions tests/expectations/tests/opaque-template-inst-member-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@


#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Default, Copy, Clone)]
pub struct OpaqueTemplate {
}
impl Default for OpaqueTemplate {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct ContainsOpaqueTemplate {
Expand Down
5 changes: 1 addition & 4 deletions tests/expectations/tests/opaque-template-inst-member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@


#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Default, Copy, Clone)]
pub struct OpaqueTemplate {
}
impl Default for OpaqueTemplate {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[repr(C)]
pub struct ContainsOpaqueTemplate {
pub mBlah: [u32; 101usize],
Expand Down
5 changes: 1 addition & 4 deletions tests/expectations/tests/opaque_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ impl Clone for OtherOpaque {
}
/// <div rustbindgen opaque></div>
#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Opaque {
}
impl Default for Opaque {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct WithOpaquePtr {
Expand Down
5 changes: 1 addition & 4 deletions tests/expectations/tests/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,9 @@ impl Default for PODButContainsDtor {
}
/// <div rustbindgen opaque>
#[repr(C)]
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Opaque {
}
impl Default for Opaque {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[repr(C)]
#[derive(Debug, Default, Copy)]
pub struct POD {
Expand Down