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!*...@@ -2324,6 +2324,15 @@ fn import(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*
2324 return addZIRUnOp(mod, scope, src, .import, target);2324 return addZIRUnOp(mod, scope, src, .import, target);
2325}2325}
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
2327fn compileLog(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {2336fn compileLog(mod: *Module, scope: *Scope, call: *ast.Node.BuiltinCall) InnerError!*zir.Inst {
2328 const tree = scope.tree();2337 const tree = scope.tree();
2329 const arena = scope.arena();2338 const arena = scope.arena();
...@@ -2378,6 +2387,8 @@ fn builtinCall(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.Built...@@ -2378,6 +2387,8 @@ fn builtinCall(mod: *Module, scope: *Scope, rl: ResultLoc, call: *ast.Node.Built
2378 return rlWrap(mod, scope, rl, try addZIRNoOp(mod, scope, src, .breakpoint));2387 return rlWrap(mod, scope, rl, try addZIRNoOp(mod, scope, src, .breakpoint));
2379 } else if (mem.eql(u8, builtin_name, "@import")) {2388 } else if (mem.eql(u8, builtin_name, "@import")) {
2380 return rlWrap(mod, scope, rl, try import(mod, scope, call));2389 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);
2381 } else if (mem.eql(u8, builtin_name, "@compileLog")) {2392 } else if (mem.eql(u8, builtin_name, "@compileLog")) {
2382 return compileLog(mod, scope, call);2393 return compileLog(mod, scope, call);
2383 } else {2394 } else {
src/zir.zig-1
...@@ -1950,7 +1950,6 @@ const EmitZIR = struct {...@@ -1950,7 +1950,6 @@ const EmitZIR = struct {
1950 .tag = Inst.CompileError.base_tag,1950 .tag = Inst.CompileError.base_tag,
1951 },1951 },
1952 .positionals = .{1952 .positionals = .{
1953
1954 .msg = blk: {1953 .msg = blk: {
1955 const msg_str = try self.arena.allocator.dupe(u8, err_msg_list.items[0].msg);1954 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)...@@ -487,7 +487,7 @@ fn analyzeInstExport(mod: *Module, scope: *Scope, export_inst: *zir.Inst.Export)
487}487}
488488
489fn analyzeInstCompileError(mod: *Module, scope: *Scope, inst: *zir.Inst.CompileError) InnerError!*Inst {489fn 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);
491 return mod.fail(scope, inst.base.src, "{}", .{msg});491 return mod.fail(scope, inst.base.src, "{}", .{msg});
492}492}
493493
test/stage2/test.zig+6
...@@ -1186,6 +1186,12 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1186,6 +1186,12 @@ pub fn addCases(ctx: *TestContext) !void {
11861186
1187 // "| true, 20, (runtime value), (function)" // TODO if this is here it invalidates the compile error checker. Need a way to check though.1187 // "| 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"});
1189 {1195 {
1190 var case = ctx.obj("variable shadowing", linux_x64);1196 var case = ctx.obj("variable shadowing", linux_x64);
1191 case.addError(1197 case.addError(