Skip to content

Commit 07eff4a

Browse files
committed
miri function ABI check: specifically look for repr(transparent)
1 parent 7225fbe commit 07eff4a

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(portable_simd)]
2+
3+
// Some targets treat arrays and structs very differently. We would probably catch that on those
4+
// targets since we check the `PassMode`; here we ensure that we catch it on *all* targets
5+
// (in particular, on x86-64 the pass mode is `Indirect` for both of these).
6+
struct S(i32, i32, i32, i32);
7+
type A = [i32; 4];
8+
9+
fn main() {
10+
fn f(_: S) {}
11+
12+
// These two types have the same size but are still not compatible.
13+
let g = unsafe { std::mem::transmute::<fn(S), fn(A)>(f) };
14+
15+
g(Default::default()) //~ ERROR: calling a function with argument of type S passing data of type [i32; 4]
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: calling a function with argument of type S passing data of type [i32; 4]
2+
--> $DIR/abi_mismatch_array_vs_struct.rs:LL:CC
3+
|
4+
LL | g(Default::default())
5+
| ^^^^^^^^^^^^^^^^^^^^^ calling a function with argument of type S passing data of type [i32; 4]
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/abi_mismatch_array_vs_struct.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to previous error
15+

tests/pass/function_calls/abi_compat.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ fn test_abi_newtype<T: Copy>(t: T) {
2424
#[repr(transparent)]
2525
struct Wrapper2<T>(T, ());
2626
#[repr(transparent)]
27+
struct Wrapper2a<T>((), T);
28+
#[repr(transparent)]
2729
struct Wrapper3<T>(T, [u8; 0]);
2830

2931
test_abi_compat(t, Wrapper1(t));
3032
test_abi_compat(t, Wrapper2(t, ()));
33+
test_abi_compat(t, Wrapper2a((), t));
3134
test_abi_compat(t, Wrapper3(t, []));
3235
}
3336

@@ -46,4 +49,5 @@ fn main() {
4649
test_abi_newtype(0f32);
4750
test_abi_newtype((0u32, 1u32, 2u32));
4851
test_abi_newtype([0u32, 1u32, 2u32]);
52+
test_abi_newtype([0i32; 0]);
4953
}

0 commit comments

Comments
 (0)