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
return "0" end if t == "EmbeddedStatementsNode" # `"#{expr1; expr2; ...; exprN}"` -- the interpolation's value # is the LAST statement in Ruby; intermediate statements run for # their side effects. Emit each leading stmt and return the # tail's expression. Issue #729. body = @nd_body[nid] if body >= 0 stmts = get_stmts(body) if stmts.length > 0 return compile_expr(stmts.first) k_es = 0 while k_es < stmts.length - 1 compile_stmt(stmts[k_es]) k_es = k_es + 1 end return compile_expr(stmts[stmts.length - 1]) end end return "0" if body >= 0 stmts = get_stmts(body) if stmts.length > 0 inner = stmts.first # Emit each leading stmt for side effects; the # interpolation's value is the LAST stmt. Issue #729. k_es2 = 0 while k_es2 < stmts.length - 1 compile_stmt(stmts[k_es2]) k_es2 = k_es2 + 1 end inner = stmts[stmts.length - 1] it = infer_type(inner) if it == "int" fmt = fmt + "%lld"test/interpolation_multistmt.rbaddedView file
Loading diff…
test/interpolation_multistmt.rb.expectedaddedView file
Loading diff…