11// check-pass
2- #![ feature( rustc_attrs) ]
2+ #![ feature( rustc_attrs, transparent_unions ) ]
33#![ allow( unused, improper_ctypes_definitions) ]
4+ use std:: marker:: PhantomData ;
45use std:: num:: NonZeroI32 ;
56use std:: ptr:: NonNull ;
67
@@ -27,7 +28,7 @@ struct ReprC2Int<T>(i32, T);
2728#[ repr( C ) ]
2829struct ReprC2Float < T > ( f32 , T ) ;
2930#[ repr( C ) ]
30- struct ReprC4 < T > ( T , T , T , T ) ;
31+ struct ReprC4 < T > ( T , Vec < i32 > , Zst , T ) ;
3132#[ repr( C ) ]
3233struct ReprC4Mixed < T > ( T , f32 , i32 , T ) ;
3334#[ repr( C ) ]
@@ -73,6 +74,45 @@ test_abi_compatible!(zst_unit, Zst, ());
7374test_abi_compatible ! ( zst_array, Zst , [ u8 ; 0 ] ) ;
7475test_abi_compatible ! ( nonzero_int, NonZeroI32 , i32 ) ;
7576
77+ // `repr(transparent)` compatibility.
78+ #[ repr( transparent) ]
79+ struct Wrapper1 < T > ( T ) ;
80+ #[ repr( transparent) ]
81+ struct Wrapper2 < T > ( ( ) , Zst , T ) ;
82+ #[ repr( transparent) ]
83+ struct Wrapper3 < T > ( T , [ u8 ; 0 ] , PhantomData < u64 > ) ;
84+ #[ repr( transparent) ]
85+ union WrapperUnion < T : Copy > {
86+ nothing : ( ) ,
87+ something : T ,
88+ }
89+
90+ macro_rules! test_transparent {
91+ ( $name: ident, $t: ty) => {
92+ mod $name {
93+ use super :: * ;
94+ test_abi_compatible!( wrap1, $t, Wrapper1 <$t>) ;
95+ test_abi_compatible!( wrap2, $t, Wrapper2 <$t>) ;
96+ test_abi_compatible!( wrap3, $t, Wrapper3 <$t>) ;
97+ test_abi_compatible!( wrap4, $t, WrapperUnion <$t>) ;
98+ }
99+ } ;
100+ }
101+
102+ test_transparent ! ( simple, i32 ) ;
103+ test_transparent ! ( reference, & ' static i32 ) ;
104+ test_transparent ! ( zst, Zst ) ;
105+ test_transparent ! ( unit, ( ) ) ;
106+ test_transparent ! ( pair, ( i32 , f32 ) ) ; // mixing in some floats since they often get special treatment
107+ test_transparent ! ( triple, ( i8 , i16 , f32 ) ) ; // chosen to fit into 64bit
108+ test_transparent ! ( tuple, ( i32 , f32 , i64 , f64 ) ) ;
109+ test_transparent ! ( empty_array, [ u32 ; 0 ] ) ;
110+ test_transparent ! ( empty_1zst_array, [ u8 ; 0 ] ) ;
111+ test_transparent ! ( small_array, [ i32 ; 2 ] ) ; // chosen to fit into 64bit
112+ test_transparent ! ( large_array, [ i32 ; 16 ] ) ;
113+ test_transparent ! ( enum_, Option <i32 >) ;
114+ test_transparent ! ( enum_niched, Option <& ' static i32 >) ;
115+
76116// RFC 3391 <https://rust-lang.github.io/rfcs/3391-result_ffi_guarantees.html>.
77117macro_rules! test_nonnull {
78118 ( $name: ident, $t: ty) => {
0 commit comments