]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #40373 - TimNN:test-ub-packed, r=arielb1
authorAriel Ben-Yehuda <arielb1@mail.tau.ac.il>
Sat, 11 Mar 2017 19:57:46 +0000 (21:57 +0200)
committerGitHub <noreply@github.com>
Sat, 11 Mar 2017 19:57:46 +0000 (21:57 +0200)
Fix UB in repr(packed) tests

r? @arielb1

cc #37609 and #27060

src/test/run-make/extern-fn-with-packed-struct/test.rs
src/test/run-pass/packed-struct-vec.rs

index c0f55893a3abe4370e0a779081591bd56a3cdc71..9e81636e36703c4372203d87202027456c7496db 100644 (file)
@@ -8,14 +8,36 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::fmt;
+
 #[repr(packed)]
-#[derive(Copy, Clone, PartialEq, Debug)]
+#[derive(Copy, Clone)]
 struct Foo {
     a: i8,
     b: i16,
     c: i8
 }
 
+impl PartialEq for Foo {
+    fn eq(&self, other: &Foo) -> bool {
+        self.a == other.a && self.b == other.b && self.c == other.c
+    }
+}
+
+impl fmt::Debug for Foo {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        let a = self.a;
+        let b = self.b;
+        let c = self.c;
+
+        f.debug_struct("Foo")
+            .field("a", &a)
+            .field("b", &b)
+            .field("c", &c)
+            .finish()
+    }
+}
+
 #[link(name = "test", kind = "static")]
 extern {
     fn foo(f: Foo) -> Foo;
index 4b32b881be73868eed9442a63fb2805c5ac5fa32..57407b8422371be2152d5d30207e1c78a17f6c8d 100644 (file)
@@ -8,15 +8,34 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::fmt;
 use std::mem;
 
 #[repr(packed)]
-#[derive(Copy, Clone, PartialEq, Debug)]
+#[derive(Copy, Clone)]
 struct Foo {
     bar: u8,
     baz: u64
 }
 
+impl PartialEq for Foo {
+    fn eq(&self, other: &Foo) -> bool {
+        self.bar == other.bar && self.baz == other.baz
+    }
+}
+
+impl fmt::Debug for Foo {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        let bar = self.bar;
+        let baz = self.baz;
+
+        f.debug_struct("Foo")
+            .field("bar", &bar)
+            .field("baz", &baz)
+            .finish()
+    }
+}
+
 pub fn main() {
     let foos = [Foo { bar: 1, baz: 2 }; 10];