@@ -65,3 +65,135 @@ test "implicit ptr to *c_void" {
65
65
var c : * u32 = @ptrCast (* u32 , ptr2 .? );
66
66
try expect (c .* == 1 );
67
67
}
68
+
69
+ const A = struct {
70
+ a : i32 ,
71
+ };
72
+ test "return null from fn() anyerror!?&T" {
73
+ const a = returnNullFromOptionalTypeErrorRef ();
74
+ const b = returnNullLitFromOptionalTypeErrorRef ();
75
+ try expect ((try a ) == null and (try b ) == null );
76
+ }
77
+ fn returnNullFromOptionalTypeErrorRef () anyerror ! ? * A {
78
+ const a : ? * A = null ;
79
+ return a ;
80
+ }
81
+ fn returnNullLitFromOptionalTypeErrorRef () anyerror ! ? * A {
82
+ return null ;
83
+ }
84
+
85
+ test "peer type resolution: [0]u8 and []const u8" {
86
+ try expect (peerTypeEmptyArrayAndSlice (true , "hi" ).len == 0 );
87
+ try expect (peerTypeEmptyArrayAndSlice (false , "hi" ).len == 1 );
88
+ comptime {
89
+ try expect (peerTypeEmptyArrayAndSlice (true , "hi" ).len == 0 );
90
+ try expect (peerTypeEmptyArrayAndSlice (false , "hi" ).len == 1 );
91
+ }
92
+ }
93
+ fn peerTypeEmptyArrayAndSlice (a : bool , slice : []const u8 ) []const u8 {
94
+ if (a ) {
95
+ return &[_ ]u8 {};
96
+ }
97
+
98
+ return slice [0.. 1];
99
+ }
100
+
101
+ test "implicitly cast from [N]T to ?[]const T" {
102
+ try expect (mem .eql (u8 , castToOptionalSlice ().? , "hi" ));
103
+ comptime try expect (mem .eql (u8 , castToOptionalSlice ().? , "hi" ));
104
+ }
105
+
106
+ fn castToOptionalSlice () ? []const u8 {
107
+ return "hi" ;
108
+ }
109
+
110
+ test "cast u128 to f128 and back" {
111
+ comptime try testCast128 ();
112
+ try testCast128 ();
113
+ }
114
+
115
+ fn testCast128 () ! void {
116
+ try expect (cast128Int (cast128Float (0x7fff0000000000000000000000000000 )) == 0x7fff0000000000000000000000000000 );
117
+ }
118
+
119
+ fn cast128Int (x : f128 ) u128 {
120
+ return @bitCast (u128 , x );
121
+ }
122
+
123
+ fn cast128Float (x : u128 ) f128 {
124
+ return @bitCast (f128 , x );
125
+ }
126
+
127
+ test "implicit cast from *[N]T to ?[*]T" {
128
+ var x : ? [* ]u16 = null ;
129
+ var y : [4 ]u16 = [4 ]u16 { 0 , 1 , 2 , 3 };
130
+
131
+ x = & y ;
132
+ try expect (std .mem .eql (u16 , x .? [0.. 4], y [0.. 4]));
133
+ x .? [0 ] = 8 ;
134
+ y [3 ] = 6 ;
135
+ try expect (std .mem .eql (u16 , x .? [0.. 4], y [0.. 4]));
136
+ }
137
+
138
+ test "implicit cast from *T to ?*c_void" {
139
+ var a : u8 = 1 ;
140
+ incrementVoidPtrValue (& a );
141
+ try std .testing .expect (a == 2 );
142
+ }
143
+
144
+ fn incrementVoidPtrValue (value : ? * c_void ) void {
145
+ @ptrCast (* u8 , value .? ).* += 1 ;
146
+ }
147
+
148
+ test "implicit cast *[0]T to E![]const u8" {
149
+ var x = @as (anyerror ! []const u8 , &[0 ]u8 {});
150
+ try expect ((x catch unreachable ).len == 0 );
151
+ }
152
+
153
+ var global_array : [4 ]u8 = undefined ;
154
+ test "cast from array reference to fn" {
155
+ const f = @ptrCast (fn () callconv (.C ) void , & global_array );
156
+ try expect (@ptrToInt (f ) == @ptrToInt (& global_array ));
157
+ }
158
+
159
+ test "*const [N]null u8 to ?[]const u8" {
160
+ const S = struct {
161
+ fn doTheTest () ! void {
162
+ var a = "Hello" ;
163
+ var b : ? []const u8 = a ;
164
+ try expect (mem .eql (u8 , b .? , "Hello" ));
165
+ }
166
+ };
167
+ try S .doTheTest ();
168
+ comptime try S .doTheTest ();
169
+ }
170
+
171
+ test "cast between [*c]T and ?[*:0]T on fn parameter" {
172
+ const S = struct {
173
+ const Handler = ? fn ([* c ]const u8 ) callconv (.C ) void ;
174
+ fn addCallback (handler : Handler ) void {
175
+ _ = handler ;
176
+ }
177
+
178
+ fn myCallback (cstr : ? [* :0 ]const u8 ) callconv (.C ) void {
179
+ _ = cstr ;
180
+ }
181
+
182
+ fn doTheTest () void {
183
+ addCallback (myCallback );
184
+ }
185
+ };
186
+ S .doTheTest ();
187
+ }
188
+
189
+ var global_struct : struct { f0 : usize } = undefined ;
190
+ test "assignment to optional pointer result loc" {
191
+ var foo : struct { ptr : ? * c_void } = .{ .ptr = & global_struct };
192
+ try expect (foo .ptr .? == @ptrCast (* c_void , & global_struct ));
193
+ }
194
+
195
+ test "cast between *[N]void and []void" {
196
+ var a : [4 ]void = undefined ;
197
+ var b : []void = & a ;
198
+ try expect (b .len == 4 );
199
+ }
0 commit comments