Skip to content

Commit 252c203

Browse files
committed
Sema: correctly label block_comptime for restoring error return trace index
Resolves: #22384
1 parent e6879e9 commit 252c203

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/Sema.zig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,21 +1658,27 @@ fn analyzeBodyInner(
16581658
const extra = sema.code.extraData(Zir.Inst.BlockComptime, pl_node.payload_index);
16591659
const block_body = sema.code.bodySlice(extra.end, extra.data.body_len);
16601660

1661-
if (block.isComptime()) {
1662-
// No need for a sub-block; just resolve the other body directly!
1663-
break :inst try sema.resolveInlineBody(block, block_body, inst);
1664-
}
1665-
16661661
var child_block = block.makeSubBlock();
16671662
defer child_block.instructions.deinit(sema.gpa);
1663+
1664+
// We won't have any merges, but we must ensure this block is properly labeled for
1665+
// any `.restore_err_ret_index_*` instructions.
1666+
var label: Block.Label = .{
1667+
.zir_block = inst,
1668+
.merges = undefined,
1669+
};
1670+
child_block.label = &label;
1671+
16681672
child_block.comptime_reason = .{ .reason = .{
16691673
.src = src,
16701674
.r = .{ .simple = extra.data.reason },
16711675
} };
16721676

16731677
const result = try sema.resolveInlineBody(&child_block, block_body, inst);
16741678

1675-
if (!try sema.isComptimeKnown(result)) {
1679+
// Only check for the result being comptime-known in the outermost `block_comptime`.
1680+
// That way, AstGen can safely elide redundant `block_comptime` without affecting semantics.
1681+
if (!block.isComptime() and !try sema.isComptimeKnown(result)) {
16761682
return sema.failWithNeededComptime(&child_block, src, null);
16771683
}
16781684

test/behavior/eval.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,3 +1744,10 @@ test "block with comptime-known result but possible runtime exit is comptime-kno
17441744
comptime assert(a == 123);
17451745
comptime assert(b == 456);
17461746
}
1747+
1748+
test "comptime labeled block implicit exit" {
1749+
const result = comptime b: {
1750+
if (false) break :b 123;
1751+
};
1752+
comptime assert(result == {});
1753+
}

0 commit comments

Comments
 (0)