summaryrefslogtreecommitdiffstats
path: root/tests/ui/structs-enums/enum-layout-optimization.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/structs-enums/enum-layout-optimization.rs
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/structs-enums/enum-layout-optimization.rs')
-rw-r--r--tests/ui/structs-enums/enum-layout-optimization.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/ui/structs-enums/enum-layout-optimization.rs b/tests/ui/structs-enums/enum-layout-optimization.rs
new file mode 100644
index 000000000..05d297906
--- /dev/null
+++ b/tests/ui/structs-enums/enum-layout-optimization.rs
@@ -0,0 +1,50 @@
+// run-pass
+// Test that we will do various size optimizations to enum layout, but
+// *not* if `#[repr(u8)]` or `#[repr(C)]` is passed. See also #40029.
+
+#![allow(dead_code)]
+
+use std::mem;
+
+enum Nullable<T> {
+ Alive(T),
+ Dropped,
+}
+
+#[repr(u8)]
+enum NullableU8<T> {
+ Alive(T),
+ Dropped,
+}
+
+#[repr(C)]
+enum NullableC<T> {
+ Alive(T),
+ Dropped,
+}
+
+struct StructNewtype<T>(T);
+
+#[repr(C)]
+struct StructNewtypeC<T>(T);
+
+enum EnumNewtype<T> { Variant(T) }
+
+#[repr(u8)]
+enum EnumNewtypeU8<T> { Variant(T) }
+
+#[repr(C)]
+enum EnumNewtypeC<T> { Variant(T) }
+
+fn main() {
+ assert!(mem::size_of::<Box<i32>>() == mem::size_of::<Nullable<Box<i32>>>());
+ assert!(mem::size_of::<Box<i32>>() < mem::size_of::<NullableU8<Box<i32>>>());
+ assert!(mem::size_of::<Box<i32>>() < mem::size_of::<NullableC<Box<i32>>>());
+
+ assert!(mem::size_of::<i32>() == mem::size_of::<StructNewtype<i32>>());
+ assert!(mem::size_of::<i32>() == mem::size_of::<StructNewtypeC<i32>>());
+
+ assert!(mem::size_of::<i32>() == mem::size_of::<EnumNewtype<i32>>());
+ assert!(mem::size_of::<i32>() < mem::size_of::<EnumNewtypeU8<i32>>());
+ assert!(mem::size_of::<i32>() < mem::size_of::<EnumNewtypeC<i32>>());
+}