Commit c5849869

c58498694011Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~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:

  1. Plain self->iv_X references emitted with no self in scope at the call site. The inline scope's enclosing function might be main() or a sibling method whose self is the wrong type.

  2. 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 emits lv_i without the suffix -- undeclared in C.

  3. raise X unless Y inside 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
c58498694011
Parent
6192c45a2820

spinel_codegen.rbmodifiedView file

@@ -489,6 +489,10 @@
489489 @proc_captures = "".split(",")
490490 @proc_capture_types = "".split(",")
491491
492+ # Inline-rename map for compile_yield_method_call_stmt / _expr.
493+ @inline_rename_map_from = nil
494+ @inline_rename_map_to = nil
495+
492496 # Symbol type Phase 2 Step 1: intern table (infrastructure only; unused yet).
493497 @sym_names = "".split(",")
494498
@@ -9641,6 +9645,20 @@
96419645 # cells from outer functions are not visible here.
96429646 return "lv_" + name
96439647 end
9648+ # Issue #392 / #395: when inside an inlined yield-method body,
9649+ # local references go through this hot path. The inliner stashes
9650+ # its rename suffix in @inline_rename_map_from / _to so a CallNode
9651+ # whose recv falls through to compile_expr (instead of
9652+ # compile_expr_remap) still picks up the renamed local.
9653+ if @inline_rename_map_from != nil
9654+ ki = 0
9655+ while ki < @inline_rename_map_from.length
9656+ if @inline_rename_map_from[ki] == name
9657+ return "lv_" + @inline_rename_map_to[ki]
9658+ end
9659+ ki = ki + 1
9660+ end
9661+ end
96449662 cell = heap_promoted_cell(name)
96459663 if cell != "" && find_var_type(name) != ""
96469664 return "(*" + cell + ")"
@@ -26611,7 +26629,12 @@
2661126629 end
2661226630
2661326631 # Compile function body, replacing yield with block body
26614- # and renaming function locals to temp names
26632+ # and renaming function locals to temp names. Stash the rename
26633+ # map for fiber_var_ref (Issue #392 / #395).
26634+ saved_in_rmf = @inline_rename_map_from
26635+ saved_in_rmt = @inline_rename_map_to
26636+ @inline_rename_map_from = param_map_from
26637+ @inline_rename_map_to = param_map_to
2661526638 if bid >= 0
2661626639 stmts = get_stmts(bid)
2661726640 k = 0
@@ -26620,6 +26643,8 @@
2662026643 k = k + 1
2662126644 end
2662226645 end
26646+ @inline_rename_map_from = saved_in_rmf
26647+ @inline_rename_map_to = saved_in_rmt
2662326648 end
2662426649
2662526650 # Issue #396: expression-context counterpart of compile_yield_call_stmt.
@@ -26693,6 +26718,10 @@
2669326718 rt_yc = @meth_return_types[mi]
2669426719 result_tmp = new_temp
2669526720 emit(" " + c_type(rt_yc) + " " + result_tmp + " = " + c_return_default(rt_yc) + ";")
26721+ saved_in_rmf = @inline_rename_map_from
26722+ saved_in_rmt = @inline_rename_map_to
26723+ @inline_rename_map_from = param_map_from
26724+ @inline_rename_map_to = param_map_to
2669626725 if bid >= 0
2669726726 stmts = get_stmts(bid)
2669826727 n = stmts.length
@@ -26717,6 +26746,8 @@
2671726746 end
2671826747 end
2671926748 end
26749+ @inline_rename_map_from = saved_in_rmf
26750+ @inline_rename_map_to = saved_in_rmt
2672026751 result_tmp
2672126752 end
2672226753
@@ -26872,6 +26903,39 @@
2687226903 # but for simplicity, just skip
2687326904 return
2687426905 end
26906+ if t == "UnlessNode"
26907+ # Issue #392: `raise X unless Y` previously fell through to
26908+ # compile_expr_remap's default, which emitted the cond as a
26909+ # no-op ternary AND the raise unconditionally as a separate
26910+ # statement. Inline as `if (!cond) { body } [else { else }]`.
26911+ cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)
26912+ emit(" if (!(" + cond + ")) {")
26913+ @indent = @indent + 1
26914+ body = @nd_body[nid]
26915+ if body >= 0
26916+ bs = get_stmts(body)
26917+ bk = 0
26918+ while bk < bs.length
26919+ compile_stmt_with_block(bs[bk], blk, bp_names, map_from, map_to)
26920+ bk = bk + 1
26921+ end
26922+ end
26923+ @indent = @indent - 1
26924+ ec = @nd_else_clause[nid]
26925+ if ec >= 0
26926+ emit(" } else {")
26927+ @indent = @indent + 1
26928+ es = get_stmts(@nd_body[ec])
26929+ ek = 0
26930+ while ek < es.length
26931+ compile_stmt_with_block(es[ek], blk, bp_names, map_from, map_to)
26932+ ek = ek + 1
26933+ end
26934+ @indent = @indent - 1
26935+ end
26936+ emit(" }")
26937+ return
26938+ end
2687526939 # Default: for expression statements, use remap
2687626940 val = compile_expr_remap(nid, map_from, map_to)
2687726941 if val != "0"
@@ -26954,6 +27018,38 @@
2695427018 if mname == "!="
2695527019 return "(" + compile_expr_remap(recv, map_from, map_to) + " != " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
2695627020 end
27021+ # `[]` / `[]=` / `length` / `size` on a remapped local: compute
27022+ # the receiver's type so the dispatch picks the right runtime
27023+ # accessor, then emit with the remapped receiver expression.
27024+ # Issue #395: without this `arr[i]` inside the inlined body
27025+ # falls through to compile_expr below, which doesn't see the
27026+ # remap and emits `lv_i` instead of `lv_i_y1`.
27027+ if mname == "[]" || mname == "length" || mname == "size"
27028+ rt_r = infer_type(recv)
27029+ rc_r = compile_expr_remap(recv, map_from, map_to)
27030+ if rt_r == "str_array" && mname == "[]"
27031+ arg_r = compile_expr_remap_arg0(nid, map_from, map_to)
27032+ return "sp_StrArray_get(" + rc_r + ", " + arg_r + ")"
27033+ end
27034+ if rt_r == "int_array" && mname == "[]"
27035+ arg_r = compile_expr_remap_arg0(nid, map_from, map_to)
27036+ return "sp_IntArray_get(" + rc_r + ", " + arg_r + ")"
27037+ end
27038+ if rt_r == "float_array" && mname == "[]"
27039+ arg_r = compile_expr_remap_arg0(nid, map_from, map_to)
27040+ return "sp_FloatArray_get(" + rc_r + ", " + arg_r + ")"
27041+ end
27042+ if (rt_r == "str_array" || rt_r == "int_array" || rt_r == "sym_array") && (mname == "length" || mname == "size")
27043+ pfx_r = "IntArray"
27044+ if rt_r == "str_array"
27045+ pfx_r = "StrArray"
27046+ end
27047+ return "sp_" + pfx_r + "_length(" + rc_r + ")"
27048+ end
27049+ if rt_r == "float_array" && (mname == "length" || mname == "size")
27050+ return "sp_FloatArray_length(" + rc_r + ")"
27051+ end
27052+ end
2695727053 end
2695827054 end
2695927055 # Fallback: compile normally
@@ -27083,6 +27179,16 @@
2708327179 saved_ci = @current_class_idx
2708427180 @current_class_idx = cci
2708527181
27182+ # Issue #395: route ivar reads / writes that fall through to
27183+ # compile_expr (rather than through compile_expr_remap's remap
27184+ # table) to the receiver instead of the literal `self`. Without
27185+ # this, a `@keys` reference inside the inlined body would emit
27186+ # `self->iv_keys` -- but the inlining happens at a call site
27187+ # whose enclosing function may not have `self` declared (e.g.
27188+ # main()).
27189+ saved_self_override = @self_override
27190+ @self_override = rc
27191+
2708627192 @block_counter = @block_counter + 1
2708727193 suffix = "_y" + @block_counter.to_s
2708827194
@@ -27141,7 +27247,14 @@
2714127247 end
2714227248 end
2714327249
27144- # Compile the method body inline with yield -> block body
27250+ # Compile the method body inline with yield -> block body.
27251+ # Stash the rename map in @inline_rename_map_* so fiber_var_ref
27252+ # picks up the renamed locals when a CallNode falls through to
27253+ # compile_expr (Issue #392 / #395).
27254+ saved_in_rmf = @inline_rename_map_from
27255+ saved_in_rmt = @inline_rename_map_to
27256+ @inline_rename_map_from = map_from
27257+ @inline_rename_map_to = map_to
2714527258 if bid >= 0
2714627259 stmts = get_stmts(bid)
2714727260 k = 0
@@ -27150,7 +27263,10 @@
2715027263 k = k + 1
2715127264 end
2715227265 end
27266+ @inline_rename_map_from = saved_in_rmf
27267+ @inline_rename_map_to = saved_in_rmt
2715327268
27269+ @self_override = saved_self_override
2715427270 @current_class_idx = saved_ci
2715527271 end
2715627272