Skip to content

Commit b1517b1

Browse files
committed
stage2: move some tests which are now passing
1 parent cb24889 commit b1517b1

File tree

8 files changed

+272
-275
lines changed

8 files changed

+272
-275
lines changed

test/behavior.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ test {
145145
_ = @import("behavior/fn_delegation.zig");
146146
_ = @import("behavior/fn_in_struct_in_comptime.zig");
147147
_ = @import("behavior/for_stage1.zig");
148-
_ = @import("behavior/generics_stage1.zig");
149148
_ = @import("behavior/if_stage1.zig");
150149
_ = @import("behavior/import.zig");
151150
_ = @import("behavior/incomplete_struct_param_tld.zig");

test/behavior/cast.zig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,32 @@ test "array coersion to undefined at runtime" {
266266
array = undefined;
267267
try expect(std.mem.eql(u8, &array, &undefined_val));
268268
}
269+
270+
test "implicitly cast from int to anyerror!?T" {
271+
implicitIntLitToOptional();
272+
comptime implicitIntLitToOptional();
273+
}
274+
fn implicitIntLitToOptional() void {
275+
const f: ?i32 = 1;
276+
_ = f;
277+
const g: anyerror!?i32 = 1;
278+
_ = g catch {};
279+
}
280+
281+
test "return u8 coercing into ?u32 return type" {
282+
const S = struct {
283+
fn doTheTest() !void {
284+
try expect(foo(123).? == 123);
285+
}
286+
fn foo(arg: u8) ?u32 {
287+
return arg;
288+
}
289+
};
290+
try S.doTheTest();
291+
comptime try S.doTheTest();
292+
}
293+
294+
test "cast from ?[*]T to ??[*]T" {
295+
const a: ??[*]u8 = @as(?[*]u8, null);
296+
try expect(a != null and a.? == null);
297+
}

test/behavior/cast_llvm.zig

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,135 @@ test "implicit ptr to *c_void" {
6565
var c: *u32 = @ptrCast(*u32, ptr2.?);
6666
try expect(c.* == 1);
6767
}
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+
}

test/behavior/cast_stage1.zig

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -58,55 +58,6 @@ fn castToOptionalTypeError(z: i32) !void {
5858
try expect((b catch unreachable).?.a == 1);
5959
}
6060

61-
test "implicitly cast from int to anyerror!?T" {
62-
implicitIntLitToOptional();
63-
comptime implicitIntLitToOptional();
64-
}
65-
fn implicitIntLitToOptional() void {
66-
const f: ?i32 = 1;
67-
_ = f;
68-
const g: anyerror!?i32 = 1;
69-
_ = g catch {};
70-
}
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-
11061
test "implicitly cast from [0]T to anyerror![]T" {
11162
try testCastZeroArrayToErrSliceMut();
11263
comptime try testCastZeroArrayToErrSliceMut();
@@ -191,23 +142,6 @@ fn testPeerErrorAndArray2(x: u8) anyerror![]const u8 {
191142
};
192143
}
193144

194-
test "cast u128 to f128 and back" {
195-
comptime try testCast128();
196-
try testCast128();
197-
}
198-
199-
fn testCast128() !void {
200-
try expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000);
201-
}
202-
203-
fn cast128Int(x: f128) u128 {
204-
return @bitCast(u128, x);
205-
}
206-
207-
fn cast128Float(x: u128) f128 {
208-
return @bitCast(f128, x);
209-
}
210-
211145
test "single-item pointer of array to slice to unknown length pointer" {
212146
try testCastPtrOfArrayToSliceAndPtr();
213147
comptime try testCastPtrOfArrayToSliceAndPtr();
@@ -316,27 +250,6 @@ test "@floatCast cast down" {
316250
}
317251
}
318252

319-
test "implicit cast from *[N]T to ?[*]T" {
320-
var x: ?[*]u16 = null;
321-
var y: [4]u16 = [4]u16{ 0, 1, 2, 3 };
322-
323-
x = &y;
324-
try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
325-
x.?[0] = 8;
326-
y[3] = 6;
327-
try expect(std.mem.eql(u16, x.?[0..4], y[0..4]));
328-
}
329-
330-
test "implicit cast from *T to ?*c_void" {
331-
var a: u8 = 1;
332-
incrementVoidPtrValue(&a);
333-
try std.testing.expect(a == 2);
334-
}
335-
336-
fn incrementVoidPtrValue(value: ?*c_void) void {
337-
@ptrCast(*u8, value.?).* += 1;
338-
}
339-
340253
test "peer type resolution: unreachable, null, slice" {
341254
const S = struct {
342255
fn doTheTest(num: usize, word: []const u8) !void {
@@ -374,11 +287,6 @@ test "peer type resolution: unreachable, error set, unreachable" {
374287
try expect(transformed_err == error.SystemResources);
375288
}
376289

377-
test "implicit cast *[0]T to E![]const u8" {
378-
var x = @as(anyerror![]const u8, &[0]u8{});
379-
try expect((x catch unreachable).len == 0);
380-
}
381-
382290
test "peer cast *[0]T to E![]const T" {
383291
var buffer: [5]u8 = "abcde".*;
384292
var buf: anyerror![]const u8 = buffer[0..];
@@ -395,24 +303,6 @@ test "peer cast *[0]T to []const T" {
395303
try expect(mem.eql(u8, "abcde", y));
396304
}
397305

398-
var global_array: [4]u8 = undefined;
399-
test "cast from array reference to fn" {
400-
const f = @ptrCast(fn () callconv(.C) void, &global_array);
401-
try expect(@ptrToInt(f) == @ptrToInt(&global_array));
402-
}
403-
404-
test "*const [N]null u8 to ?[]const u8" {
405-
const S = struct {
406-
fn doTheTest() !void {
407-
var a = "Hello";
408-
var b: ?[]const u8 = a;
409-
try expect(mem.eql(u8, b.?, "Hello"));
410-
}
411-
};
412-
try S.doTheTest();
413-
comptime try S.doTheTest();
414-
}
415-
416306
test "peer resolution of string literals" {
417307
const S = struct {
418308
const E = enum { a, b, c, d };
@@ -502,19 +392,6 @@ test "cast i8 fn call peers to i32 result" {
502392
comptime try S.doTheTest();
503393
}
504394

505-
test "return u8 coercing into ?u32 return type" {
506-
const S = struct {
507-
fn doTheTest() !void {
508-
try expect(foo(123).? == 123);
509-
}
510-
fn foo(arg: u8) ?u32 {
511-
return arg;
512-
}
513-
};
514-
try S.doTheTest();
515-
comptime try S.doTheTest();
516-
}
517-
518395
test "peer type resolution implicit cast to return type" {
519396
const S = struct {
520397
fn doTheTest() !void {
@@ -553,24 +430,6 @@ test "variable initialization uses result locations properly with regards to the
553430
try expect(x == 1);
554431
}
555432

556-
test "cast between [*c]T and ?[*:0]T on fn parameter" {
557-
const S = struct {
558-
const Handler = ?fn ([*c]const u8) callconv(.C) void;
559-
fn addCallback(handler: Handler) void {
560-
_ = handler;
561-
}
562-
563-
fn myCallback(cstr: ?[*:0]const u8) callconv(.C) void {
564-
_ = cstr;
565-
}
566-
567-
fn doTheTest() void {
568-
addCallback(myCallback);
569-
}
570-
};
571-
S.doTheTest();
572-
}
573-
574433
test "cast between C pointer with different but compatible types" {
575434
const S = struct {
576435
fn foo(arg: [*]c_ushort) u16 {
@@ -584,13 +443,6 @@ test "cast between C pointer with different but compatible types" {
584443
try S.doTheTest();
585444
}
586445

587-
var global_struct: struct { f0: usize } = undefined;
588-
589-
test "assignment to optional pointer result loc" {
590-
var foo: struct { ptr: ?*c_void } = .{ .ptr = &global_struct };
591-
try expect(foo.ptr.? == @ptrCast(*c_void, &global_struct));
592-
}
593-
594446
test "peer type resolve string lit with sentinel-terminated mutable slice" {
595447
var array: [4:0]u8 = undefined;
596448
array[4] = 0; // TODO remove this when #4372 is solved
@@ -649,14 +501,3 @@ test "comptime float casts" {
649501
fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) !void {
650502
try expect(@floatToInt(I, f) == i);
651503
}
652-
653-
test "cast from ?[*]T to ??[*]T" {
654-
const a: ??[*]u8 = @as(?[*]u8, null);
655-
try expect(a != null and a.? == null);
656-
}
657-
658-
test "cast between *[N]void and []void" {
659-
var a: [4]void = undefined;
660-
var b: []void = &a;
661-
try expect(b.len == 4);
662-
}

0 commit comments

Comments
 (0)