authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-20 14:23:37-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-02-20 14:23:37-05:00
loga933a59ced8175a513ff123c3496b1b563d58453
treed5a56d76d0a6156a925a86507d27a4025b317890
parent6214f66dc108bd34a3ffa1ba5ac7704050a2f156
parentec4cd87ed76b47eae9fce95f18ec396aa44f2c0f
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14682 from ziglang/stage1-coverage

add test coverage for fixed stage1 bugs

5 files changed, 95 insertions(+), 0 deletions(-)

test/behavior/basic.zig+14
......@@ -1143,3 +1143,17 @@ test "orelse coercion as function argument" {
11431143 var foo = Container.init(optional orelse .{});
11441144 try expect(foo.a.?.start == -1);
11451145}
1146
1147test "runtime-known globals initialized with undefined" {
1148 const S = struct {
1149 var array: [10]u32 = [_]u32{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
1150 var vp: [*]u32 = undefined;
1151 var s: []u32 = undefined;
1152 };
1153
1154 S.vp = &S.array;
1155 S.s = S.vp[0..5];
1156
1157 try expect(S.s[0] == 1);
1158 try expect(S.s[4] == 5);
1159}
test/behavior/cast.zig+9
......@@ -1568,3 +1568,12 @@ test "@volatileCast without a result location" {
15681568 try expect(@TypeOf(z) == *i32);
15691569 try expect(z.* == 1234);
15701570}
1571
1572test "coercion from single-item pointer to @as to slice" {
1573 var x: u32 = 1;
1574
1575 // Why the following line gets a compile error?
1576 const t: []u32 = @as(*[1]u32, &x);
1577
1578 try expect(t[0] == 1);
1579}
test/behavior/struct.zig+35
......@@ -1578,3 +1578,38 @@ test "directly initiating tuple like struct" {
15781578 const a = struct { u8 }{8};
15791579 try expect(a[0] == 8);
15801580}
1581
1582test "instantiate struct with comptime field" {
1583 {
1584 var things = struct {
1585 comptime foo: i8 = 1,
1586 }{};
1587
1588 comptime std.debug.assert(things.foo == 1);
1589 }
1590
1591 {
1592 const T = struct {
1593 comptime foo: i8 = 1,
1594 };
1595 var things = T{};
1596
1597 comptime std.debug.assert(things.foo == 1);
1598 }
1599
1600 {
1601 var things: struct {
1602 comptime foo: i8 = 1,
1603 } = .{};
1604
1605 comptime std.debug.assert(things.foo == 1);
1606 }
1607
1608 {
1609 var things: struct {
1610 comptime foo: i8 = 1,
1611 } = undefined; // Segmentation fault at address 0x0
1612
1613 comptime std.debug.assert(things.foo == 1);
1614 }
1615}
test/behavior/type_info.zig+6
......@@ -603,3 +603,9 @@ test "@typeInfo decls ignore dependency loops" {
603603 };
604604 _ = S.foo;
605605}
606
607test "type info of tuple of string literal default value" {
608 const struct_field = @typeInfo(@TypeOf(.{"hi"})).Struct.fields[0];
609 const value = @ptrCast(*align(1) const *const [2:0]u8, struct_field.default_value.?).*;
610 comptime std.debug.assert(value[0] == 'h');
611}
test/cases/compile_errors/error_set_membership.zig created+31
......@@ -0,0 +1,31 @@
1const std = @import("std");
2
3const Error = error{InvalidCharacter};
4
5const Direction = enum { upside_down };
6
7const Barrrr = union(enum) {
8 float: f64,
9 direction: Direction,
10};
11
12fn fooey(bar: std.meta.Tag(Barrrr), args: []const []const u8) !Barrrr {
13 return switch (bar) {
14 .float => .{ .float = try std.fmt.parseFloat(f64, args[0]) },
15 .direction => if (std.mem.eql(u8, args[0], "upside_down"))
16 Barrrr{ .direction = .upside_down }
17 else
18 error.InvalidDirection,
19 };
20}
21
22pub fn main() Error!void {
23 std.debug.print("{}", .{try fooey(.direction, &[_][]const u8{ "one", "two", "three" })});
24}
25
26// error
27// backend=llvm
28// target=native
29//
30// :23:29: error: expected type 'error{InvalidCharacter}', found '@typeInfo(@typeInfo(@TypeOf(tmp.fooey)).Fn.return_type.?).ErrorUnion.error_set'
31// :23:29: note: 'error.InvalidDirection' not a member of destination error set