diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-11-12 01:40:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-11-12 01:40:31 +0000 |
| commit | 5502160bd23f14b91ac2bd3726a93bdd0b40cc53 (patch) | |
| tree | 81ced0f54f4c02eb7fd3bdd5d3d1248014f356a6 /test | |
| parent | ae0a219d1f5495acc4d82421fa24d84186c2a40d (diff) | |
| parent | 0c315e7f7613b085a203e9c94d222e846b5b9e46 (diff) | |
| download | zig-5502160bd23f14b91ac2bd3726a93bdd0b40cc53.tar.gz zig-5502160bd23f14b91ac2bd3726a93bdd0b40cc53.zip | |
Merge pull request #3652 from ziglang/anon-container-lit
implement anonymous struct literals and anonymous list literals
Diffstat (limited to 'test')
| -rw-r--r-- | test/compile_errors.zig | 21 | ||||
| -rw-r--r-- | test/stage1/behavior/array.zig | 14 | ||||
| -rw-r--r-- | test/stage1/behavior/async_fn.zig | 4 | ||||
| -rw-r--r-- | test/stage1/behavior/struct.zig | 59 | ||||
| -rw-r--r-- | test/stage1/behavior/union.zig | 22 |
5 files changed, 116 insertions, 4 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 0917c3dbb4..1adf3c3f8e 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -3,6 +3,23 @@ const builtin = @import("builtin"); pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( + "missing const in slice with nested array type", + \\const Geo3DTex2D = struct { vertices: [][2]f32 }; + \\pub fn getGeo3DTex2D() Geo3DTex2D { + \\ return Geo3DTex2D{ + \\ .vertices = [_][2]f32{ + \\ [_]f32{ -0.5, -0.5}, + \\ }, + \\ }; + \\} + \\export fn entry() void { + \\ var geo_data = getGeo3DTex2D(); + \\} + , + "tmp.zig:4:30: error: expected type '[][2]f32', found '[1][2]f32'", + ); + + cases.add( "slicing of global undefined pointer", \\var buf: *[1]u8 = undefined; \\export fn entry() void { @@ -216,9 +233,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ const obj = AstObject{ .lhsExpr = lhsExpr }; \\} , - "tmp.zig:4:19: error: union 'AstObject' depends on itself", - "tmp.zig:2:5: note: while checking this field", + "tmp.zig:1:17: error: struct 'LhsExpr' depends on itself", "tmp.zig:5:5: note: while checking this field", + "tmp.zig:2:5: note: while checking this field", ); cases.add( diff --git a/test/stage1/behavior/array.zig b/test/stage1/behavior/array.zig index f9ca1efdb9..0558a47fa4 100644 --- a/test/stage1/behavior/array.zig +++ b/test/stage1/behavior/array.zig @@ -298,3 +298,17 @@ test "implicit cast zero sized array ptr to slice" { const c: []const u8 = &b; expect(c.len == 0); } + +test "anonymous list literal syntax" { + const S = struct { + fn doTheTest() void { + var array: [4]u8 = .{1, 2, 3, 4}; + expect(array[0] == 1); + expect(array[1] == 2); + expect(array[2] == 3); + expect(array[3] == 4); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} diff --git a/test/stage1/behavior/async_fn.zig b/test/stage1/behavior/async_fn.zig index 99efa0e7be..f44ca541eb 100644 --- a/test/stage1/behavior/async_fn.zig +++ b/test/stage1/behavior/async_fn.zig @@ -1214,7 +1214,7 @@ test "spill target expr in a for loop" { } const Foo = struct { - slice: []i32, + slice: []const i32, }; fn atest(foo: *Foo) i32 { @@ -1245,7 +1245,7 @@ test "spill target expr in a for loop, with a var decl in the loop body" { } const Foo = struct { - slice: []i32, + slice: []const i32, }; fn atest(foo: *Foo) i32 { diff --git a/test/stage1/behavior/struct.zig b/test/stage1/behavior/struct.zig index 76ecad6b43..a42c261e3b 100644 --- a/test/stage1/behavior/struct.zig +++ b/test/stage1/behavior/struct.zig @@ -709,3 +709,62 @@ test "packed struct field passed to generic function" { var loaded = S.genericReadPackedField(&p.b); expect(loaded == 29); } + +test "anonymous struct literal syntax" { + const S = struct { + const Point = struct { + x: i32, + y: i32, + }; + + fn doTheTest() void { + var p: Point = .{ + .x = 1, + .y = 2, + }; + expect(p.x == 1); + expect(p.y == 2); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} + +test "fully anonymous struct" { + const S = struct { + fn doTheTest() void { + dump(.{ + .int = @as(u32, 1234), + .float = @as(f64, 12.34), + .b = true, + .s = "hi", + }); + } + fn dump(args: var) void { + expect(args.int == 1234); + expect(args.float == 12.34); + expect(args.b); + expect(args.s[0] == 'h'); + expect(args.s[1] == 'i'); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} + +test "fully anonymous list literal" { + const S = struct { + fn doTheTest() void { + dump(.{ @as(u32, 1234), @as(f64, 12.34), true, "hi"}); + } + fn dump(args: var) void { + expect(args.@"0" == 1234); + expect(args.@"1" == 12.34); + expect(args.@"2"); + expect(args.@"3"[0] == 'h'); + expect(args.@"3"[1] == 'i'); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} diff --git a/test/stage1/behavior/union.zig b/test/stage1/behavior/union.zig index 497aa7e574..caad1d474f 100644 --- a/test/stage1/behavior/union.zig +++ b/test/stage1/behavior/union.zig @@ -549,3 +549,25 @@ test "initialize global array of union" { expect(glbl_array[0].U0 == 1); expect(glbl_array[1].U1 == 2); } + +test "anonymous union literal syntax" { + const S = struct { + const Number = union { + int: i32, + float: f64, + }; + + fn doTheTest() void { + var i: Number = .{.int = 42}; + var f = makeNumber(); + expect(i.int == 42); + expect(f.float == 12.34); + } + + fn makeNumber() Number { + return .{.float = 12.34}; + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} |
