aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/stage1/behavior/struct.zig33
-rw-r--r--test/stage2/compile_errors.zig45
-rw-r--r--test/stage2/zir.zig4
3 files changed, 80 insertions, 2 deletions
diff --git a/test/stage1/behavior/struct.zig b/test/stage1/behavior/struct.zig
index 3a4e0a4d93..7b8690d604 100644
--- a/test/stage1/behavior/struct.zig
+++ b/test/stage1/behavior/struct.zig
@@ -851,3 +851,36 @@ test "struct with union field" {
expectEqual(@as(u32, 2), True.ref);
expectEqual(true, True.kind.Bool);
}
+
+test "type coercion of anon struct literal to struct" {
+ const S = struct {
+ const S2 = struct {
+ A: u32,
+ B: []const u8,
+ C: void,
+ D: Foo = .{},
+ };
+
+ const Foo = struct {
+ field: i32 = 1234,
+ };
+
+ fn doTheTest() void {
+ var y: u32 = 42;
+ const t0 = .{ .A = 123, .B = "foo", .C = {} };
+ const t1 = .{ .A = y, .B = "foo", .C = {} };
+ const y0: S2 = t0;
+ var y1: S2 = t1;
+ expect(y0.A == 123);
+ expect(std.mem.eql(u8, y0.B, "foo"));
+ expect(y0.C == {});
+ expect(y0.D.field == 1234);
+ expect(y1.A == y);
+ expect(std.mem.eql(u8, y1.B, "foo"));
+ expect(y1.C == {});
+ expect(y1.D.field == 1234);
+ }
+ };
+ S.doTheTest();
+ comptime S.doTheTest();
+}
diff --git a/test/stage2/compile_errors.zig b/test/stage2/compile_errors.zig
index 9b8dcd91c4..43c41aa364 100644
--- a/test/stage2/compile_errors.zig
+++ b/test/stage2/compile_errors.zig
@@ -1,8 +1,53 @@
const TestContext = @import("../../src-self-hosted/test.zig").TestContext;
+const std = @import("std");
+
+const ErrorMsg = @import("../../src-self-hosted/Module.zig").ErrorMsg;
+
+const linux_x64 = std.zig.CrossTarget{
+ .cpu_arch = .x86_64,
+ .os_tag = .linux,
+};
pub fn addCases(ctx: *TestContext) !void {
+ ctx.addZIRError("call undefined local", linux_x64,
+ \\@noreturn = primitive(noreturn)
+ \\
+ \\@start_fnty = fntype([], @noreturn, cc=Naked)
+ \\@start = fn(@start_fnty, {
+ \\ %0 = call(%test, [])
+ \\})
+ // TODO: address inconsistency in this message and the one in the next test
+ , &[_][]const u8{":5:13: error: unrecognized identifier: %test"});
+
+ ctx.addZIRError("call with non-existent target", linux_x64,
+ \\@noreturn = primitive(noreturn)
+ \\
+ \\@start_fnty = fntype([], @noreturn, cc=Naked)
+ \\@start = fn(@start_fnty, {
+ \\ %0 = call(@notafunc, [])
+ \\})
+ \\@0 = str("_start")
+ \\@1 = ref(@0)
+ \\@2 = export(@1, @start)
+ , &[_][]const u8{":5:13: error: use of undeclared identifier 'notafunc'"});
+
+ // TODO: this error should occur at the call site, not the fntype decl
+ ctx.addZIRError("call naked function", linux_x64,
+ \\@noreturn = primitive(noreturn)
+ \\
+ \\@start_fnty = fntype([], @noreturn, cc=Naked)
+ \\@s = fn(@start_fnty, {})
+ \\@start = fn(@start_fnty, {
+ \\ %0 = call(@s, [])
+ \\})
+ \\@0 = str("_start")
+ \\@1 = ref(@0)
+ \\@2 = export(@1, @start)
+ , &[_][]const u8{":4:9: error: unable to call function with naked calling convention"});
+
// TODO: re-enable these tests.
// https://github.com/ziglang/zig/issues/1364
+ // TODO: add Zig AST -> ZIR testing pipeline
//try ctx.testCompileError(
// \\export fn entry() void {}
diff --git a/test/stage2/zir.zig b/test/stage2/zir.zig
index bf5d4b8eae..d58b30c29d 100644
--- a/test/stage2/zir.zig
+++ b/test/stage2/zir.zig
@@ -92,7 +92,7 @@ pub fn addCases(ctx: *TestContext) void {
{
var case = ctx.addZIRMulti("reference cycle with compile error in the cycle", linux_x64);
- case.addZIR(
+ case.addTransform(
\\@void = primitive(void)
\\@fnty = fntype([], @void, cc=C)
\\
@@ -171,7 +171,7 @@ pub fn addCases(ctx: *TestContext) void {
// Now we remove the call to `a`. `a` and `b` form a cycle, but no entry points are
// referencing either of them. This tests that the cycle is detected, and the error
// goes away.
- case.addZIR(
+ case.addTransform(
\\@void = primitive(void)
\\@fnty = fntype([], @void, cc=C)
\\