summaryrefslogtreecommitdiffstats
path: root/tests/ui/packed/packed-struct-size.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/packed/packed-struct-size.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/packed/packed-struct-size.rs')
-rw-r--r--tests/ui/packed/packed-struct-size.rs157
1 files changed, 157 insertions, 0 deletions
diff --git a/tests/ui/packed/packed-struct-size.rs b/tests/ui/packed/packed-struct-size.rs
new file mode 100644
index 000000000..c832c7cfa
--- /dev/null
+++ b/tests/ui/packed/packed-struct-size.rs
@@ -0,0 +1,157 @@
+// run-pass
+#![allow(dead_code)]
+#![allow(non_camel_case_types)]
+#![allow(non_upper_case_globals)]
+
+use std::mem;
+
+#[repr(packed)]
+struct P1S4 {
+ a: u8,
+ b: [u8; 3],
+}
+
+#[repr(packed(2))]
+struct P2S4 {
+ a: u8,
+ b: [u8; 3],
+}
+
+#[repr(packed)]
+struct P1S5 {
+ a: u8,
+ b: u32
+}
+
+#[repr(packed(2))]
+struct P2S2 {
+ a: u8,
+ b: u8
+}
+
+#[repr(packed(2))]
+struct P2S6 {
+ a: u8,
+ b: u32
+}
+
+#[repr(packed(2))]
+struct P2S12 {
+ a: u32,
+ b: u64
+}
+
+#[repr(packed)]
+struct P1S13 {
+ a: i64,
+ b: f32,
+ c: u8,
+}
+
+#[repr(packed(2))]
+struct P2S14 {
+ a: i64,
+ b: f32,
+ c: u8,
+}
+
+#[repr(packed(4))]
+struct P4S16 {
+ a: u8,
+ b: f32,
+ c: i64,
+ d: u16,
+}
+
+#[repr(C, packed(4))]
+struct P4CS20 {
+ a: u8,
+ b: f32,
+ c: i64,
+ d: u16,
+}
+
+enum Foo {
+ Bar = 1,
+ Baz = 2
+}
+
+#[repr(packed)]
+struct P1S3_Foo {
+ a: u8,
+ b: u16,
+ c: Foo
+}
+
+#[repr(packed(2))]
+struct P2_Foo {
+ a: Foo,
+}
+
+#[repr(packed(2))]
+struct P2S3_Foo {
+ a: u8,
+ b: u16,
+ c: Foo
+}
+
+#[repr(packed)]
+struct P1S7_Option {
+ a: f32,
+ b: u8,
+ c: u16,
+ d: Option<Box<f64>>
+}
+
+#[repr(packed(2))]
+struct P2_Option {
+ a: Option<Box<f64>>
+}
+
+#[repr(packed(2))]
+struct P2S7_Option {
+ a: f32,
+ b: u8,
+ c: u16,
+ d: Option<Box<f64>>
+}
+
+// Placing packed structs in statics should work
+static TEST_P1S4: P1S4 = P1S4 { a: 1, b: [2, 3, 4] };
+static TEST_P1S5: P1S5 = P1S5 { a: 3, b: 67 };
+static TEST_P1S3_Foo: P1S3_Foo = P1S3_Foo { a: 1, b: 2, c: Foo::Baz };
+static TEST_P2S2: P2S2 = P2S2 { a: 1, b: 2 };
+static TEST_P2S4: P2S4 = P2S4 { a: 1, b: [2, 3, 4] };
+static TEST_P2S6: P2S6 = P2S6 { a: 1, b: 2 };
+static TEST_P2S12: P2S12 = P2S12 { a: 1, b: 2 };
+static TEST_P4S16: P4S16 = P4S16 { a: 1, b: 2.0, c: 3, d: 4 };
+static TEST_P4CS20: P4CS20 = P4CS20 { a: 1, b: 2.0, c: 3, d: 4 };
+
+fn align_to(value: usize, align: usize) -> usize {
+ (value + (align - 1)) & !(align - 1)
+}
+
+macro_rules! check {
+ ($t:ty, $align:expr, $size:expr) => ({
+ assert_eq!(mem::align_of::<$t>(), $align);
+ assert_eq!(mem::size_of::<$t>(), $size);
+ });
+}
+
+pub fn main() {
+ check!(P1S4, 1, 4);
+ check!(P1S5, 1, 5);
+ check!(P1S13, 1, 13);
+ check!(P1S3_Foo, 1, 3 + mem::size_of::<Foo>());
+ check!(P1S7_Option, 1, 7 + mem::size_of::<Option<Box<f64>>>());
+
+ check!(P2S2, 1, 2);
+ check!(P2S4, 1, 4);
+ check!(P2S6, 2, 6);
+ check!(P2S12, 2, 12);
+ check!(P2S14, 2, 14);
+ check!(P4S16, 4, 16);
+ check!(P4CS20, 4, 20);
+ check!(P2S3_Foo, 2, align_to(3 + mem::size_of::<P2_Foo>(), 2));
+ check!(P2S7_Option, 2, align_to(7 + mem::size_of::<P2_Option>(), 2));
+}