summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Fitzgerald <fitzgen@gmail.com>2017-09-06 15:17:07 -0700
committerNick Fitzgerald <fitzgen@gmail.com>2017-09-06 15:17:07 -0700
commit74e2aeab89cec911ccaecdc349c27e896f40f8f7 (patch)
tree2fd879a5393d6113dc6397553eaefefc19ceea77
parent641337dd9eaa068314220373568ded01e18b0871 (diff)
Do not derive things on opaque unions
Unions don't support deriving these things, even if there is only one variant (the opaque layout field).
-rw-r--r--src/ir/analysis/derive_debug.rs4
-rw-r--r--src/ir/analysis/derive_default.rs4
-rw-r--r--src/ir/analysis/derive_hash.rs4
-rw-r--r--src/ir/analysis/derive_partial_eq.rs4
-rw-r--r--src/ir/ty.rs8
-rw-r--r--tests/expectations/tests/doggo-or-null.rs65
-rw-r--r--tests/headers/doggo-or-null.hpp20
7 files changed, 105 insertions, 4 deletions
diff --git a/src/ir/analysis/derive_debug.rs b/src/ir/analysis/derive_debug.rs
index 76b889df..b7a35132 100644
--- a/src/ir/analysis/derive_debug.rs
+++ b/src/ir/analysis/derive_debug.rs
@@ -149,7 +149,9 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDebug<'ctx, 'gen> {
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
l.opaque().can_trivially_derive_debug()
});
- return if layout_can_derive {
+ return if layout_can_derive &&
+ !(ty.is_union() &&
+ self.ctx.options().rust_features().untagged_union()) {
trace!(" we can trivially derive Debug for the layout");
ConstrainResult::Same
} else {
diff --git a/src/ir/analysis/derive_default.rs b/src/ir/analysis/derive_default.rs
index 34c34cfe..19f4a842 100644
--- a/src/ir/analysis/derive_default.rs
+++ b/src/ir/analysis/derive_default.rs
@@ -176,7 +176,9 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveDefault<'ctx, 'gen> {
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
l.opaque().can_trivially_derive_default()
});
- return if layout_can_derive {
+ return if layout_can_derive &&
+ !(ty.is_union() &&
+ self.ctx.options().rust_features().untagged_union()) {
trace!(" we can trivially derive Default for the layout");
ConstrainResult::Same
} else {
diff --git a/src/ir/analysis/derive_hash.rs b/src/ir/analysis/derive_hash.rs
index d0ab3762..ee9cb23a 100644
--- a/src/ir/analysis/derive_hash.rs
+++ b/src/ir/analysis/derive_hash.rs
@@ -133,7 +133,9 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDeriveHash<'ctx, 'gen> {
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
l.opaque().can_trivially_derive_hash()
});
- return if layout_can_derive {
+ return if layout_can_derive &&
+ !(ty.is_union() &&
+ self.ctx.options().rust_features().untagged_union()) {
trace!(" we can trivially derive Hash for the layout");
ConstrainResult::Same
} else {
diff --git a/src/ir/analysis/derive_partial_eq.rs b/src/ir/analysis/derive_partial_eq.rs
index 030c4c48..45ba67a5 100644
--- a/src/ir/analysis/derive_partial_eq.rs
+++ b/src/ir/analysis/derive_partial_eq.rs
@@ -136,7 +136,9 @@ impl<'ctx, 'gen> MonotoneFramework for CannotDerivePartialEq<'ctx, 'gen> {
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
l.opaque().can_trivially_derive_partialeq()
});
- return if layout_can_derive {
+ return if layout_can_derive &&
+ !(ty.is_union() &&
+ self.ctx.options().rust_features().untagged_union()) {
trace!(" we can trivially derive PartialEq for the layout");
ConstrainResult::Same
} else {
diff --git a/src/ir/ty.rs b/src/ir/ty.rs
index da1cc6d7..b588dbb4 100644
--- a/src/ir/ty.rs
+++ b/src/ir/ty.rs
@@ -99,6 +99,14 @@ impl Type {
}
}
+ /// Is this a union?
+ pub fn is_union(&self) -> bool {
+ match self.kind {
+ TypeKind::Comp(ref comp) => comp.is_union(),
+ _ => false,
+ }
+ }
+
/// Is this type of kind `TypeKind::TypeParam`?
pub fn is_type_param(&self) -> bool {
match self.kind {
diff --git a/tests/expectations/tests/doggo-or-null.rs b/tests/expectations/tests/doggo-or-null.rs
new file mode 100644
index 00000000..292533d9
--- /dev/null
+++ b/tests/expectations/tests/doggo-or-null.rs
@@ -0,0 +1,65 @@
+/* automatically generated by rust-bindgen */
+
+
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+
+
+#[repr(C)]
+#[derive(Debug, Default, Copy, Hash, PartialEq)]
+pub struct Doggo {
+ pub x: ::std::os::raw::c_int,
+}
+#[test]
+fn bindgen_test_layout_Doggo() {
+ assert_eq!(::std::mem::size_of::<Doggo>() , 4usize , concat ! (
+ "Size of: " , stringify ! ( Doggo ) ));
+ assert_eq! (::std::mem::align_of::<Doggo>() , 4usize , concat ! (
+ "Alignment of " , stringify ! ( Doggo ) ));
+ assert_eq! (unsafe {
+ & ( * ( 0 as * const Doggo ) ) . x as * const _ as usize } ,
+ 0usize , concat ! (
+ "Alignment of field: " , stringify ! ( Doggo ) , "::" ,
+ stringify ! ( x ) ));
+}
+impl Clone for Doggo {
+ fn clone(&self) -> Self { *self }
+}
+#[repr(C)]
+#[derive(Debug, Default, Copy, Hash, PartialEq)]
+pub struct Null {
+ pub _address: u8,
+}
+#[test]
+fn bindgen_test_layout_Null() {
+ assert_eq!(::std::mem::size_of::<Null>() , 1usize , concat ! (
+ "Size of: " , stringify ! ( Null ) ));
+ assert_eq! (::std::mem::align_of::<Null>() , 1usize , concat ! (
+ "Alignment of " , stringify ! ( Null ) ));
+}
+impl Clone for Null {
+ fn clone(&self) -> Self { *self }
+}
+/// This type is an opaque union. Unions can't derive anything interesting like
+/// Debug or Default, even if their layout can, because it would require knowing
+/// which variant is in use. Opaque unions still end up as a `union` in the Rust
+/// bindings, but they just have one variant. Even so, can't derive. We should
+/// probably emit an opaque struct for opaque unions... but until then, we have
+/// this test to make sure that opaque unions don't derive and still compile.
+#[repr(C)]
+#[derive(Copy)]
+pub union DoggoOrNull {
+ pub _bindgen_opaque_blob: u32,
+}
+#[test]
+fn bindgen_test_layout_DoggoOrNull() {
+ assert_eq!(::std::mem::size_of::<DoggoOrNull>() , 4usize , concat ! (
+ "Size of: " , stringify ! ( DoggoOrNull ) ));
+ assert_eq! (::std::mem::align_of::<DoggoOrNull>() , 4usize , concat ! (
+ "Alignment of " , stringify ! ( DoggoOrNull ) ));
+}
+impl Clone for DoggoOrNull {
+ fn clone(&self) -> Self { *self }
+}
+impl Default for DoggoOrNull {
+ fn default() -> Self { unsafe { ::std::mem::zeroed() } }
+}
diff --git a/tests/headers/doggo-or-null.hpp b/tests/headers/doggo-or-null.hpp
new file mode 100644
index 00000000..9146e189
--- /dev/null
+++ b/tests/headers/doggo-or-null.hpp
@@ -0,0 +1,20 @@
+// bindgen-flags: --opaque-type DoggoOrNull --with-derive-partialeq --with-derive-hash -- -std=c++14
+
+class Doggo {
+ int x;
+};
+
+class Null {};
+
+/**
+ * This type is an opaque union. Unions can't derive anything interesting like
+ * Debug or Default, even if their layout can, because it would require knowing
+ * which variant is in use. Opaque unions still end up as a `union` in the Rust
+ * bindings, but they just have one variant. Even so, can't derive. We should
+ * probably emit an opaque struct for opaque unions... but until then, we have
+ * this test to make sure that opaque unions don't derive and still compile.
+ */
+union DoggoOrNull {
+ Doggo doggo;
+ Null none;
+};