From 98a5b24ef7eb05ae52d63ce9e050d6ac144ba345 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 14 Aug 2018 20:09:07 +0200 Subject: [PATCH] test some more things around packed structs --- tests/run-pass/dst-struct.rs | 4 ++-- tests/run-pass/packed_struct.rs | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/tests/run-pass/dst-struct.rs b/tests/run-pass/dst-struct.rs index 932b571eccdb..0820614ab5c8 100644 --- a/tests/run-pass/dst-struct.rs +++ b/tests/run-pass/dst-struct.rs @@ -65,9 +65,9 @@ impl ToBar for Bar { pub fn main() { // With a vec of ints. - let f1 = Fat { f1: 5, f2: "some str", ptr: [1, 2, 3] }; + let f1 : Fat<[isize; 3]> = Fat { f1: 5, f2: "some str", ptr: [1, 2, 3] }; foo(&f1); - let f2 = &f1; + let f2 : &Fat<[isize; 3]> = &f1; foo(f2); let f3: &Fat<[isize]> = f2; foo(f3); diff --git a/tests/run-pass/packed_struct.rs b/tests/run-pass/packed_struct.rs index 7bd04c44438c..e10781e65605 100644 --- a/tests/run-pass/packed_struct.rs +++ b/tests/run-pass/packed_struct.rs @@ -45,6 +45,43 @@ fn test_unsizing() { let _unused = &arr_unaligned; // forcing an allocation, which could also yield "unaligned write"-errors } +fn test_drop() { + struct Wrap(u32); + impl Drop for Wrap { + fn drop(&mut self) { + // Do an (aligned) load + let _test = self.0; + // For the fun of it, test alignment + assert_eq!(&self.0 as *const _ as usize % std::mem::align_of::(), 0); + } + } + + #[repr(packed,C)] + struct Packed { + f1: u8, // this should move the second field to something not very aligned + f2: T, + } + + let p = Packed { f1: 42, f2: Wrap(23) }; + drop(p); +} + +fn test_inner_packed() { + // Even if just the inner struct is packed, accesses to the outer field can get unaligned. + // Make sure that works. + #[repr(packed)] + #[derive(Clone,Copy)] + struct Inner(u32); + + #[derive(Clone,Copy)] + struct Outer(u8, Inner); + + let o = Outer(0, Inner(42)); + let _x = o.1; + let _y = (o.1).0; + let _o2 = o.clone(); +} + fn main() { let mut x = S { a: 42, @@ -64,4 +101,6 @@ fn main() { test(Test2 { x: 0, other: &Test1 { x: 0, other: &42 }}); test_unsizing(); + test_drop(); + test_inner_packed(); }