Skip to content

Commit deb708a

Browse files
committed
Auto merge of rust-lang#115678 - RalfJung:abi-compat-test, r=petrochenkov
test ABI compatibility for some unsized types as well and test for what `DispatchFromDyn` needs. Also I ran this on a whole bunch of targets via Miri and added enough `cfg` to make it all work, as documentation for what does and doesn't currently work. (Most of those targets do not have their tests run on CI anyway.) Here's the shell rune I used for that: ``` for TARGET in x86_64-unknown-linux-gnu x86_64-pc-windows-gnu aarch64-unknown-linux-gnu s390x-unknown-linux-gnu mips64-unknown-linux-gnuabi64 sparc64-unknown-linux-gnu powerpc64-unknown-linux-gnu powerpc64le-unknown-linux-gnu riscv64gc-unknown-linux-gnu loongarch64-unknown-linux-gnu wasm32-unknown-unknown; do BOOTSTRAP_SKIP_TARGET_SANITY=1 ./x.py run miri --stage 0 --args tests/ui/abi/compatibility.rs --target $TARGET; done ```
2 parents 6c03617 + 6f2c051 commit deb708a

File tree

1 file changed

+64
-14
lines changed

1 file changed

+64
-14
lines changed

tests/ui/abi/compatibility.rs

+64-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
// check-pass
2-
#![feature(rustc_attrs, transparent_unions)]
3-
#![allow(unused, improper_ctypes_definitions)]
2+
#![feature(rustc_attrs, unsized_fn_params, transparent_unions)]
3+
#![allow(unused, improper_ctypes_definitions, internal_features)]
44
use std::marker::PhantomData;
55
use std::mem::ManuallyDrop;
66
use std::num::NonZeroI32;
77
use std::ptr::NonNull;
88

9+
// FIXME: a bunch of targets are broken in various ways.
10+
// Hence there are `cfg` throughout this test to disable parts of it on those targets.
11+
// sparc64: https://github.com/rust-lang/rust/issues/115336
12+
// mips64: https://github.com/rust-lang/rust/issues/115404
13+
// riscv64: https://github.com/rust-lang/rust/issues/115481
14+
// loongarch64: https://github.com/rust-lang/rust/issues/115509
15+
916
macro_rules! assert_abi_compatible {
1017
($name:ident, $t1:ty, $t2:ty) => {
1118
mod $name {
1219
use super::*;
20+
// Declaring a `type` doesn't even check well-formedness, so we also declare a function.
21+
fn check_wf(_x: $t1, _y: $t2) {}
1322
// Test argument and return value, `Rust` and `C` ABIs.
1423
#[rustc_abi(assert_eq)]
1524
type TestRust = (fn($t1) -> $t1, fn($t2) -> $t2);
@@ -23,7 +32,7 @@ macro_rules! assert_abi_compatible {
2332
struct Zst;
2433

2534
#[repr(C)]
26-
struct ReprC1<T>(T);
35+
struct ReprC1<T: ?Sized>(T);
2736
#[repr(C)]
2837
struct ReprC2Int<T>(i32, T);
2938
#[repr(C)]
@@ -72,14 +81,20 @@ test_abi_compatible!(fn_fn, fn(), fn(i32) -> i32);
7281

7382
// Some further guarantees we will likely (have to) make.
7483
test_abi_compatible!(zst_unit, Zst, ());
84+
#[cfg(not(any(target_arch = "sparc64")))]
7585
test_abi_compatible!(zst_array, Zst, [u8; 0]);
7686
test_abi_compatible!(nonzero_int, NonZeroI32, i32);
7787

88+
// `DispatchFromDyn` relies on ABI compatibility.
89+
// This is interesting since these types are not `repr(transparent)`.
90+
test_abi_compatible!(rc, std::rc::Rc<i32>, *mut i32);
91+
test_abi_compatible!(arc, std::sync::Arc<i32>, *mut i32);
92+
7893
// `repr(transparent)` compatibility.
7994
#[repr(transparent)]
80-
struct Wrapper1<T>(T);
95+
struct Wrapper1<T: ?Sized>(T);
8196
#[repr(transparent)]
82-
struct Wrapper2<T>((), Zst, T);
97+
struct Wrapper2<T: ?Sized>((), Zst, T);
8398
#[repr(transparent)]
8499
struct Wrapper3<T>(T, [u8; 0], PhantomData<u64>);
85100
#[repr(transparent)]
@@ -95,6 +110,7 @@ macro_rules! test_transparent {
95110
test_abi_compatible!(wrap1, $t, Wrapper1<$t>);
96111
test_abi_compatible!(wrap2, $t, Wrapper2<$t>);
97112
test_abi_compatible!(wrap3, $t, Wrapper3<$t>);
113+
#[cfg(not(any(target_arch = "riscv64", target_arch = "loongarch64")))]
98114
test_abi_compatible!(wrap4, $t, WrapperUnion<$t>);
99115
}
100116
};
@@ -104,17 +120,51 @@ test_transparent!(simple, i32);
104120
test_transparent!(reference, &'static i32);
105121
test_transparent!(zst, Zst);
106122
test_transparent!(unit, ());
107-
test_transparent!(pair, (i32, f32)); // mixing in some floats since they often get special treatment
108-
test_transparent!(triple, (i8, i16, f32)); // chosen to fit into 64bit
109-
test_transparent!(triple_f32, (f32, f32, f32)); // homogeneous case
110-
test_transparent!(triple_f64, (f64, f64, f64));
111-
test_transparent!(tuple, (i32, f32, i64, f64));
112-
test_transparent!(empty_array, [u32; 0]);
113-
test_transparent!(empty_1zst_array, [u8; 0]);
114-
test_transparent!(small_array, [i32; 2]); // chosen to fit into 64bit
115-
test_transparent!(large_array, [i32; 16]);
116123
test_transparent!(enum_, Option<i32>);
117124
test_transparent!(enum_niched, Option<&'static i32>);
125+
#[cfg(not(any(target_arch = "mips64", target_arch = "sparc64")))]
126+
mod tuples {
127+
use super::*;
128+
// mixing in some floats since they often get special treatment
129+
test_transparent!(pair, (i32, f32));
130+
// chosen to fit into 64bit
131+
test_transparent!(triple, (i8, i16, f32));
132+
// Pure-float types that are not ScalarPair seem to be tricky.
133+
test_transparent!(triple_f32, (f32, f32, f32));
134+
test_transparent!(triple_f64, (f64, f64, f64));
135+
// and also something that's larger than 2 pointers
136+
test_transparent!(tuple, (i32, f32, i64, f64));
137+
}
138+
// Some targets have special rules for arrays.
139+
#[cfg(not(any(target_arch = "mips64", target_arch = "sparc64")))]
140+
mod arrays {
141+
use super::*;
142+
test_transparent!(empty_array, [u32; 0]);
143+
test_transparent!(empty_1zst_array, [u8; 0]);
144+
test_transparent!(small_array, [i32; 2]); // chosen to fit into 64bit
145+
test_transparent!(large_array, [i32; 16]);
146+
}
147+
148+
// Some tests with unsized types (not all wrappers are compatible with that).
149+
macro_rules! test_transparent_unsized {
150+
($name:ident, $t:ty) => {
151+
mod $name {
152+
use super::*;
153+
assert_abi_compatible!(wrap1, $t, Wrapper1<$t>);
154+
assert_abi_compatible!(wrap1_reprc, ReprC1<$t>, ReprC1<Wrapper1<$t>>);
155+
assert_abi_compatible!(wrap2, $t, Wrapper2<$t>);
156+
assert_abi_compatible!(wrap2_reprc, ReprC1<$t>, ReprC1<Wrapper2<$t>>);
157+
}
158+
};
159+
}
160+
161+
#[cfg(not(any(target_arch = "mips64", target_arch = "sparc64")))]
162+
mod unsized_ {
163+
use super::*;
164+
test_transparent_unsized!(str_, str);
165+
test_transparent_unsized!(slice, [u8]);
166+
test_transparent_unsized!(dyn_trait, dyn std::any::Any);
167+
}
118168

119169
// RFC 3391 <https://rust-lang.github.io/rfcs/3391-result_ffi_guarantees.html>.
120170
macro_rules! test_nonnull {

0 commit comments

Comments
 (0)