Commit b245e10b

b245e10baf54Merged

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

Description

fix(codegen): inline nested user-yield call inside inlined caller (#664)

outer { |x| yield x*2 } appearing inside the body of inner (where inner itself yields) produced unresolved lv_x and _block references in main: compile_stmt_with_block's CallNode arm only inlined each, so any other yield-using user method (here, outer) fell through to compile_expr_remap. That path emitted a real sp_outer(_block, _benv) call, but _block / _benv exist only in the function-shaped emission of outer; once inner was inlined into main, the outer-scope substitution had nowhere to source them.

Adds compile_nested_yield_call_inline (+ compile_nested_callee_stmt helper). When compile_stmt_with_block sees a CallNode-with-literal-block whose callee is a top-level method with yield in its body, it now inlines the callee body: yields in the callee body become "bind the call's block params, then run the call's do/end body" -- and yields inside that do/end body continue to forward to the surrounding caller's block (the blk / bp_names we already carry). The call-block param's destination LV is routed through remap_local against the outer caller's rename map so x resolves to the suffix-renamed x_y2 slot that scan_locals already declared.

Closes #664.

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

Details

Commit
b245e10baf54
Parent
ab824fbd4cc4

spinel_codegen.rbmodifiedView file

@@ -37564,6 +37564,23 @@
3756437564 compile_each_with_yield_inline(nid, blk, bp_names, map_from, map_to)
3756537565 return
3756637566 end
37567+ # Nested call to a user yield-method while we're already inlining
37568+ # the surrounding caller. The classic shape is `outer do |x| yield x*2 end`
37569+ # inside `inner`, where `inner` was inlined into main and `outer` is
37570+ # itself a yield-using top-level method. Pre-fix the call fell through
37571+ # to compile_expr_remap, which emitted a real `sp_outer(...)` call —
37572+ # but inlined locals like `lv_x_y2` were never reachable from that
37573+ # generated function. Inline outer's body in place; its `yield` becomes
37574+ # the do/end body, and any `yield` *inside* that do/end body still
37575+ # refers to the surrounding caller's block (the `blk` we already have).
37576+ # Issue #664.
37577+ if @nd_receiver[nid] < 0 && find_method_idx(mname2) >= 0
37578+ inner_mi = find_method_idx(mname2)
37579+ if inner_mi >= 0 && inner_mi < @meth_has_yield.length && @meth_has_yield[inner_mi] == 1
37580+ compile_nested_yield_call_inline(nid, inner_mi, blk, bp_names, map_from, map_to)
37581+ return
37582+ end
37583+ end
3756737584 end
3756837585 # Compile call with remapping for args
3756937586 val = compile_expr_remap(nid, map_from, map_to)
@@ -38089,6 +38106,153 @@
3808938106 @current_class_idx = saved_ci
3809038107 end
3809138108
38109+ # Two-level yield inline: `outer do |x| yield x*2 end` appearing
38110+ # inside an already-inlined caller (`inner`) where `outer` is itself a
38111+ # yield-using top-level method. The callee body's yield gets replaced
38112+ # by THIS call's literal block body (the do |x| ... end on this call),
38113+ # and the surrounding `blk` / `bp_names` / `map_from` / `map_to` carry
38114+ # the inner-most yields back to the original caller's block. Used by
38115+ # compile_stmt_with_block's CallNode arm. Issue #664.
38116+ def compile_nested_yield_call_inline(nid, callee_mi, outer_blk, outer_bp_names, map_from, map_to)
38117+ call_blk = @nd_block[nid]
38118+ if call_blk < 0
38119+ return
38120+ end
38121+ cb_names = "".split(",")
38122+ cb_params = @nd_parameters[call_blk]
38123+ if cb_params >= 0
38124+ cb_inner = @nd_parameters[cb_params]
38125+ if cb_inner >= 0
38126+ cb_reqs = parse_id_list(@nd_requireds[cb_inner])
38127+ kcb = 0
38128+ while kcb < cb_reqs.length
38129+ cb_names.push(@nd_name[cb_reqs[kcb]])
38130+ kcb = kcb + 1
38131+ end
38132+ end
38133+ end
38134+ # Bind callee's params from the call's args. Args are evaluated in the
38135+ # currently-inlined scope so they go through compile_expr_remap. The
38136+ # callee's locals get a unique suffix to avoid colliding with outer
38137+ # scope names (e.g. `lv_x` both from `inner` body and `outer`).
38138+ pnames_n = @meth_param_names[callee_mi].split(",")
38139+ ptypes_n = @meth_param_types[callee_mi].split(",")
38140+ args_id_n = @nd_arguments[nid]
38141+ arg_ids_n = []
38142+ if args_id_n >= 0
38143+ arg_ids_n = get_args(args_id_n)
38144+ end
38145+ @block_counter = @block_counter + 1
38146+ suffix_n = "_n" + @block_counter.to_s
38147+ nested_from = "".split(",")
38148+ nested_to = "".split(",")
38149+ kp_n = 0
38150+ while kp_n < pnames_n.length
38151+ pt_n = "int"
38152+ if kp_n < ptypes_n.length
38153+ pt_n = ptypes_n[kp_n]
38154+ end
38155+ tname_n = pnames_n[kp_n] + suffix_n
38156+ val_n = c_default_val(pt_n)
38157+ if kp_n < arg_ids_n.length
38158+ val_n = compile_expr_remap(arg_ids_n[kp_n], map_from, map_to)
38159+ end
38160+ emit(" " + c_type(pt_n) + " lv_" + tname_n + " = " + val_n + ";")
38161+ nested_from.push(pnames_n[kp_n])
38162+ nested_to.push(tname_n)
38163+ declare_var(tname_n, pt_n)
38164+ kp_n = kp_n + 1
38165+ end
38166+ # Declare the call-block's params under their original names so the
38167+ # callee body's `yield x*2` resolves x in this scope. They were
38168+ # already added to scope_names by scan_locals on the inlined caller,
38169+ # so we don't re-declare lv_x — just route through nested_from/to so
38170+ # the value lookup in compile_stmt_with_block lands on the right name.
38171+ bid_n = @meth_body_ids[callee_mi]
38172+ if bid_n >= 0
38173+ flocals_n = "".split(",")
38174+ flocals_t = "".split(",")
38175+ sn_n = @nd_scope_names[bid_n]
38176+ if sn_n != ""
38177+ flocals_n = sn_n.split("|")
38178+ flocals_t = @nd_scope_types[bid_n].split("|")
38179+ end
38180+ kf_n = 0
38181+ while kf_n < flocals_n.length
38182+ tname_lv_n = flocals_n[kf_n] + suffix_n
38183+ emit(" " + c_type(flocals_t[kf_n]) + " lv_" + tname_lv_n + " = " + c_default_val(flocals_t[kf_n]) + ";")
38184+ nested_from.push(flocals_n[kf_n])
38185+ nested_to.push(tname_lv_n)
38186+ declare_var(tname_lv_n, flocals_t[kf_n])
38187+ declare_var(flocals_n[kf_n], flocals_t[kf_n])
38188+ kf_n = kf_n + 1
38189+ end
38190+ end
38191+ # Walk callee body. A YieldNode in the callee body becomes "bind
38192+ # call-block params + run call-block body". Within call-block body,
38193+ # any further YieldNode refers to the *original* outer block, so we
38194+ # pass outer_blk + outer_bp_names + map_from + map_to through.
38195+ if bid_n >= 0
38196+ stmts_n = get_stmts(bid_n)
38197+ ks_n = 0
38198+ while ks_n < stmts_n.length
38199+ compile_nested_callee_stmt(stmts_n[ks_n], call_blk, cb_names,
38200+ nested_from, nested_to,
38201+ outer_blk, outer_bp_names, map_from, map_to)
38202+ ks_n = ks_n + 1
38203+ end
38204+ end
38205+ end
38206+
38207+ # Helper for compile_nested_yield_call_inline. Walks the callee body
38208+ # (e.g. outer's `yield 1`) translating a YieldNode into the call's
38209+ # do/end body, and routing other statements through compile_stmt_with_block
38210+ # so their LV reads see nested_from/to renames.
38211+ def compile_nested_callee_stmt(nid, call_blk, cb_names,
38212+ nested_from, nested_to,
38213+ outer_blk, outer_bp_names, map_from, map_to)
38214+ if nid < 0
38215+ return
38216+ end
38217+ if @nd_type[nid] == "YieldNode"
38218+ # Bind call-block params from yield args (evaluated in the nested
38219+ # callee scope, so use nested_from/to for renaming). The block
38220+ # param's storage name in the surrounding inlined caller goes
38221+ # through map_from/to — `x` in the source becomes `x_y2` in main
38222+ # after scan_locals' rename, so we must route the destination
38223+ # through remap_local too.
38224+ yargs = @nd_arguments[nid]
38225+ if yargs >= 0
38226+ yaids = get_args(yargs)
38227+ yk = 0
38228+ while yk < yaids.length
38229+ if yk < cb_names.length
38230+ v_y = compile_expr_remap(yaids[yk], nested_from, nested_to)
38231+ dst = remap_local(cb_names[yk], map_from, map_to)
38232+ emit(" lv_" + dst + " = " + v_y + ";")
38233+ end
38234+ yk = yk + 1
38235+ end
38236+ end
38237+ # Run the call's do/end body. yields inside it refer to the
38238+ # original outer block, so use outer_blk / outer_bp_names /
38239+ # map_from / map_to (the surrounding inlined caller's scope).
38240+ cb_body = @nd_body[call_blk]
38241+ if cb_body >= 0
38242+ cb_stmts = get_stmts(cb_body)
38243+ kb = 0
38244+ while kb < cb_stmts.length
38245+ compile_stmt_with_block(cb_stmts[kb], outer_blk, outer_bp_names, map_from, map_to)
38246+ kb = kb + 1
38247+ end
38248+ end
38249+ return
38250+ end
38251+ # Non-yield statements in callee body: emit through the standard
38252+ # walker but with nested_from/to so LV reads see the suffix rename.
38253+ compile_stmt_with_block(nid, call_blk, cb_names, nested_from, nested_to)
38254+ end
38255+
3809238256 def compile_each_with_yield_inline(nid, outer_blk, outer_bp_names, map_from, map_to)
3809338257 # An each call on an array inside an inlined yield function
3809438258 # The each block contains yield statements that should be replaced with outer block body