11// check-pass
2+ // revisions: host
3+ // revisions: arm
4+ //[arm] compile-flags: --target arm-unknown-linux-gnueabi
5+ //[arm] needs-llvm-components: arm
6+ // revisions: aarch64
7+ //[aarch64] compile-flags: --target aarch64-unknown-linux-gnu
8+ //[aarch64] needs-llvm-components: aarch64
9+ // revisions: s390x
10+ //[s390x] compile-flags: --target s390x-unknown-linux-gnu
11+ //[s390x] needs-llvm-components: s390x
12+ // revisions: mips
13+ //[mips] compile-flags: --target mips-unknown-linux-gnu
14+ //[mips] needs-llvm-components: mips
15+ // revisions: mips64
16+ //[mips64] compile-flags: --target mips64-unknown-linux-gnuabi64
17+ //[mips64] needs-llvm-components: mips
18+ // revisions: sparc
19+ //[sparc] compile-flags: --target sparc-unknown-linux-gnu
20+ //[sparc] needs-llvm-components: sparc
21+ // revisions: sparc64
22+ //[sparc64] compile-flags: --target sparc64-unknown-linux-gnu
23+ //[sparc64] needs-llvm-components: sparc
24+ // revisions: powerpc64
25+ //[powerpc64] compile-flags: --target powerpc64-unknown-linux-gnu
26+ //[powerpc64] needs-llvm-components: powerpc
27+ // revisions: riscv
28+ //[riscv] compile-flags: --target riscv64gc-unknown-linux-gnu
29+ //[riscv] needs-llvm-components: riscv
30+ // revisions: loongarch64
31+ //[loongarch64] compile-flags: --target loongarch64-unknown-linux-gnu
32+ //[loongarch64] needs-llvm-components: loongarch
33+ // revisions: wasm
34+ //[wasm] compile-flags: --target wasm32-unknown-unknown
35+ //[wasm] needs-llvm-components: webassembly
36+ // revisions: wasi
37+ //[wasi] compile-flags: --target wasm32-wasi
38+ //[wasi] needs-llvm-components: webassembly
39+ // revisions: nvptx64
40+ //[nvptx64] compile-flags: --target nvptx64-nvidia-cuda
41+ //[nvptx64] needs-llvm-components: nvptx
242#![ feature( rustc_attrs, unsized_fn_params, transparent_unions) ]
43+ #![ cfg_attr( not( host) , feature( no_core, lang_items) , no_std, no_core) ]
344#![ allow( unused, improper_ctypes_definitions, internal_features) ]
4- use std:: marker:: PhantomData ;
5- use std:: mem:: ManuallyDrop ;
6- use std:: num:: NonZeroI32 ;
7- use std:: ptr:: NonNull ;
845
9- // FIXME: a bunch of targets are broken in various ways.
46+ // FIXME: some targets are broken in various ways.
1047// Hence there are `cfg` throughout this test to disable parts of it on those targets.
1148// sparc64: https://github.com/rust-lang/rust/issues/115336
1249// mips64: https://github.com/rust-lang/rust/issues/115404
1350
51+ #[ cfg( host) ]
52+ use std:: {
53+ any:: Any , marker:: PhantomData , mem:: ManuallyDrop , num:: NonZeroI32 , ptr:: NonNull , rc:: Rc ,
54+ sync:: Arc ,
55+ } ;
56+
57+ /// To work cross-target this test must be no_core.
58+ /// This little prelude supplies what we need.
59+ #[ cfg( not( host) ) ]
60+ mod prelude {
61+ #[ lang = "sized" ]
62+ pub trait Sized { }
63+
64+ #[ lang = "receiver" ]
65+ pub trait Receiver { }
66+ impl < T : ?Sized > Receiver for & T { }
67+ impl < T : ?Sized > Receiver for & mut T { }
68+
69+ #[ lang = "copy" ]
70+ pub trait Copy : Sized { }
71+ impl Copy for i32 { }
72+ impl Copy for f32 { }
73+ impl < T : ?Sized > Copy for & T { }
74+ impl < T : ?Sized > Copy for * const T { }
75+ impl < T : ?Sized > Copy for * mut T { }
76+
77+ #[ lang = "clone" ]
78+ pub trait Clone : Sized {
79+ fn clone ( & self ) -> Self ;
80+ }
81+
82+ #[ lang = "phantom_data" ]
83+ pub struct PhantomData < T : ?Sized > ;
84+ impl < T : ?Sized > Copy for PhantomData < T > { }
85+
86+ #[ lang = "unsafe_cell" ]
87+ #[ repr( transparent) ]
88+ pub struct UnsafeCell < T : ?Sized > {
89+ value : T ,
90+ }
91+
92+ pub trait Any : ' static { }
93+
94+ pub enum Option < T > {
95+ None ,
96+ Some ( T ) ,
97+ }
98+ impl < T : Copy > Copy for Option < T > { }
99+
100+ pub enum Result < T , E > {
101+ Ok ( T ) ,
102+ Err ( E ) ,
103+ }
104+ impl < T : Copy , E : Copy > Copy for Result < T , E > { }
105+
106+ #[ lang = "manually_drop" ]
107+ #[ repr( transparent) ]
108+ pub struct ManuallyDrop < T : ?Sized > {
109+ value : T ,
110+ }
111+ impl < T : Copy + ?Sized > Copy for ManuallyDrop < T > { }
112+
113+ #[ repr( transparent) ]
114+ #[ rustc_layout_scalar_valid_range_start( 1 ) ]
115+ #[ rustc_nonnull_optimization_guaranteed]
116+ pub struct NonNull < T : ?Sized > {
117+ pointer : * const T ,
118+ }
119+ impl < T : ?Sized > Copy for NonNull < T > { }
120+
121+ #[ repr( transparent) ]
122+ #[ rustc_layout_scalar_valid_range_start( 1 ) ]
123+ #[ rustc_nonnull_optimization_guaranteed]
124+ pub struct NonZeroI32 ( i32 ) ;
125+
126+ // This just stands in for a non-trivial type.
127+ pub struct Vec < T > {
128+ ptr : NonNull < T > ,
129+ cap : usize ,
130+ len : usize ,
131+ }
132+
133+ pub struct Unique < T : ?Sized > {
134+ pub pointer : NonNull < T > ,
135+ pub _marker : PhantomData < T > ,
136+ }
137+
138+ pub struct Global ;
139+
140+ #[ lang = "owned_box" ]
141+ pub struct Box < T : ?Sized , A = Global > ( Unique < T > , A ) ;
142+
143+ #[ repr( C ) ]
144+ struct RcBox < T : ?Sized > {
145+ strong : UnsafeCell < usize > ,
146+ weak : UnsafeCell < usize > ,
147+ value : T ,
148+ }
149+ pub struct Rc < T : ?Sized , A = Global > {
150+ ptr : NonNull < RcBox < T > > ,
151+ phantom : PhantomData < RcBox < T > > ,
152+ alloc : A ,
153+ }
154+
155+ #[ repr( C , align( 8 ) ) ]
156+ struct AtomicUsize ( usize ) ;
157+ #[ repr( C ) ]
158+ struct ArcInner < T : ?Sized > {
159+ strong : AtomicUsize ,
160+ weak : AtomicUsize ,
161+ data : T ,
162+ }
163+ pub struct Arc < T : ?Sized , A = Global > {
164+ ptr : NonNull < ArcInner < T > > ,
165+ phantom : PhantomData < ArcInner < T > > ,
166+ alloc : A ,
167+ }
168+ }
169+ #[ cfg( not( host) ) ]
170+ use prelude:: * ;
171+
14172macro_rules! assert_abi_compatible {
15173 ( $name: ident, $t1: ty, $t2: ty) => {
16174 mod $name {
@@ -26,8 +184,13 @@ macro_rules! assert_abi_compatible {
26184 } ;
27185}
28186
29- #[ derive( Copy , Clone ) ]
30187struct Zst ;
188+ impl Copy for Zst { }
189+ impl Clone for Zst {
190+ fn clone ( & self ) -> Self {
191+ Zst
192+ }
193+ }
31194
32195#[ repr( C ) ]
33196struct ReprC1 < T : ?Sized > ( T ) ;
@@ -85,8 +248,8 @@ test_abi_compatible!(nonzero_int, NonZeroI32, i32);
85248
86249// `DispatchFromDyn` relies on ABI compatibility.
87250// This is interesting since these types are not `repr(transparent)`.
88- test_abi_compatible ! ( rc, std :: rc :: Rc <i32 >, * mut i32 ) ;
89- test_abi_compatible ! ( arc, std :: sync :: Arc <i32 >, * mut i32 ) ;
251+ test_abi_compatible ! ( rc, Rc <i32 >, * mut i32 ) ;
252+ test_abi_compatible ! ( arc, Arc <i32 >, * mut i32 ) ;
90253
91254// `repr(transparent)` compatibility.
92255#[ repr( transparent) ]
@@ -160,7 +323,7 @@ mod unsized_ {
160323 use super :: * ;
161324 test_transparent_unsized ! ( str_, str ) ;
162325 test_transparent_unsized ! ( slice, [ u8 ] ) ;
163- test_transparent_unsized ! ( dyn_trait, dyn std :: any :: Any ) ;
326+ test_transparent_unsized ! ( dyn_trait, dyn Any ) ;
164327}
165328
166329// RFC 3391 <https://rust-lang.github.io/rfcs/3391-result_ffi_guarantees.html>.
@@ -185,7 +348,7 @@ test_nonnull!(ref_unsized, &[i32]);
185348test_nonnull ! ( mut_unsized, & mut [ i32 ] ) ;
186349test_nonnull ! ( fn_, fn ( ) ) ;
187350test_nonnull ! ( nonnull, NonNull <i32 >) ;
188- test_nonnull ! ( nonnull_unsized, NonNull <dyn std :: fmt :: Debug >) ;
351+ test_nonnull ! ( nonnull_unsized, NonNull <dyn Any >) ;
189352test_nonnull ! ( non_zero, NonZeroI32 ) ;
190353
191354fn main ( ) { }
0 commit comments