authorgravatar for 51252236+xdBronch@users.noreply.github.comxdBronch <51252236+xdBronch@users.noreply.github.com> 2025-11-07 07:53:04-05:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-11-10 14:21:26+00:00
logfb914a9a1002ec178aa493e974dfb5a3e6aa4bdd
tree6d8496c69cd1c03649a290e56fcf6bdeed32901e
parentd942f693c5ed6ea8d12b38960af72e44c10efb57

sema: print @panic message at comptime


2 files changed, 26 insertions(+), 7 deletions(-)

src/Sema.zig+25-6
...@@ -2024,7 +2024,9 @@ fn resolveConstString(...@@ -2024,7 +2024,9 @@ fn resolveConstString(
2024 block: *Block,2024 block: *Block,
2025 src: LazySrcLoc,2025 src: LazySrcLoc,
2026 zir_ref: Zir.Inst.Ref,2026 zir_ref: Zir.Inst.Ref,
2027 reason: ComptimeReason,2027 /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value
2028 /// being comptime-resolved is that the block is being comptime-evaluated.
2029 reason: ?ComptimeReason,
2028) ![]u8 {2030) ![]u8 {
2029 const air_inst = try sema.resolveInst(zir_ref);2031 const air_inst = try sema.resolveInst(zir_ref);
2030 return sema.toConstString(block, src, air_inst, reason);2032 return sema.toConstString(block, src, air_inst, reason);
...@@ -2035,7 +2037,9 @@ pub fn toConstString(...@@ -2035,7 +2037,9 @@ pub fn toConstString(
2035 block: *Block,2037 block: *Block,
2036 src: LazySrcLoc,2038 src: LazySrcLoc,
2037 air_inst: Air.Inst.Ref,2039 air_inst: Air.Inst.Ref,
2038 reason: ComptimeReason,2040 /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value
2041 /// being comptime-resolved is that the block is being comptime-evaluated.
2042 reason: ?ComptimeReason,
2039) ![]u8 {2043) ![]u8 {
2040 const pt = sema.pt;2044 const pt = sema.pt;
2041 const coerced_inst = try sema.coerce(block, .slice_const_u8, air_inst, src);2045 const coerced_inst = try sema.coerce(block, .slice_const_u8, air_inst, src);
...@@ -2261,6 +2265,8 @@ fn resolveConstValue(...@@ -2261,6 +2265,8 @@ fn resolveConstValue(
2261 block: *Block,2265 block: *Block,
2262 src: LazySrcLoc,2266 src: LazySrcLoc,
2263 inst: Air.Inst.Ref,2267 inst: Air.Inst.Ref,
2268 /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value
2269 /// being comptime-resolved is that the block is being comptime-evaluated.
2264 reason: ?ComptimeReason,2270 reason: ?ComptimeReason,
2265) CompileError!Value {2271) CompileError!Value {
2266 return try sema.resolveValue(inst) orelse {2272 return try sema.resolveValue(inst) orelse {
...@@ -2288,6 +2294,8 @@ fn resolveConstDefinedValue(...@@ -2288,6 +2294,8 @@ fn resolveConstDefinedValue(
2288 block: *Block,2294 block: *Block,
2289 src: LazySrcLoc,2295 src: LazySrcLoc,
2290 air_ref: Air.Inst.Ref,2296 air_ref: Air.Inst.Ref,
2297 /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value
2298 /// being comptime-resolved is that the block is being comptime-evaluated.
2291 reason: ?ComptimeReason,2299 reason: ?ComptimeReason,
2292) CompileError!Value {2300) CompileError!Value {
2293 const val = try sema.resolveConstValue(block, src, air_ref, reason);2301 const val = try sema.resolveConstValue(block, src, air_ref, reason);
...@@ -2317,7 +2325,14 @@ pub fn resolveFinalDeclValue(...@@ -2317,7 +2325,14 @@ pub fn resolveFinalDeclValue(
2317 return val;2325 return val;
2318}2326}
23192327
2320fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: ?ComptimeReason) CompileError {2328fn failWithNeededComptime(
2329 sema: *Sema,
2330 block: *Block,
2331 src: LazySrcLoc,
2332 /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value
2333 /// being comptime-resolved is that the block is being comptime-evaluated.
2334 reason: ?ComptimeReason,
2335) CompileError {
2321 const msg, const fail_block = msg: {2336 const msg, const fail_block = msg: {
2322 const msg = try sema.errMsg(src, "unable to resolve comptime value", .{});2337 const msg = try sema.errMsg(src, "unable to resolve comptime value", .{});
2323 errdefer msg.destroy(sema.gpa);2338 errdefer msg.destroy(sema.gpa);
...@@ -5569,10 +5584,12 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -5569,10 +5584,12 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
5569 const src = block.nodeOffset(inst_data.src_node);5584 const src = block.nodeOffset(inst_data.src_node);
5570 const msg_inst = try sema.resolveInst(inst_data.operand);5585 const msg_inst = try sema.resolveInst(inst_data.operand);
55715586
5572 const coerced_msg = try sema.coerce(block, .slice_const_u8, msg_inst, block.builtinCallArgSrc(inst_data.src_node, 0));5587 const arg_src = block.builtinCallArgSrc(inst_data.src_node, 0);
5588 const coerced_msg = try sema.coerce(block, .slice_const_u8, msg_inst, arg_src);
55735589
5574 if (block.isComptime()) {5590 if (block.isComptime()) {
5575 return sema.fail(block, src, "encountered @panic at comptime", .{});5591 const string = try sema.resolveConstString(block, arg_src, inst_data.operand, null);
5592 return sema.fail(block, src, "encountered @panic at comptime: {s}", .{string});
5576 }5593 }
55775594
5578 // We only apply the first hint in a branch.5595 // We only apply the first hint in a branch.
...@@ -37261,7 +37278,9 @@ fn derefSliceAsArray(...@@ -37261,7 +37278,9 @@ fn derefSliceAsArray(
37261 block: *Block,37278 block: *Block,
37262 src: LazySrcLoc,37279 src: LazySrcLoc,
37263 slice_val: Value,37280 slice_val: Value,
37264 reason: ComptimeReason,37281 /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value
37282 /// being comptime-resolved is that the block is being comptime-evaluated.
37283 reason: ?ComptimeReason,
37265) CompileError!Value {37284) CompileError!Value {
37266 return try sema.maybeDerefSliceAsArray(block, src, slice_val) orelse {37285 return try sema.maybeDerefSliceAsArray(block, src, slice_val) orelse {
37267 return sema.failWithNeededComptime(block, src, reason);37286 return sema.failWithNeededComptime(block, src, reason);
test/cases/compile_errors/panic_called_at_compile_time.zig+1-1
...@@ -8,4 +8,4 @@ export fn entry() void {...@@ -8,4 +8,4 @@ export fn entry() void {
88
9// error9// error
10//10//
11// :3:9: error: encountered @panic at comptime11// :3:9: error: encountered @panic at comptime: aoeu