Commit d6e9862049

g-w1 <jacoblevgw@gmail.com>
2020-12-26 17:28:11
add test for @compileError in zig code, not only zir
1 parent 7f512f2
Changed files (4)
src/astgen.zig
@@ -2324,6 +2324,15 @@ fn import(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*
     return addZIRUnOp(mod, scope, src, .import, target);
 }
 
+fn compileError(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
+    try ensureBuiltinParamCount(mod, scope, call, 1);
+    const tree = scope.tree();
+    const src = tree.token_locs[call.builtin_token].start;
+    const params = call.params();
+    const target = try expr(mod, scope, .none, params[0]);
+    return addZIRUnOp(mod, scope, src, .compileerror, target);
+}
+
 fn compileLog(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
     const tree = scope.tree();
     const arena = scope.arena();
@@ -2378,6 +2387,8 @@ fn builtinCall(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.Built
         return rlWrap(mod, scope, rl, try addZIRNoOp(mod, scope, src, .breakpoint));
     } else if (mem.eql(u8, builtin_name, "@import")) {
         return rlWrap(mod, scope, rl, try import(mod, scope, call));
+    } else if (mem.eql(u8, builtin_name, "@compileError")) {
+        return compileError(mod, scope, call);
     } else if (mem.eql(u8, builtin_name, "@compileLog")) {
         return compileLog(mod, scope, call);
     } else {
src/zir.zig
@@ -1950,7 +1950,6 @@ const EmitZIR = struct {
                             .tag = Inst.CompileError.base_tag,
                         },
                         .positionals = .{
-
                             .msg = blk: {
                                 const msg_str = try self.arena.allocator.dupe(u8, err_msg_list.items[0].msg);
 
src/zir_sema.zig
@@ -487,7 +487,7 @@ fn analyzeInstExport(mod: *Module, scope: *Scope, export_inst: *zir.Inst.Export)
 }
 
 fn analyzeInstCompileError(mod: *Module, scope: *Scope, inst: *zir.Inst.CompileError) InnerError!*Inst {
-    const msg = try resolveConstString(mod,scope,inst.positionals.msg);
+    const msg = try resolveConstString(mod, scope, inst.positionals.msg);
     return mod.fail(scope, inst.base.src, "{}", .{msg});
 }
 
test/stage2/test.zig
@@ -1186,6 +1186,12 @@ pub fn addCases(ctx: *TestContext) !void {
 
     // "| true, 20, (runtime value), (function)" // TODO if this is here it invalidates the compile error checker. Need a way to check though.
 
+    ctx.compileError("compileError", linux_x64,
+        \\export fn _start() noreturn {
+        \\  @compileError("this is an error");
+        \\  unreachable;
+        \\}
+    , &[_][]const u8{":2:3: error: this is an error"});
     {
         var case = ctx.obj("variable shadowing", linux_x64);
         case.addError(