Commit f99002b2

f99002b2b5c4Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): EmbeddedStatementsNode uses last stmt value not first (#729)

"#{stmt1; stmt2; ...; stmtN}" used to interpolate stmt1's value and silently drop stmt2..stmtN, both their side effects and the intended interpolation result. Two call sites had stmts.first:

  • compile_expr's EmbeddedStatementsNode arm (used when the node appears standalone -- rare; mostly through compile_interpolated).
  • compile_interpolated's per-part EmbeddedStatementsNode arm (the hot path for "#{...}" literals).

Both now emit each leading statement via compile_stmt and take the final statement's value via compile_expr. Matches Ruby's statement-list-is-the-last-value semantics.

Test: test/interpolation_multistmt.rb covers the issue example (y = 10; y = 20; y -> "20", y persists as 20) plus a no-result interpolation with counter += 1 thrice (no puts of the string, just verifying counter reaches 3).

467 tests pass; optcarrot checksum 59662.

Closes #729

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
f99002b2b5c4
Parent
c7d1d1512aff

spinel_codegen.rbmodifiedView file

@@ -13451,11 +13451,20 @@
1345113451 return "0"
1345213452 end
1345313453 if t == "EmbeddedStatementsNode"
13454+ # `"#{expr1; expr2; ...; exprN}"` -- the interpolation's value
13455+ # is the LAST statement in Ruby; intermediate statements run for
13456+ # their side effects. Emit each leading stmt and return the
13457+ # tail's expression. Issue #729.
1345413458 body = @nd_body[nid]
1345513459 if body >= 0
1345613460 stmts = get_stmts(body)
1345713461 if stmts.length > 0
13458- return compile_expr(stmts.first)
13462+ k_es = 0
13463+ while k_es < stmts.length - 1
13464+ compile_stmt(stmts[k_es])
13465+ k_es = k_es + 1
13466+ end
13467+ return compile_expr(stmts[stmts.length - 1])
1345913468 end
1346013469 end
1346113470 return "0"
@@ -13919,7 +13928,14 @@
1391913928 if body >= 0
1392013929 stmts = get_stmts(body)
1392113930 if stmts.length > 0
13922- inner = stmts.first
13931+ # Emit each leading stmt for side effects; the
13932+ # interpolation's value is the LAST stmt. Issue #729.
13933+ k_es2 = 0
13934+ while k_es2 < stmts.length - 1
13935+ compile_stmt(stmts[k_es2])
13936+ k_es2 = k_es2 + 1
13937+ end
13938+ inner = stmts[stmts.length - 1]
1392313939 it = infer_type(inner)
1392413940 if it == "int"
1392513941 fmt = fmt + "%lld"