fix: return inside inline if no longer drops trailing statements
A `return` inside an `inline if` / comptime `case` branch — itself inside an
`inline for`, under a runtime `if` — made the compiler wrongly reject the
function as "body produces no value" and DROP its trailing statements (e.g. a
trailing `return -1`).
Root cause: the inline-if branch lowering sets the global `block_terminated` flag
when its taken arm returns (control_flow.zig / stmt.zig `lowerInlineBranch`),
unlike a bare `return` STATEMENT which deliberately never sets it (precisely to
avoid leaking past an `if cond { return }` merge — see the comment at
stmt.zig:37-42). The enclosing runtime-`if`'s merge never reset the flag, so it
leaked past the merge and `lowerBlock` skipped the following statements as dead.
Fix: after the runtime-`if` switches to its merge block, set
`block_terminated = then_diverged and has_else and else_diverged` — the `if`
leaves the block terminated ONLY when both arms diverged with an `else` covering
the cond-false edge (otherwise the merge is reachable and the flag must be
false). Adds `else_diverged` tracking alongside the existing `then_diverged`.
Adversarially reviewed (SHIP): the reachability predicate is correct across all
arm/else/divergence cases; the both-arms-diverge case still sets true (preserving
prior behavior); value-ternary and inline-for-unroll paths are unaffected.
Regression: examples/comptime/0651-comptime-inline-if-return.sx (nested inline-if
and comptime `case`, both with per-arm returns, asserting the trailing return is
emitted). Suite green (822/0).
This commit is contained in:
@@ -250,6 +250,7 @@ pub fn lowerIfExpr(self: *Lowering, ie: *const ast.IfExpr) Ref {
|
||||
const saved_target = self.target_type;
|
||||
if (is_value and result_type != .void) self.target_type = result_type;
|
||||
var then_diverged = false;
|
||||
var else_diverged = false;
|
||||
var then_snap = self.narrowSnapshot();
|
||||
self.applyNarrowing(present_true.items);
|
||||
if (is_value) {
|
||||
@@ -278,6 +279,7 @@ pub fn lowerIfExpr(self: *Lowering, ie: *const ast.IfExpr) Ref {
|
||||
self.applyNarrowing(present_false.items);
|
||||
if (is_value) {
|
||||
var v = self.lowerExpr(ie.else_branch.?);
|
||||
else_diverged = self.currentBlockHasTerminator();
|
||||
if (!self.currentBlockHasTerminator()) {
|
||||
const v_ty = self.builder.getRefType(v);
|
||||
if (v_ty != result_type and v_ty != .void and result_type != .void) {
|
||||
@@ -287,6 +289,7 @@ pub fn lowerIfExpr(self: *Lowering, ie: *const ast.IfExpr) Ref {
|
||||
}
|
||||
} else {
|
||||
self.lowerBlock(ie.else_branch.?);
|
||||
else_diverged = self.currentBlockHasTerminator();
|
||||
if (!self.currentBlockHasTerminator()) {
|
||||
self.builder.br(merge_bb, &.{});
|
||||
}
|
||||
@@ -302,6 +305,15 @@ pub fn lowerIfExpr(self: *Lowering, ie: *const ast.IfExpr) Ref {
|
||||
|
||||
// Continue at merge
|
||||
self.builder.switchToBlock(merge_bb);
|
||||
// The merge block is REACHABLE (control falls through this `if`) unless BOTH
|
||||
// arms diverged with an explicit `else` covering the cond-false edge. Reset
|
||||
// `block_terminated` to that reachability so the flag — which an inline-if in
|
||||
// either arm may have set (control_flow.zig / stmt.zig `lowerInlineBranch`)
|
||||
// when its taken branch returned — does NOT leak past this merge and wrongly
|
||||
// drop the enclosing block's trailing statements. Mirrors the bare-`return`-
|
||||
// statement rule documented in `lowerBlock` (a return terminates its block but
|
||||
// never sets the flag).
|
||||
self.block_terminated = then_diverged and has_else and else_diverged;
|
||||
if (is_value) {
|
||||
return self.builder.blockParam(merge_bb, 0, result_type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user