about summary refs log tree commit diff
path: root/tests/ui/structs-enums
diff options
context:
space:
mode:
authorThe 8472 <git@infinite-source.de>2023-04-22 19:24:39 +0200
committerThe 8472 <git@infinite-source.de>2023-04-28 23:08:54 +0200
commit61fb5a91b794d7ab9c5f923b26c92cfc473b976b (patch)
treefab8cdc1c35362e85b38171b32ed6133e7e41d6f /tests/ui/structs-enums
parentafe106cdc8b5dbcbeedb292b87dc7d7ae58964f1 (diff)
downloadrust-61fb5a91b794d7ab9c5f923b26c92cfc473b976b.tar.gz
rust-61fb5a91b794d7ab9c5f923b26c92cfc473b976b.zip
layout-alignment-promotion logic should depend on the niche-bias
For start-biased layout we want to avoid overpromoting so that
the niche doesn't get pushed back.
For end-biased layout we want to avoid promoting fields that
may contain one of the niches of interest.
Diffstat (limited to 'tests/ui/structs-enums')
-rw-r--r--tests/ui/structs-enums/type-sizes.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/ui/structs-enums/type-sizes.rs b/tests/ui/structs-enums/type-sizes.rs
index e5c6857a26a..406e5c8441e 100644
--- a/tests/ui/structs-enums/type-sizes.rs
+++ b/tests/ui/structs-enums/type-sizes.rs
@@ -198,6 +198,18 @@ struct ReorderWithNiche {
     ary: [u8; 8]
 }
 
+#[repr(C)]
+struct EndNiche8([u8; 7], bool);
+
+#[repr(C)]
+struct MiddleNiche4(u8, u8, bool, u8);
+
+struct ReorderEndNiche {
+    a: EndNiche8,
+    b: MiddleNiche4,
+}
+
+
 // standins for std types which we want to be laid out in a reasonable way
 struct RawVecDummy {
     ptr: NonNull<u8>,
@@ -316,4 +328,11 @@ pub fn main() {
             "here [u8; 8] should group with _at least_ align-4 fields");
     assert_eq!(ptr::from_ref(&v), ptr::from_ref(&v.b).cast(),
                "sort niches to the front where possible");
+
+    // Neither field has a niche at the beginning so the layout algorithm should try move niches to
+    // the end which means the 8-sized field shouldn't be alignment-promoted before the 4-sized one.
+    let v = ReorderEndNiche { a: EndNiche8([0; 7], false), b: MiddleNiche4(0, 0, false, 0) };
+    assert!(ptr::from_ref(&v.a).addr() > ptr::from_ref(&v.b).addr());
+
+
 }