Skip to content

Commit 1587202

Browse files
committed
sema: print @Panic message at comptime
1 parent ce355e0 commit 1587202

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/Sema.zig

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,9 @@ fn resolveConstString(
20242024
block: *Block,
20252025
src: LazySrcLoc,
20262026
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,
20282030
) ![]u8 {
20292031
const air_inst = try sema.resolveInst(zir_ref);
20302032
return sema.toConstString(block, src, air_inst, reason);
@@ -2035,7 +2037,9 @@ pub fn toConstString(
20352037
block: *Block,
20362038
src: LazySrcLoc,
20372039
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,
20392043
) ![]u8 {
20402044
const pt = sema.pt;
20412045
const coerced_inst = try sema.coerce(block, .slice_const_u8, air_inst, src);
@@ -2261,6 +2265,8 @@ fn resolveConstValue(
22612265
block: *Block,
22622266
src: LazySrcLoc,
22632267
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.
22642270
reason: ?ComptimeReason,
22652271
) CompileError!Value {
22662272
return try sema.resolveValue(inst) orelse {
@@ -2288,6 +2294,8 @@ fn resolveConstDefinedValue(
22882294
block: *Block,
22892295
src: LazySrcLoc,
22902296
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.
22912299
reason: ?ComptimeReason,
22922300
) CompileError!Value {
22932301
const val = try sema.resolveConstValue(block, src, air_ref, reason);
@@ -2317,7 +2325,14 @@ pub fn resolveFinalDeclValue(
23172325
return val;
23182326
}
23192327

2320-
fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: ?ComptimeReason) CompileError {
2328+
fn 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 {
23212336
const msg, const fail_block = msg: {
23222337
const msg = try sema.errMsg(src, "unable to resolve comptime value", .{});
23232338
errdefer msg.destroy(sema.gpa);
@@ -5569,10 +5584,12 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
55695584
const src = block.nodeOffset(inst_data.src_node);
55705585
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

55745590
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});
55765593
}
55775594

55785595
// We only apply the first hint in a branch.
@@ -37261,7 +37278,9 @@ fn derefSliceAsArray(
3726137278
block: *Block,
3726237279
src: LazySrcLoc,
3726337280
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,
3726537284
) CompileError!Value {
3726637285
return try sema.maybeDerefSliceAsArray(block, src, slice_val) orelse {
3726737286
return sema.failWithNeededComptime(block, src, reason);

test/cases/compile_errors/panic_called_at_compile_time.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export fn entry() void {
88

99
// error
1010
//
11-
// :3:9: error: encountered @panic at comptime
11+
// :3:9: error: encountered @panic at comptime: aoeu

0 commit comments

Comments
 (0)