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:

  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
9aa3b3bbdd3b
Parent
776a69fd62c4

spinel_codegen.rbmodifiedView file

@@ -470,6 +470,10 @@
470470 @proc_captures = "".split(",")
471471 @proc_capture_types = "".split(",")
472472
473+ # Inline-rename map for compile_yield_method_call_stmt / _expr.
474+ @inline_rename_map_from = nil
475+ @inline_rename_map_to = nil
476+
473477 # Symbol type Phase 2 Step 1: intern table (infrastructure only; unused yet).
474478 @sym_names = "".split(",")
475479
@@ -23020,6 +23024,20 @@
2302023024 # cells from outer functions are not visible here.
2302123025 return "lv_" + name
2302223026 end
23027+ # Issue #392 / #395: when inside an inlined yield-method body,
23028+ # local references go through this hot path. The inliner stashes
23029+ # its rename suffix in @inline_rename_map_from / _to so a CallNode
23030+ # whose recv falls through to compile_expr (instead of
23031+ # compile_expr_remap) still picks up the renamed local.
23032+ if @inline_rename_map_from != nil
23033+ ki = 0
23034+ while ki < @inline_rename_map_from.length
23035+ if @inline_rename_map_from[ki] == name
23036+ return "lv_" + @inline_rename_map_to[ki]
23037+ end
23038+ ki = ki + 1
23039+ end
23040+ end
2302323041 cell = heap_promoted_cell(name)
2302423042 if cell != "" && find_var_type(name) != ""
2302523043 return "(*" + cell + ")"
@@ -39974,7 +39992,12 @@
3997439992 end
3997539993
3997639994 # Compile function body, replacing yield with block body
39977- # and renaming function locals to temp names
39995+ # and renaming function locals to temp names. Stash the rename
39996+ # map for fiber_var_ref (Issue #392 / #395).
39997+ saved_in_rmf = @inline_rename_map_from
39998+ saved_in_rmt = @inline_rename_map_to
39999+ @inline_rename_map_from = param_map_from
40000+ @inline_rename_map_to = param_map_to
3997840001 if bid >= 0
3997940002 stmts = get_stmts(bid)
3998040003 k = 0
@@ -39983,6 +40006,8 @@
3998340006 k = k + 1
3998440007 end
3998540008 end
40009+ @inline_rename_map_from = saved_in_rmf
40010+ @inline_rename_map_to = saved_in_rmt
3998640011 end
3998740012
3998840013 # Issue #396: expression-context counterpart of compile_yield_call_stmt.
@@ -40056,6 +40081,10 @@
4005640081 rt_yc = @meth_return_types[mi]
4005740082 result_tmp = new_temp
4005840083 emit(" " + c_type(rt_yc) + " " + result_tmp + " = " + c_return_default(rt_yc) + ";")
40084+ saved_in_rmf = @inline_rename_map_from
40085+ saved_in_rmt = @inline_rename_map_to
40086+ @inline_rename_map_from = param_map_from
40087+ @inline_rename_map_to = param_map_to
4005940088 if bid >= 0
4006040089 stmts = get_stmts(bid)
4006140090 n = stmts.length
@@ -40080,6 +40109,8 @@
4008040109 end
4008140110 end
4008240111 end
40112+ @inline_rename_map_from = saved_in_rmf
40113+ @inline_rename_map_to = saved_in_rmt
4008340114 result_tmp
4008440115 end
4008540116
@@ -40235,6 +40266,39 @@
4023540266 # but for simplicity, just skip
4023640267 return
4023740268 end
40269+ if t == "UnlessNode"
40270+ # Issue #392: `raise X unless Y` previously fell through to
40271+ # compile_expr_remap's default, which emitted the cond as a
40272+ # no-op ternary AND the raise unconditionally as a separate
40273+ # statement. Inline as `if (!cond) { body } [else { else }]`.
40274+ cond = compile_expr_remap(@nd_predicate[nid], map_from, map_to)
40275+ emit(" if (!(" + cond + ")) {")
40276+ @indent = @indent + 1
40277+ body = @nd_body[nid]
40278+ if body >= 0
40279+ bs = get_stmts(body)
40280+ bk = 0
40281+ while bk < bs.length
40282+ compile_stmt_with_block(bs[bk], blk, bp_names, map_from, map_to)
40283+ bk = bk + 1
40284+ end
40285+ end
40286+ @indent = @indent - 1
40287+ ec = @nd_else_clause[nid]
40288+ if ec >= 0
40289+ emit(" } else {")
40290+ @indent = @indent + 1
40291+ es = get_stmts(@nd_body[ec])
40292+ ek = 0
40293+ while ek < es.length
40294+ compile_stmt_with_block(es[ek], blk, bp_names, map_from, map_to)
40295+ ek = ek + 1
40296+ end
40297+ @indent = @indent - 1
40298+ end
40299+ emit(" }")
40300+ return
40301+ end
4023840302 # Default: for expression statements, use remap
4023940303 val = compile_expr_remap(nid, map_from, map_to)
4024040304 if val != "0"
@@ -40317,6 +40381,38 @@
4031740381 if mname == "!="
4031840382 return "(" + compile_expr_remap(recv, map_from, map_to) + " != " + compile_expr_remap_arg0(nid, map_from, map_to) + ")"
4031940383 end
40384+ # `[]` / `[]=` / `length` / `size` on a remapped local: compute
40385+ # the receiver's type so the dispatch picks the right runtime
40386+ # accessor, then emit with the remapped receiver expression.
40387+ # Issue #395: without this `arr[i]` inside the inlined body
40388+ # falls through to compile_expr below, which doesn't see the
40389+ # remap and emits `lv_i` instead of `lv_i_y1`.
40390+ if mname == "[]" || mname == "length" || mname == "size"
40391+ rt_r = infer_type(recv)
40392+ rc_r = compile_expr_remap(recv, map_from, map_to)
40393+ if rt_r == "str_array" && mname == "[]"
40394+ arg_r = compile_expr_remap_arg0(nid, map_from, map_to)
40395+ return "sp_StrArray_get(" + rc_r + ", " + arg_r + ")"
40396+ end
40397+ if rt_r == "int_array" && mname == "[]"
40398+ arg_r = compile_expr_remap_arg0(nid, map_from, map_to)
40399+ return "sp_IntArray_get(" + rc_r + ", " + arg_r + ")"
40400+ end
40401+ if rt_r == "float_array" && mname == "[]"
40402+ arg_r = compile_expr_remap_arg0(nid, map_from, map_to)
40403+ return "sp_FloatArray_get(" + rc_r + ", " + arg_r + ")"
40404+ end
40405+ if (rt_r == "str_array" || rt_r == "int_array" || rt_r == "sym_array") && (mname == "length" || mname == "size")
40406+ pfx_r = "IntArray"
40407+ if rt_r == "str_array"
40408+ pfx_r = "StrArray"
40409+ end
40410+ return "sp_" + pfx_r + "_length(" + rc_r + ")"
40411+ end
40412+ if rt_r == "float_array" && (mname == "length" || mname == "size")
40413+ return "sp_FloatArray_length(" + rc_r + ")"
40414+ end
40415+ end
4032040416 end
4032140417 end
4032240418 # Fallback: compile normally
@@ -40446,6 +40542,16 @@
4044640542 saved_ci = @current_class_idx
4044740543 @current_class_idx = cci
4044840544
40545+ # Issue #395: route ivar reads / writes that fall through to
40546+ # compile_expr (rather than through compile_expr_remap's remap
40547+ # table) to the receiver instead of the literal `self`. Without
40548+ # this, a `@keys` reference inside the inlined body would emit
40549+ # `self->iv_keys` -- but the inlining happens at a call site
40550+ # whose enclosing function may not have `self` declared (e.g.
40551+ # main()).
40552+ saved_self_override = @self_override
40553+ @self_override = rc
40554+
4044940555 @block_counter = @block_counter + 1
4045040556 suffix = "_y" + @block_counter.to_s
4045140557
@@ -40500,7 +40606,14 @@
4050040606 end
4050140607 end
4050240608
40503- # Compile the method body inline with yield -> block body
40609+ # Compile the method body inline with yield -> block body.
40610+ # Stash the rename map in @inline_rename_map_* so fiber_var_ref
40611+ # picks up the renamed locals when a CallNode falls through to
40612+ # compile_expr (Issue #392 / #395).
40613+ saved_in_rmf = @inline_rename_map_from
40614+ saved_in_rmt = @inline_rename_map_to
40615+ @inline_rename_map_from = map_from
40616+ @inline_rename_map_to = map_to
4050440617 if bid >= 0
4050540618 stmts = get_stmts(bid)
4050640619 k = 0
@@ -40509,7 +40622,10 @@
4050940622 k = k + 1
4051040623 end
4051140624 end
40625+ @inline_rename_map_from = saved_in_rmf
40626+ @inline_rename_map_to = saved_in_rmt
4051240627
40628+ @self_override = saved_self_override
4051340629 @current_class_idx = saved_ci
4051440630 end
4051540631