Commit 9aa3b3bb
9aa3b3bbdd3bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): inlined yield-method body — self / locals / unless
Fixes #392 and #395 (cluster).
compile_yield_method_call_stmt inlines the callee's body at the call site. Three bugs the inliner had with the body:
-
Plain
self->iv_Xreferences emitted with noselfin scope at the call site. The inline scope's enclosing function might bemain()or a sibling method whoseselfis the wrong type. -
Half-renamed locals: the declaration site emits
lv_i_y1(with the inline suffix) but a CallNode that falls through to compile_expr (e.g.arr[i],arr.length) still emitslv_iwithout the suffix -- undeclared in C. -
raise X unless Yinside the inlined body emitted the raise unconditionally, then a no-op ternary(!cond ? 0 : 0)referencing the non-renamed locals. UnlessNode wasn't in compile_stmt_with_block's dispatch table.
Three coordinated fixes:
A. compile_yield_method_call_stmt sets @self_override to the
receiver expression while walking the body, so the default
self_arrow resolves to the right pointer for ivar reads /
writes that fall through to compile_expr.
B. fiber_var_ref consults @inline_rename_map_from / _to (set by the inliner around the body walk) so any LocalVariableReadNode that hits compile_expr's plain path still picks up the renamed local.
C. compile_expr_remap learns [] / length / size arms for
typed-array receivers so the dispatch picks up the renamed
recv via remap rather than falling through to compile_expr.
D. compile_stmt_with_block gets a UnlessNode arm that emits
if (!(cond)) { body } with the same nested compile_stmt_with_block
walk, keeping the raise correctly scoped under the unless.
The string-yielding variant of #395 (block param k declared
mrb_int at main()'s outer scope) is a separate sub-bug about
block-param-type inference; out of scope here.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 9aa3b3bbdd3b
- Parent
- 776a69fd62c4
spinel_codegen.rbmodifiedView file
@proc_captures = "".split(",") @proc_capture_types = "".split(",") # Inline-rename map for compile_yield_method_call_stmt / _expr. @inline_rename_map_from = nil @inline_rename_map_to = nil # Symbol type Phase 2 Step 1: intern table (infrastructure only; unused yet). @sym_names = "".split(",") # cells from outer functions are not visible here. return "lv_" + name end # Issue #392 / #395: when inside an inlined yield-method body, # local references go through this hot path. The inliner stashes # its rename suffix in @inline_rename_map_from / _to so a CallNode # whose recv falls through to compile_expr (instead of # compile_expr_remap) still picks up the renamed local. if @inline_rename_map_from != nil ki = 0 while ki < @inline_rename_map_from.length if @inline_rename_map_from[ki] == name return "lv_" + @inline_rename_map_to[ki] end ki = ki + 1 end end cell = heap_promoted_cell(name) if cell != "" && find_var_type(name) != "" return "(*" + cell + ")" end # Compile function body, replacing yield with block body # and renaming function locals to temp names # and renaming function locals to temp names. Stash the rename # map for fiber_var_ref (Issue #392 / #395). saved_in_rmf = @inline_rename_map_from saved_in_rmt = @inline_rename_map_to @inline_rename_map_from = param_map_from @inline_rename_map_to = param_map_to if bid >= 0 stmts = get_stmts(bid) k = 0 k = k + 1 end end @inline_rename_map_from = saved_in_rmf @inline_rename_map_to = saved_in_rmt end # Issue #396: expression-context counterpart of compile_yield_call_stmt. rt_yc = @meth_return_types[mi] result_tmp = new_temp emit(" " + c_type(rt_yc) + " " + result_tmp + " = " + c_return_default(rt_yc) + ";") saved_in_rmf = @inline_rename_map_from saved_in_rmt = @inline_rename_map_to @inline_rename_map_from = param_map_from @inline_rename_map_to = param_map_to if bid >= 0 stmts = get_stmts(bid) n = stmts.length end end end @inline_rename_map_from = saved_in_rmf @inline_rename_map_to = saved_in_rmt result_tmp end # but for simplicity, just skip return end if t == "UnlessNode" # Issue #392: `raise X unless Y` previously fell through to # compile_expr_remap's default, which emitted the cond as a # no-op ternary AND the raise unconditionally as a separate # statement. Inline as `if (!cond) { body } [else { else }]`. cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to) emit(" if (!(" + cond + ")) {") @indent = @indent + 1 body = @nd_body[nid] if body >= 0 bs = get_stmts(body) bk = 0 while bk < bs.length compile_stmt_with_block(bs[bk], blk, bp_names, map_from, map_to) bk = bk + 1 end end @indent = @indent - 1 ec = @nd_else_clause[nid] if ec >= 0 emit(" } else {") @indent = @indent + 1 es = get_stmts(@nd_body[ec]) ek = 0 while ek < es.length compile_stmt_with_block(es[ek], blk, bp_names, map_from, map_to) ek = ek + 1 end @indent = @indent - 1 end emit(" }") return end # Default: for expression statements, use remap val = compile_expr_remap(nid, map_from, map_to) if val != "0" if mname == "!=" return "(" + compile_expr_remap(recv, map_from, map_to) + " != " + compile_expr_remap_arg0(nid, map_from, map_to) + ")" end # `[]` / `[]=` / `length` / `size` on a remapped local: compute # the receiver's type so the dispatch picks the right runtime # accessor, then emit with the remapped receiver expression. # Issue #395: without this `arr[i]` inside the inlined body # falls through to compile_expr below, which doesn't see the # remap and emits `lv_i` instead of `lv_i_y1`. if mname == "[]" || mname == "length" || mname == "size" rt_r = infer_type(recv) rc_r = compile_expr_remap(recv, map_from, map_to) if rt_r == "str_array" && mname == "[]" arg_r = compile_expr_remap_arg0(nid, map_from, map_to) return "sp_StrArray_get(" + rc_r + ", " + arg_r + ")" end if rt_r == "int_array" && mname == "[]" arg_r = compile_expr_remap_arg0(nid, map_from, map_to) return "sp_IntArray_get(" + rc_r + ", " + arg_r + ")" end if rt_r == "float_array" && mname == "[]" arg_r = compile_expr_remap_arg0(nid, map_from, map_to) return "sp_FloatArray_get(" + rc_r + ", " + arg_r + ")" end if (rt_r == "str_array" || rt_r == "int_array" || rt_r == "sym_array") && (mname == "length" || mname == "size") pfx_r = "IntArray" if rt_r == "str_array" pfx_r = "StrArray" end return "sp_" + pfx_r + "_length(" + rc_r + ")" end if rt_r == "float_array" && (mname == "length" || mname == "size") return "sp_FloatArray_length(" + rc_r + ")" end end end end # Fallback: compile normally saved_ci = @current_class_idx @current_class_idx = cci # Issue #395: route ivar reads / writes that fall through to # compile_expr (rather than through compile_expr_remap's remap # table) to the receiver instead of the literal `self`. Without # this, a `@keys` reference inside the inlined body would emit # `self->iv_keys` -- but the inlining happens at a call site # whose enclosing function may not have `self` declared (e.g. # main()). saved_self_override = @self_override @self_override = rc @block_counter = @block_counter + 1 suffix = "_y" + @block_counter.to_s end end # Compile the method body inline with yield -> block body # Compile the method body inline with yield -> block body. # Stash the rename map in @inline_rename_map_* so fiber_var_ref # picks up the renamed locals when a CallNode falls through to # compile_expr (Issue #392 / #395). saved_in_rmf = @inline_rename_map_from saved_in_rmt = @inline_rename_map_to @inline_rename_map_from = map_from @inline_rename_map_to = map_to if bid >= 0 stmts = get_stmts(bid) k = 0 k = k + 1 end end @inline_rename_map_from = saved_in_rmf @inline_rename_map_to = saved_in_rmt @self_override = saved_self_override @current_class_idx = saved_ci endtest/inlined_yield_self_locals.rbaddedView file
Loading diff…
test/inlined_yield_self_locals.rb.expectedaddedView file
Loading diff…