Commit 0e952a9f3a

Noam Preil <pleasantatk@gmail.com>
2020-06-26 11:00:53
Stage2/Testing: Simply incremental compilation tests
1 parent c8f60b2
Changed files (2)
src-self-hosted
test
src-self-hosted/test.zig
@@ -274,6 +274,32 @@ pub const TestContext = struct {
         ctx.addObj(name, target, T).compiles(src);
     }
 
+    pub fn incrementalFailure(
+        ctx: *TestContext,
+        name: []const u8,
+        target: std.zig.CrossTarget,
+        src: [:0]const u8,
+        expected_errors: []const []const u8,
+        fixed_src: [:0]const u8,
+    ) void {
+        var case = ctx.addObj(name, target, .Zig);
+        case.addError(src, expected_errors);
+        case.compiles(fixed_src);
+    }
+
+    pub fn incrementalFailureZIR(
+        ctx: *TestContext,
+        name: []const u8,
+        target: std.zig.CrossTarget,
+        src: [:0]const u8,
+        expected_errors: []const []const u8,
+        fixed_src: [:0]const u8,
+    ) void {
+        var case = ctx.addObj(name, target, .ZIR);
+        case.addError(src, expected_errors);
+        case.compiles(fixed_src);
+    }
+
     pub fn compiles(
         ctx: *TestContext,
         name: []const u8,
test/stage2/compile_errors.zig
@@ -43,58 +43,46 @@ pub fn addCases(ctx: *TestContext) !void {
         \\@1 = export(@0, "start")
     , &[_][]const u8{":4:9: error: unable to call function with naked calling convention"});
 
-    {
-        var case = ctx.objZIR("exported symbol collision", linux_x64);
-        // First, ensure we receive the error correctly
-        case.addError(
-            \\@noreturn = primitive(noreturn)
-            \\
-            \\@start_fnty = fntype([], @noreturn)
-            \\@start = fn(@start_fnty, {})
-            \\
-            \\@0 = str("_start")
-            \\@1 = export(@0, "start")
-            \\@2 = export(@0, "start")
-        , &[_][]const u8{":8:13: error: exported symbol collision: _start"});
-        // Next, ensure everything works properly on the next compilation with the problem fixed
-        case.compiles(
-            \\@noreturn = primitive(noreturn)
-            \\
-            \\@start_fnty = fntype([], @noreturn)
-            \\@start = fn(@start_fnty, {})
-            \\
-            \\@0 = str("_start")
-            \\@1 = export(@0, "start")
-        );
-    }
+    ctx.incrementalFailureZIR("exported symbol collision", linux_x64,
+        \\@noreturn = primitive(noreturn)
+        \\
+        \\@start_fnty = fntype([], @noreturn)
+        \\@start = fn(@start_fnty, {})
+        \\
+        \\@0 = str("_start")
+        \\@1 = export(@0, "start")
+        \\@2 = export(@0, "start")
+    , &[_][]const u8{":8:13: error: exported symbol collision: _start"},
+        \\@noreturn = primitive(noreturn)
+        \\
+        \\@start_fnty = fntype([], @noreturn)
+        \\@start = fn(@start_fnty, {})
+        \\
+        \\@0 = str("_start")
+        \\@1 = export(@0, "start")
+    );
+
+    ctx.incrementalFailure("function redefinition", linux_x64,
+        \\fn entry() void {}
+        \\fn entry() void {}
+    , &[_][]const u8{":2:4: error: redefinition of 'entry'"},
+        \\fn entry() void {}
+    );
+
     // TODO: need to make sure this works with other variants of export.
-    // As is, the same error occurs without export.
-    {
-        var case = ctx.obj("exported symbol collision", linux_x64);
-        case.addError(
-            \\export fn entry() void {}
-            \\export fn entry() void {}
-        , &[_][]const u8{":2:11: error: redefinition of 'entry'"});
-        case.compiles(
-            \\export fn entry() void {}
-        );
-        case.addError(
-            \\fn entry() void {}
-            \\fn entry() void {}
-        , &[_][]const u8{":2:4: error: redefinition of 'entry'"});
-        case.compiles(
-            \\export fn entry() void {}
-        );
-    }
-    {
-        var case = ctx.obj("missing function name", linux_x64);
-        case.addError(
-            \\fn() void {}
-        , &[_][]const u8{":1:3: error: missing function name"});
-        case.compiles(
-            \\fn a() void {}
-        );
-    }
+    ctx.incrementalFailure("function redefinition", linux_x64,
+        \\export fn entry() void {}
+        \\export fn entry() void {}
+    , &[_][]const u8{":2:11: error: redefinition of 'entry'"},
+        \\export fn entry() void {}
+    );
+
+    ctx.incrementalFailure("missing function name", linux_x64,
+        \\fn() void {}
+    , &[_][]const u8{":1:3: error: missing function name"},
+        \\fn a() void {}
+    );
+
     // TODO: re-enable these tests.
     // https://github.com/ziglang/zig/issues/1364