authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2020-12-26 11:28:11-05:00
committergravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2020-12-26 11:34:15-05:00
logd6e9862049c069bcfcafdef3f3e7de842ba44cbd
treeb0527810c07ab7270603794e7c5d2caa07a57a8b
parent7f512f2236cf504775bb15002be89ecbb28a7679

add test for @compileError in zig code, not only zir


4 files changed, 18 insertions(+), 2 deletions(-)

src/astgen.zig+11
......@@ -2324,6 +2324,15 @@ fn import(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*
23242324 return addZIRUnOp(mod, scope, src, .import, target);
23252325}
23262326
2327fn compileError(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
2328 try ensureBuiltinParamCount(mod, scope, call, 1);
2329 const tree = scope.tree();
2330 const src = tree.token_locs[call.builtin_token].start;
2331 const params = call.params();
2332 const target = try expr(mod, scope, .none, params[0]);
2333 return addZIRUnOp(mod, scope, src, .compileerror, target);
2334}
2335
23272336fn compileLog(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
23282337 const tree = scope.tree();
23292338 const arena = scope.arena();
......@@ -2378,6 +2387,8 @@ fn builtinCall(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.Built
23782387 return rlWrap(mod, scope, rl, try addZIRNoOp(mod, scope, src, .breakpoint));
23792388 } else if (mem.eql(u8, builtin_name, "@import")) {
23802389 return rlWrap(mod, scope, rl, try import(mod, scope, call));
2390 } else if (mem.eql(u8, builtin_name, "@compileError")) {
2391 return compileError(mod, scope, call);
23812392 } else if (mem.eql(u8, builtin_name, "@compileLog")) {
23822393 return compileLog(mod, scope, call);
23832394 } else {
src/zir.zig-1
......@@ -1950,7 +1950,6 @@ const EmitZIR = struct {
19501950 .tag = Inst.CompileError.base_tag,
19511951 },
19521952 .positionals = .{
1953
19541953 .msg = blk: {
19551954 const msg_str = try self.arena.allocator.dupe(u8, err_msg_list.items[0].msg);
19561955
src/zir_sema.zig+1-1
......@@ -487,7 +487,7 @@ fn analyzeInstExport(mod: *Module, scope: *Scope, export_inst: *zir.Inst.Export)
487487}
488488
489489fn analyzeInstCompileError(mod: *Module, scope: *Scope, inst: *zir.Inst.CompileError) InnerError!*Inst {
490 const msg = try resolveConstString(mod,scope,inst.positionals.msg);
490 const msg = try resolveConstString(mod, scope, inst.positionals.msg);
491491 return mod.fail(scope, inst.base.src, "{}", .{msg});
492492}
493493
test/stage2/test.zig+6
......@@ -1186,6 +1186,12 @@ pub fn addCases(ctx: *TestContext) !void {
11861186
11871187 // "| true, 20, (runtime value), (function)" // TODO if this is here it invalidates the compile error checker. Need a way to check though.
11881188
1189 ctx.compileError("compileError", linux_x64,
1190 \\export fn _start() noreturn {
1191 \\ @compileError("this is an error");
1192 \\ unreachable;
1193 \\}
1194 , &[_][]const u8{":2:3: error: this is an error"});
11891195 {
11901196 var case = ctx.obj("variable shadowing", linux_x64);
11911197 case.addError(