From d6e9862049c069bcfcafdef3f3e7de842ba44cbd Mon Sep 17 00:00:00 2001 From: g-w1 Date: Sat, 26 Dec 2020 11:28:11 -0500 Subject: [PATCH] add test for @compileError in zig code, not only zir --- src/astgen.zig | 11 +++++++++++ src/zir.zig | 1 - src/zir_sema.zig | 2 +- test/stage2/test.zig | 6 ++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/astgen.zig b/src/astgen.zig index deb4eb6ae9..3db3f3654d 100644 --- a/src/astgen.zig +++ b/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 { diff --git a/src/zir.zig b/src/zir.zig index 927dfa4a21..b44d06bad5 100644 --- a/src/zir.zig +++ b/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); diff --git a/src/zir_sema.zig b/src/zir_sema.zig index cbb3dfc3fb..4f0c8c0233 100644 --- a/src/zir_sema.zig +++ b/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}); } diff --git a/test/stage2/test.zig b/test/stage2/test.zig index 2bda86092e..4e57bbf9f7 100644 --- a/test/stage2/test.zig +++ b/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(