diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/src.zig | 11 | ||||
| -rw-r--r-- | test/behavior/union.zig | 40 | ||||
| -rw-r--r-- | test/cases/compile_errors/field_access_of_wrapped_type.zig | 20 |
3 files changed, 71 insertions, 0 deletions
diff --git a/test/behavior/src.zig b/test/behavior/src.zig index 77e420afcf..e6b84e5d56 100644 --- a/test/behavior/src.zig +++ b/test/behavior/src.zig @@ -32,3 +32,14 @@ test "@src used as a comptime parameter" { const T2 = S.Foo(@src()); try expect(T1 != T2); } + +test "@src in tuple passed to anytype function" { + const S = struct { + fn Foo(a: anytype) u32 { + return a[0].line; + } + }; + const l1 = S.Foo(.{@src()}); + const l2 = S.Foo(.{@src()}); + try expect(l1 != l2); +} diff --git a/test/behavior/union.zig b/test/behavior/union.zig index 3b040fcba9..f247bf6fa2 100644 --- a/test/behavior/union.zig +++ b/test/behavior/union.zig @@ -1492,3 +1492,43 @@ test "union reassignment can use previous value" { a = U{ .b = a.a }; try expect(a.b == 32); } + +test "packed union with zero-bit field" { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + + const S = packed struct { + nested: packed union { + zero: void, + sized: u32, + }, + bar: u32, + + fn doTest(self: @This()) !void { + try expect(self.bar == 42); + } + }; + try S.doTest(.{ .nested = .{ .zero = {} }, .bar = 42 }); +} + +test "reinterpreting enum value inside packed union" { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO + + const U = packed union { + tag: enum { a, b }, + val: u8, + + fn doTest() !void { + var u: @This() = .{ .tag = .a }; + u.val += 1; + try expect(u.tag == .b); + } + }; + try U.doTest(); + comptime try U.doTest(); +} diff --git a/test/cases/compile_errors/field_access_of_wrapped_type.zig b/test/cases/compile_errors/field_access_of_wrapped_type.zig new file mode 100644 index 0000000000..9d8a7ef17c --- /dev/null +++ b/test/cases/compile_errors/field_access_of_wrapped_type.zig @@ -0,0 +1,20 @@ +const Foo = struct { + a: i32, +}; +export fn f1() void { + var foo: ?Foo = undefined; + foo.a += 1; +} +export fn f2() void { + var foo: anyerror!Foo = undefined; + foo.a += 1; +} + +// error +// backend=stage2 +// target=native +// +// :6:8: error: optional type '?tmp.Foo' does not support field access +// :6:8: note: consider using '.?', 'orelse', or 'if' +// :10:8: error: error union type 'anyerror!tmp.Foo' does not support field access +// :10:8: note: consider using 'try', 'catch', or 'if' |
