about summary refs log tree commit diff
path: root/tests/ui/layout
diff options
context:
space:
mode:
authorThe 8472 <git@infinite-source.de>2024-11-16 01:55:07 +0100
committerThe 8472 <git@infinite-source.de>2025-01-10 02:22:57 +0100
commita75617c223ca4476625c20565a3e1ff51d384102 (patch)
treebace5ede4c60dacc6e001703926119c8e71282b5 /tests/ui/layout
parenta580b5c379b4fca50dfe5afc0fc0ce00921e4e00 (diff)
downloadrust-a75617c223ca4476625c20565a3e1ff51d384102.tar.gz
rust-a75617c223ca4476625c20565a3e1ff51d384102.zip
Foo<T> != Foo<U> under layout randomization
previously field ordering was using the same seed for all instances of Foo,
now we pass seed values through the layout tree so that not only
the struct itself affects layout but also its fields
Diffstat (limited to 'tests/ui/layout')
-rw-r--r--tests/ui/layout/randomize.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/ui/layout/randomize.rs b/tests/ui/layout/randomize.rs
new file mode 100644
index 00000000000..be895252a1b
--- /dev/null
+++ b/tests/ui/layout/randomize.rs
@@ -0,0 +1,29 @@
+//@ build-pass
+//@ revisions: normal randomize-layout
+//@ [randomize-layout]compile-flags: -Zrandomize-layout
+
+#![crate_type = "lib"]
+
+struct Foo<T>(u32, T, u8);
+
+struct Wrapper<T>(T);
+
+#[repr(transparent)]
+struct TransparentWrapper(u16);
+
+const _: () = {
+    // behavior of the current implementation, not guaranteed
+    #[cfg(not(randomize_layout))]
+    assert!(std::mem::offset_of!(Foo::<u16>, 1) == std::mem::offset_of!(Foo::<Wrapper<u16>>, 1));
+
+    // under randomization Foo<T> != Foo<U>
+    #[cfg(randomize_layout)]
+    assert!(std::mem::offset_of!(Foo::<u16>, 1) != std::mem::offset_of!(Foo::<Wrapper<u16>>, 1));
+
+    // but repr(transparent) should make them the same again.
+    // maybe not strictly guaranteed? but UCG has been leaning in that direction at least
+    #[cfg(randomize_layout)]
+    assert!(
+        std::mem::offset_of!(Foo::<u16>, 1) == std::mem::offset_of!(Foo::<TransparentWrapper>, 1)
+    );
+};