From 158720207b0454c2f09bc10f99e240cfaccb752d Mon Sep 17 00:00:00 2001 From: xdBronch <51252236+xdBronch@users.noreply.github.com> Date: Fri, 7 Nov 2025 07:53:04 -0500 Subject: [PATCH] sema: print @panic message at comptime --- src/Sema.zig | 31 +++++++++++++++---- .../panic_called_at_compile_time.zig | 2 +- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 0b303d9ad7df..1aec52f33472 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2024,7 +2024,9 @@ fn resolveConstString( block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref, - reason: ComptimeReason, + /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value + /// being comptime-resolved is that the block is being comptime-evaluated. + reason: ?ComptimeReason, ) ![]u8 { const air_inst = try sema.resolveInst(zir_ref); return sema.toConstString(block, src, air_inst, reason); @@ -2035,7 +2037,9 @@ pub fn toConstString( block: *Block, src: LazySrcLoc, air_inst: Air.Inst.Ref, - reason: ComptimeReason, + /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value + /// being comptime-resolved is that the block is being comptime-evaluated. + reason: ?ComptimeReason, ) ![]u8 { const pt = sema.pt; const coerced_inst = try sema.coerce(block, .slice_const_u8, air_inst, src); @@ -2261,6 +2265,8 @@ fn resolveConstValue( block: *Block, src: LazySrcLoc, inst: Air.Inst.Ref, + /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value + /// being comptime-resolved is that the block is being comptime-evaluated. reason: ?ComptimeReason, ) CompileError!Value { return try sema.resolveValue(inst) orelse { @@ -2288,6 +2294,8 @@ fn resolveConstDefinedValue( block: *Block, src: LazySrcLoc, air_ref: Air.Inst.Ref, + /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value + /// being comptime-resolved is that the block is being comptime-evaluated. reason: ?ComptimeReason, ) CompileError!Value { const val = try sema.resolveConstValue(block, src, air_ref, reason); @@ -2317,7 +2325,14 @@ pub fn resolveFinalDeclValue( return val; } -fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: ?ComptimeReason) CompileError { +fn failWithNeededComptime( + sema: *Sema, + block: *Block, + src: LazySrcLoc, + /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value + /// being comptime-resolved is that the block is being comptime-evaluated. + reason: ?ComptimeReason, +) CompileError { const msg, const fail_block = msg: { const msg = try sema.errMsg(src, "unable to resolve comptime value", .{}); errdefer msg.destroy(sema.gpa); @@ -5569,10 +5584,12 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void const src = block.nodeOffset(inst_data.src_node); const msg_inst = try sema.resolveInst(inst_data.operand); - const coerced_msg = try sema.coerce(block, .slice_const_u8, msg_inst, block.builtinCallArgSrc(inst_data.src_node, 0)); + const arg_src = block.builtinCallArgSrc(inst_data.src_node, 0); + const coerced_msg = try sema.coerce(block, .slice_const_u8, msg_inst, arg_src); if (block.isComptime()) { - return sema.fail(block, src, "encountered @panic at comptime", .{}); + const string = try sema.resolveConstString(block, arg_src, inst_data.operand, null); + return sema.fail(block, src, "encountered @panic at comptime: {s}", .{string}); } // We only apply the first hint in a branch. @@ -37261,7 +37278,9 @@ fn derefSliceAsArray( block: *Block, src: LazySrcLoc, slice_val: Value, - reason: ComptimeReason, + /// `null` may be passed only if `block.isComptime()`. It indicates that the reason for the value + /// being comptime-resolved is that the block is being comptime-evaluated. + reason: ?ComptimeReason, ) CompileError!Value { return try sema.maybeDerefSliceAsArray(block, src, slice_val) orelse { return sema.failWithNeededComptime(block, src, reason); diff --git a/test/cases/compile_errors/panic_called_at_compile_time.zig b/test/cases/compile_errors/panic_called_at_compile_time.zig index d05e6d063c94..30cafcc8a9c5 100644 --- a/test/cases/compile_errors/panic_called_at_compile_time.zig +++ b/test/cases/compile_errors/panic_called_at_compile_time.zig @@ -8,4 +8,4 @@ export fn entry() void { // error // -// :3:9: error: encountered @panic at comptime +// :3:9: error: encountered @panic at comptime: aoeu