From 1abd864972012b2c027fc3378f1db88333f5d6ea Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 21 Mar 2017 14:04:22 +0100 Subject: [PATCH 1/2] Do not derive Default on vtable types https://github.com/servo/rust-bindgen/pull/597#issuecomment-288006557 --- src/codegen/mod.rs | 6 +----- tests/expectations/tests/enum_and_vtable_mangling.rs | 1 - tests/expectations/tests/nested_vtable.rs | 1 - tests/expectations/tests/ref_argument_array.rs | 1 - tests/expectations/tests/virtual_dtor.rs | 1 - tests/expectations/tests/virtual_inheritance.rs | 2 -- tests/expectations/tests/virtual_overloaded.rs | 1 - tests/expectations/tests/vtable_recursive_sig.rs | 1 - 8 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 7db083e728..577b8aabfc 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -706,11 +706,7 @@ impl<'a> CodeGenerator for Vtable<'a> { assert_eq!(item.id(), self.item_id); // For now, generate an empty struct, later we should generate function // pointers and whatnot. - let mut attributes = vec![attributes::repr("C")]; - - if ctx.options().derive_default { - attributes.push(attributes::derives(&["Default"])) - } + let attributes = vec![attributes::repr("C")]; let vtable = aster::AstBuilder::new() .item() diff --git a/tests/expectations/tests/enum_and_vtable_mangling.rs b/tests/expectations/tests/enum_and_vtable_mangling.rs index 300edd3931..f1a580931f 100644 --- a/tests/expectations/tests/enum_and_vtable_mangling.rs +++ b/tests/expectations/tests/enum_and_vtable_mangling.rs @@ -10,7 +10,6 @@ pub const whatever_else: _bindgen_ty_1 = _bindgen_ty_1::whatever_else; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum _bindgen_ty_1 { match_ = 0, whatever_else = 1, } #[repr(C)] -#[derive(Default)] pub struct C__bindgen_vtable { } #[repr(C)] diff --git a/tests/expectations/tests/nested_vtable.rs b/tests/expectations/tests/nested_vtable.rs index e16a23d71d..62466ee013 100644 --- a/tests/expectations/tests/nested_vtable.rs +++ b/tests/expectations/tests/nested_vtable.rs @@ -5,7 +5,6 @@ #[repr(C)] -#[derive(Default)] pub struct nsISupports__bindgen_vtable { } #[repr(C)] diff --git a/tests/expectations/tests/ref_argument_array.rs b/tests/expectations/tests/ref_argument_array.rs index 714467f6f3..ac761796f1 100644 --- a/tests/expectations/tests/ref_argument_array.rs +++ b/tests/expectations/tests/ref_argument_array.rs @@ -6,7 +6,6 @@ pub const NSID_LENGTH: ::std::os::raw::c_uint = 10; #[repr(C)] -#[derive(Default)] pub struct nsID__bindgen_vtable { } #[repr(C)] diff --git a/tests/expectations/tests/virtual_dtor.rs b/tests/expectations/tests/virtual_dtor.rs index 0c4109776d..9df3c31c90 100644 --- a/tests/expectations/tests/virtual_dtor.rs +++ b/tests/expectations/tests/virtual_dtor.rs @@ -5,7 +5,6 @@ #[repr(C)] -#[derive(Default)] pub struct nsSlots__bindgen_vtable { } #[repr(C)] diff --git a/tests/expectations/tests/virtual_inheritance.rs b/tests/expectations/tests/virtual_inheritance.rs index 6896eb31c9..a09ad5672f 100644 --- a/tests/expectations/tests/virtual_inheritance.rs +++ b/tests/expectations/tests/virtual_inheritance.rs @@ -25,7 +25,6 @@ impl Clone for A { fn clone(&self) -> Self { *self } } #[repr(C)] -#[derive(Default)] pub struct B__bindgen_vtable { } #[repr(C)] @@ -53,7 +52,6 @@ impl Default for B { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] -#[derive(Default)] pub struct C__bindgen_vtable { } #[repr(C)] diff --git a/tests/expectations/tests/virtual_overloaded.rs b/tests/expectations/tests/virtual_overloaded.rs index e7ae9f4a73..b10d2adc9f 100644 --- a/tests/expectations/tests/virtual_overloaded.rs +++ b/tests/expectations/tests/virtual_overloaded.rs @@ -5,7 +5,6 @@ #[repr(C)] -#[derive(Default)] pub struct C__bindgen_vtable { } #[repr(C)] diff --git a/tests/expectations/tests/vtable_recursive_sig.rs b/tests/expectations/tests/vtable_recursive_sig.rs index 716ce39f5c..18e4f07ea1 100644 --- a/tests/expectations/tests/vtable_recursive_sig.rs +++ b/tests/expectations/tests/vtable_recursive_sig.rs @@ -23,7 +23,6 @@ impl Default for Derived { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] -#[derive(Default)] pub struct Base__bindgen_vtable { } #[repr(C)] From f67967a2930082ef54cde03d26dc3c0fe8f388cd Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 20 Mar 2017 18:35:13 +0100 Subject: [PATCH 2/2] Make vtables non-zero-size to fix a rustc warning. ``` warning: found non-foreign-function-safe member in struct marked #[repr(C)]: found zero-size struct in foreign module, consider adding a member to this struct ``` --- src/codegen/mod.rs | 4 +++- tests/expectations/tests/enum_and_vtable_mangling.rs | 3 +-- tests/expectations/tests/nested_vtable.rs | 3 +-- tests/expectations/tests/ref_argument_array.rs | 3 +-- tests/expectations/tests/virtual_dtor.rs | 3 +-- tests/expectations/tests/virtual_inheritance.rs | 6 ++---- tests/expectations/tests/virtual_overloaded.rs | 3 +-- tests/expectations/tests/vtable_recursive_sig.rs | 3 +-- 8 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 577b8aabfc..fb6c839d84 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -712,7 +712,9 @@ impl<'a> CodeGenerator for Vtable<'a> { .item() .pub_() .with_attrs(attributes) - .struct_(self.canonical_name(ctx)) + .tuple_struct(self.canonical_name(ctx)) + .field() + .build_ty(helpers::ast_ty::raw_type(ctx, "c_void")) .build(); result.push(vtable); } diff --git a/tests/expectations/tests/enum_and_vtable_mangling.rs b/tests/expectations/tests/enum_and_vtable_mangling.rs index f1a580931f..b34e217f4a 100644 --- a/tests/expectations/tests/enum_and_vtable_mangling.rs +++ b/tests/expectations/tests/enum_and_vtable_mangling.rs @@ -10,8 +10,7 @@ pub const whatever_else: _bindgen_ty_1 = _bindgen_ty_1::whatever_else; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum _bindgen_ty_1 { match_ = 0, whatever_else = 1, } #[repr(C)] -pub struct C__bindgen_vtable { -} +pub struct C__bindgen_vtable(::std::os::raw::c_void); #[repr(C)] #[derive(Debug, Copy)] pub struct C { diff --git a/tests/expectations/tests/nested_vtable.rs b/tests/expectations/tests/nested_vtable.rs index 62466ee013..4c9dc8f6c2 100644 --- a/tests/expectations/tests/nested_vtable.rs +++ b/tests/expectations/tests/nested_vtable.rs @@ -5,8 +5,7 @@ #[repr(C)] -pub struct nsISupports__bindgen_vtable { -} +pub struct nsISupports__bindgen_vtable(::std::os::raw::c_void); #[repr(C)] #[derive(Debug, Copy)] pub struct nsISupports { diff --git a/tests/expectations/tests/ref_argument_array.rs b/tests/expectations/tests/ref_argument_array.rs index ac761796f1..51531824cb 100644 --- a/tests/expectations/tests/ref_argument_array.rs +++ b/tests/expectations/tests/ref_argument_array.rs @@ -6,8 +6,7 @@ pub const NSID_LENGTH: ::std::os::raw::c_uint = 10; #[repr(C)] -pub struct nsID__bindgen_vtable { -} +pub struct nsID__bindgen_vtable(::std::os::raw::c_void); #[repr(C)] #[derive(Debug, Copy)] pub struct nsID { diff --git a/tests/expectations/tests/virtual_dtor.rs b/tests/expectations/tests/virtual_dtor.rs index 9df3c31c90..e5d3ace2fc 100644 --- a/tests/expectations/tests/virtual_dtor.rs +++ b/tests/expectations/tests/virtual_dtor.rs @@ -5,8 +5,7 @@ #[repr(C)] -pub struct nsSlots__bindgen_vtable { -} +pub struct nsSlots__bindgen_vtable(::std::os::raw::c_void); #[repr(C)] #[derive(Debug)] pub struct nsSlots { diff --git a/tests/expectations/tests/virtual_inheritance.rs b/tests/expectations/tests/virtual_inheritance.rs index a09ad5672f..b3119ca7ad 100644 --- a/tests/expectations/tests/virtual_inheritance.rs +++ b/tests/expectations/tests/virtual_inheritance.rs @@ -25,8 +25,7 @@ impl Clone for A { fn clone(&self) -> Self { *self } } #[repr(C)] -pub struct B__bindgen_vtable { -} +pub struct B__bindgen_vtable(::std::os::raw::c_void); #[repr(C)] #[derive(Debug, Copy)] pub struct B { @@ -52,8 +51,7 @@ impl Default for B { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] -pub struct C__bindgen_vtable { -} +pub struct C__bindgen_vtable(::std::os::raw::c_void); #[repr(C)] #[derive(Debug, Copy)] pub struct C { diff --git a/tests/expectations/tests/virtual_overloaded.rs b/tests/expectations/tests/virtual_overloaded.rs index b10d2adc9f..625abe3bde 100644 --- a/tests/expectations/tests/virtual_overloaded.rs +++ b/tests/expectations/tests/virtual_overloaded.rs @@ -5,8 +5,7 @@ #[repr(C)] -pub struct C__bindgen_vtable { -} +pub struct C__bindgen_vtable(::std::os::raw::c_void); #[repr(C)] #[derive(Debug, Copy)] pub struct C { diff --git a/tests/expectations/tests/vtable_recursive_sig.rs b/tests/expectations/tests/vtable_recursive_sig.rs index 18e4f07ea1..0faf37ac33 100644 --- a/tests/expectations/tests/vtable_recursive_sig.rs +++ b/tests/expectations/tests/vtable_recursive_sig.rs @@ -23,8 +23,7 @@ impl Default for Derived { fn default() -> Self { unsafe { ::std::mem::zeroed() } } } #[repr(C)] -pub struct Base__bindgen_vtable { -} +pub struct Base__bindgen_vtable(::std::os::raw::c_void); #[repr(C)] #[derive(Debug, Copy)] pub struct Base {