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

@@ -20202,12 +20202,90 @@
2020220202 "sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")"
2020320203 end
2020420204
20205+ def if_branch_has_multi_stmt(body)
20206+ if body < 0
20207+ return 0
20208+ end
20209+ if get_stmts(body).length > 1
20210+ return 1
20211+ end
20212+ 0
20213+ end
20214+
20215+ # Assign the value of an if-arm body to a temp, executing any
20216+ # leading side-effect statements before the assignment. Body
20217+ # convention: stmts[0..n-2] run for their side-effects;
20218+ # stmts[n-1] supplies the arm's value. Empty/missing body → "0".
20219+ def compile_if_arm_to_tmp(body, tmp, unified_t)
20220+ if body < 0
20221+ emit(" " + tmp + " = 0;")
20222+ return
20223+ end
20224+ stmts = get_stmts(body)
20225+ if stmts.length == 0
20226+ emit(" " + tmp + " = 0;")
20227+ return
20228+ end
20229+ i = 0
20230+ while i < stmts.length - 1
20231+ compile_stmt(stmts[i])
20232+ i = i + 1
20233+ end
20234+ last_id = stmts.last
20235+ val = compile_expr(last_id)
20236+ if unified_t == "poly" && infer_type(last_id) != "poly"
20237+ val = box_value_to_poly(infer_type(last_id), val)
20238+ end
20239+ emit(" " + tmp + " = " + val + ";")
20240+ end
20241+
2020520242 def compile_if_expr(nid)
2020620243 cond = compile_cond_expr(@nd_predicate[nid])
2020720244 unified_t = infer_type(nid)
20245+ body = @nd_body[nid]
20246+ sub = @nd_subsequent[nid]
20247+
20248+ # Side-effect-aware path: when any branch holds more than one
20249+ # statement, the leading stmts must execute conditionally. The
20250+ # plain ternary form drops them. Emit a real if-statement that
20251+ # assigns to a temp and return the temp as the expression value.
20252+ # Elsif chains recurse — the inner call returns either a temp or
20253+ # a ternary string, both valid expressions.
20254+ has_then_multi = if_branch_has_multi_stmt(body)
20255+ has_else_multi = 0
20256+ if sub >= 0 && @nd_type[sub] == "ElseNode"
20257+ has_else_multi = if_branch_has_multi_stmt(@nd_body[sub])
20258+ end
20259+ if has_then_multi == 1 || has_else_multi == 1
20260+ tmp = new_temp
20261+ emit(" " + c_type(unified_t) + " " + tmp + ";")
20262+ emit(" if (" + cond + ") {")
20263+ @indent = @indent + 1
20264+ compile_if_arm_to_tmp(body, tmp, unified_t)
20265+ @indent = @indent - 1
20266+ emit(" } else {")
20267+ @indent = @indent + 1
20268+ if sub >= 0 && @nd_type[sub] == "ElseNode"
20269+ compile_if_arm_to_tmp(@nd_body[sub], tmp, unified_t)
20270+ elsif sub >= 0
20271+ sub_val = compile_if_expr(sub)
20272+ sub_t = infer_type(sub)
20273+ if unified_t == "poly" && sub_t != "poly"
20274+ sub_val = box_value_to_poly(sub_t, sub_val)
20275+ end
20276+ emit(" " + tmp + " = " + sub_val + ";")
20277+ else
20278+ emit(" " + tmp + " = 0;")
20279+ end
20280+ @indent = @indent - 1
20281+ emit(" }")
20282+ return tmp
20283+ end
20284+
20285+ # Fast path: every branch is at most one statement, so the
20286+ # whole if-expression collapses to a C ternary.
2020820287 then_val = "0"
2020920288 then_t = "nil"
20210- body = @nd_body[nid]
2021120289 if body >= 0
2021220290 stmts = get_stmts(body)
2021320291 if stmts.length > 0
@@ -20217,7 +20295,6 @@
2021720295 end
2021820296 else_val = "0"
2021920297 else_t = "nil"
20220- sub = @nd_subsequent[nid]
2022120298 if sub >= 0
2022220299 if @nd_type[sub] == "ElseNode"
2022320300 eb = @nd_body[sub]