From 0163768e0d98ea41432f4c144d900e4865c5be39 Mon Sep 17 00:00:00 2001 From: WANG Rui Date: Mon, 11 Sep 2023 16:05:49 +0800 Subject: [PATCH 1/2] rustc_target/loongarch: Fix passing of transparent unions with only one non-ZST member This ensures that `MaybeUninit` has the same ABI as `T` when passed through an `extern "C"` function. Fixes https://github.com/rust-lang/rust/issues/115509 --- compiler/rustc_target/src/abi/call/loongarch.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/compiler/rustc_target/src/abi/call/loongarch.rs b/compiler/rustc_target/src/abi/call/loongarch.rs index 247256f076b..e649d58bbca 100644 --- a/compiler/rustc_target/src/abi/call/loongarch.rs +++ b/compiler/rustc_target/src/abi/call/loongarch.rs @@ -83,6 +83,17 @@ where } FieldsShape::Union(_) => { if !arg_layout.is_zst() { + if arg_layout.is_transparent() { + let non_1zst_elem = arg_layout.non_1zst_field(cx).expect("not exactly one non-1-ZST field in non-ZST repr(transparent) union").1; + return should_use_fp_conv_helper( + cx, + &non_1zst_elem, + xlen, + flen, + field1_kind, + field2_kind, + ); + } return Err(CannotUseFpConv); } } From 10d55c3e03a833ea5cf37eea283d631505bccad6 Mon Sep 17 00:00:00 2001 From: WANG Rui Date: Wed, 20 Sep 2023 09:24:22 +0800 Subject: [PATCH 2/2] tests/ui/abi: Enable repr(transparent) union ABI tests on LoongArch64 --- tests/ui/abi/compatibility.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/ui/abi/compatibility.rs b/tests/ui/abi/compatibility.rs index d4f42cdda97..249e8176283 100644 --- a/tests/ui/abi/compatibility.rs +++ b/tests/ui/abi/compatibility.rs @@ -10,7 +10,6 @@ use std::ptr::NonNull; // Hence there are `cfg` throughout this test to disable parts of it on those targets. // sparc64: https://github.com/rust-lang/rust/issues/115336 // mips64: https://github.com/rust-lang/rust/issues/115404 -// loongarch64: https://github.com/rust-lang/rust/issues/115509 macro_rules! assert_abi_compatible { ($name:ident, $t1:ty, $t2:ty) => { @@ -109,7 +108,6 @@ macro_rules! test_transparent { test_abi_compatible!(wrap1, $t, Wrapper1<$t>); test_abi_compatible!(wrap2, $t, Wrapper2<$t>); test_abi_compatible!(wrap3, $t, Wrapper3<$t>); - #[cfg(not(target_arch = "loongarch64"))] test_abi_compatible!(wrap4, $t, WrapperUnion<$t>); } };