diff options
| author | xdBronch <51252236+xdBronch@users.noreply.github.com> | 2025-11-07 07:53:04 -0500 |
|---|---|---|
| committer | Matthew Lugg <mlugg@mlugg.co.uk> | 2025-11-10 14:21:26 +0000 |
| commit | fb914a9a1002ec178aa493e974dfb5a3e6aa4bdd (patch) | |
| tree | 6d8496c69cd1c03649a290e56fcf6bdeed32901e /src/Sema.zig | |
| parent | d942f693c5ed6ea8d12b38960af72e44c10efb57 (diff) | |
| download | zig-fb914a9a1002ec178aa493e974dfb5a3e6aa4bdd.tar.gz zig-fb914a9a1002ec178aa493e974dfb5a3e6aa4bdd.zip | |
sema: print @panic message at comptime
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 0b303d9ad7..1aec52f334 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); |
