1
+ #![ feature( never_type) ]
2
+
1
3
use std:: rc:: Rc ;
2
4
use std:: { mem, num, ptr} ;
3
5
@@ -12,6 +14,18 @@ fn id<T>(x: T) -> T {
12
14
x
13
15
}
14
16
17
+ #[ derive( Copy , Clone ) ]
18
+ enum Either < T , U > {
19
+ Left ( T ) ,
20
+ Right ( U ) ,
21
+ }
22
+ #[ derive( Copy , Clone ) ]
23
+ enum Either2 < T , U > {
24
+ Left ( T ) ,
25
+ #[ allow( unused) ]
26
+ Right ( U , ( ) ) ,
27
+ }
28
+
15
29
fn test_abi_compat < T : Clone , U : Clone > ( t : T , u : U ) {
16
30
fn id < T > ( x : T ) -> T {
17
31
x
@@ -81,6 +95,8 @@ fn main() {
81
95
test_abi_compat ( main as fn ( ) , id :: < i32 > as fn ( i32 ) -> i32 ) ;
82
96
// - 1-ZST
83
97
test_abi_compat ( ( ) , [ 0u8 ; 0 ] ) ;
98
+
99
+ // Guaranteed null-pointer-layout optimizations:
84
100
// - Guaranteed Option<X> null-pointer-optimizations (RFC 3391).
85
101
test_abi_compat ( & 0u32 as * const u32 , Some ( & 0u32 ) ) ;
86
102
test_abi_compat ( main as fn ( ) , Some ( main as fn ( ) ) ) ;
@@ -89,6 +105,7 @@ fn main() {
89
105
test_abi_compat ( 0u32 , Some ( Wrapper ( num:: NonZeroU32 :: new ( 1u32 ) . unwrap ( ) ) ) ) ;
90
106
// - Guaranteed Result<X, ZST1> does the same as Option<X> (RFC 3391)
91
107
test_abi_compat ( & 0u32 as * const u32 , Result :: < _ , ( ) > :: Ok ( & 0u32 ) ) ;
108
+ test_abi_compat ( & 0u32 as * const u32 , Result :: < _ , !> :: Ok ( & 0u32 ) ) ;
92
109
test_abi_compat ( main as fn ( ) , Result :: < _ , ( ) > :: Ok ( main as fn ( ) ) ) ;
93
110
test_abi_compat ( 0u32 , Result :: < _ , ( ) > :: Ok ( num:: NonZeroU32 :: new ( 1 ) . unwrap ( ) ) ) ;
94
111
test_abi_compat ( & 0u32 as * const u32 , Result :: < _ , ( ) > :: Ok ( Wrapper ( & 0u32 ) ) ) ;
@@ -99,6 +116,13 @@ fn main() {
99
116
test_abi_compat ( 0u32 , Result :: < ( ) , _ > :: Err ( num:: NonZeroU32 :: new ( 1 ) . unwrap ( ) ) ) ;
100
117
test_abi_compat ( & 0u32 as * const u32 , Result :: < ( ) , _ > :: Err ( Wrapper ( & 0u32 ) ) ) ;
101
118
test_abi_compat ( 0u32 , Result :: < ( ) , _ > :: Err ( Wrapper ( num:: NonZeroU32 :: new ( 1 ) . unwrap ( ) ) ) ) ;
119
+ // - Guaranteed null-pointer-optimizations for custom option-like types
120
+ test_abi_compat ( & 0u32 as * const u32 , Either :: < _ , ( ) > :: Left ( & 0u32 ) ) ;
121
+ test_abi_compat ( & 0u32 as * const u32 , Either :: < _ , !> :: Left ( & 0u32 ) ) ;
122
+ test_abi_compat ( & 0u32 as * const u32 , Either :: < ( ) , _ > :: Right ( & 0u32 ) ) ;
123
+ test_abi_compat ( & 0u32 as * const u32 , Either :: < !, _ > :: Right ( & 0u32 ) ) ;
124
+ test_abi_compat ( & 0u32 as * const u32 , Either2 :: < _ , ( ) > :: Left ( & 0u32 ) ) ;
125
+ test_abi_compat ( & 0u32 as * const u32 , Either2 :: < _ , [ u8 ; 0 ] > :: Left ( & 0u32 ) ) ;
102
126
103
127
// These must work for *any* type, since we guarantee that `repr(transparent)` is ABI-compatible
104
128
// with the wrapped field.
0 commit comments