Commit 7c5c27e0
7c5c27e035d5Merged
Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): emit side-effect stmts in consumed if-expr branches
When an if is consumed for its value (RHS of assignment, return slot,
ternary arm, etc.), compile_if_expr previously kept only the last
statement of each branch — emitting (cond ? lastexpr : ...). Any
leading side-effect statements (puts, ivar writes, method calls, etc.)
in the branch body were silently dropped.
x = if true puts "a" # this was being dropped 1 end
The fix detects multi-stmt branches and switches from the ternary form
to a real if (cond) { ... tmp = lastexpr; } else { ... tmp = lastexpr; }
that:
- compiles each non-final statement with compile_stmt (firing its side effect under the right conditional gate)
- assigns the final statement's expression value to a temp
- returns the temp as the if-expression's value
Single-stmt branches (the common case) stay on the ternary fast path — no behavior change. Elsif chains recurse: the inner call returns either a temp name or a ternary string, both valid C expressions assignable to the outer temp. Poly-unified branches keep the existing box_value_to_poly wrapping.
Test (test/if_expr_multi_stmt_body.rb) exercises:
- Multi-stmt then- and else-branches assigned to a local
- Elsif chain recursion (multi-stmt branches inside the else slot)
- Nested if-expression used as a method argument (verifies emit() ordering — inner if-stmt prologue lands before outer call site)
- Mixed branch lengths (single-stmt then, multi-stmt else)
- Empty/missing else (implicit nil → 0)
Out-of-scope (documented in the fixture header): poly type-unification
of branches with concrete mixed types (int/string) hits a separate
inference bug where spinel picks one concrete type as the unified
type rather than poly. Same bug exists on the ternary fast path;
not introduced by this PR.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 7c5c27e035d5
- Parent
- d5b20a0db9db
spinel_codegen.rbmodifiedView file
"sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")" end def if_branch_has_multi_stmt(body) if body < 0 return 0 end if get_stmts(body).length > 1 return 1 end 0 end # Assign the value of an if-arm body to a temp, executing any # leading side-effect statements before the assignment. Body # convention: stmts[0..n-2] run for their side-effects; # stmts[n-1] supplies the arm's value. Empty/missing body → "0". def compile_if_arm_to_tmp(body, tmp, unified_t) if body < 0 emit(" " + tmp + " = 0;") return end stmts = get_stmts(body) if stmts.length == 0 emit(" " + tmp + " = 0;") return end i = 0 while i < stmts.length - 1 compile_stmt(stmts[i]) i = i + 1 end last_id = stmts.last val = compile_expr(last_id) if unified_t == "poly" && infer_type(last_id) != "poly" val = box_value_to_poly(infer_type(last_id), val) end emit(" " + tmp + " = " + val + ";") end def compile_if_expr(nid) cond = compile_cond_expr(@nd_predicate[nid]) unified_t = infer_type(nid) body = @nd_body[nid] sub = @nd_subsequent[nid] # Side-effect-aware path: when any branch holds more than one # statement, the leading stmts must execute conditionally. The # plain ternary form drops them. Emit a real if-statement that # assigns to a temp and return the temp as the expression value. # Elsif chains recurse — the inner call returns either a temp or # a ternary string, both valid expressions. has_then_multi = if_branch_has_multi_stmt(body) has_else_multi = 0 if sub >= 0 && @nd_type[sub] == "ElseNode" has_else_multi = if_branch_has_multi_stmt(@nd_body[sub]) end if has_then_multi == 1 || has_else_multi == 1 tmp = new_temp emit(" " + c_type(unified_t) + " " + tmp + ";") emit(" if (" + cond + ") {") @indent = @indent + 1 compile_if_arm_to_tmp(body, tmp, unified_t) @indent = @indent - 1 emit(" } else {") @indent = @indent + 1 if sub >= 0 && @nd_type[sub] == "ElseNode" compile_if_arm_to_tmp(@nd_body[sub], tmp, unified_t) elsif sub >= 0 sub_val = compile_if_expr(sub) sub_t = infer_type(sub) if unified_t == "poly" && sub_t != "poly" sub_val = box_value_to_poly(sub_t, sub_val) end emit(" " + tmp + " = " + sub_val + ";") else emit(" " + tmp + " = 0;") end @indent = @indent - 1 emit(" }") return tmp end # Fast path: every branch is at most one statement, so the # whole if-expression collapses to a C ternary. then_val = "0" then_t = "nil" body = @nd_body[nid] if body >= 0 stmts = get_stmts(body) if stmts.length > 0 end else_val = "0" else_t = "nil" sub = @nd_subsequent[nid] if sub >= 0 if @nd_type[sub] == "ElseNode" eb = @nd_body[sub]test/if_expr_multi_stmt_body.rbaddedView file
Loading diff…
test/if_expr_multi_stmt_body.rb.expectedaddedView file
Loading diff…