about summary refs log tree commit diff
path: root/tests/ui/codegen
diff options
context:
space:
mode:
authorKivooeo <Kivooeo123@gmail.com>2025-06-29 17:39:08 +0500
committerKivooeo <Kivooeo123@gmail.com>2025-06-29 18:06:00 +0500
commita38c78c4616d6032af37739fad903e842a52e290 (patch)
tree36f857ea47ba0a396dc51c402086c25b66de17b3 /tests/ui/codegen
parent5ca574e85b67cec0a6fc3fddfe398cbe676c9c69 (diff)
downloadrust-a38c78c4616d6032af37739fad903e842a52e290.tar.gz
rust-a38c78c4616d6032af37739fad903e842a52e290.zip
moved tests
Diffstat (limited to 'tests/ui/codegen')
-rw-r--r--tests/ui/codegen/mono-respects-abi-alignment.rs35
-rw-r--r--tests/ui/codegen/msvc-opt-level-z-no-corruption.rs31
2 files changed, 66 insertions, 0 deletions
diff --git a/tests/ui/codegen/mono-respects-abi-alignment.rs b/tests/ui/codegen/mono-respects-abi-alignment.rs
new file mode 100644
index 00000000000..62df1aca357
--- /dev/null
+++ b/tests/ui/codegen/mono-respects-abi-alignment.rs
@@ -0,0 +1,35 @@
+//@ run-pass
+
+#![allow(non_upper_case_globals)]
+#![allow(dead_code)]
+/*!
+ * On x86_64-linux-gnu and possibly other platforms, structs get 8-byte "preferred" alignment,
+ * but their "ABI" alignment (i.e., what actually matters for data layout) is the largest alignment
+ * of any field. (Also, `u64` has 8-byte ABI alignment; this is not always true).
+ *
+ * On such platforms, if monomorphize uses the "preferred" alignment, then it will unify
+ * `A` and `B`, even though `S<A>` and `S<B>` have the field `t` at different offsets,
+ * and apply the wrong instance of the method `unwrap`.
+ */
+
+#[derive(Copy, Clone)]
+struct S<T> { i:u8, t:T }
+
+impl<T> S<T> {
+    fn unwrap(self) -> T {
+        self.t
+    }
+}
+
+#[derive(Copy, Clone, PartialEq, Debug)]
+struct A((u32, u32));
+
+#[derive(Copy, Clone, PartialEq, Debug)]
+struct B(u64);
+
+pub fn main() {
+    static Ca: S<A> = S { i: 0, t: A((13, 104)) };
+    static Cb: S<B> = S { i: 0, t: B(31337) };
+    assert_eq!(Ca.unwrap(), A((13, 104)));
+    assert_eq!(Cb.unwrap(), B(31337));
+}
diff --git a/tests/ui/codegen/msvc-opt-level-z-no-corruption.rs b/tests/ui/codegen/msvc-opt-level-z-no-corruption.rs
new file mode 100644
index 00000000000..c1be168a05d
--- /dev/null
+++ b/tests/ui/codegen/msvc-opt-level-z-no-corruption.rs
@@ -0,0 +1,31 @@
+// A previously outdated version of LLVM caused compilation failures on Windows
+// specifically with optimization level `z`. After the update to a more recent LLVM
+// version, this test checks that compilation and execution both succeed.
+// See https://github.com/rust-lang/rust/issues/45034
+
+//@ ignore-cross-compile
+// Reason: the compiled binary is executed
+//@ only-windows
+// Reason: the observed bug only occurs on Windows
+//@ run-pass
+//@ compile-flags: -C opt-level=z
+
+#![feature(test)]
+extern crate test;
+
+fn foo(x: i32, y: i32) -> i64 {
+    (x + y) as i64
+}
+
+#[inline(never)]
+fn bar() {
+    let _f = Box::new(0);
+    // This call used to trigger an LLVM bug in opt-level z where the base
+    // pointer gets corrupted, see issue #45034
+    let y: fn(i32, i32) -> i64 = test::black_box(foo);
+    test::black_box(y(1, 2));
+}
+
+fn main() {
+    bar();
+}