Commit 62f81b61

62f81b616f65Merged

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

Description

fix(codegen): yield-bearing fn assigned in expression position (#396)

Fixes #396.

x = measure { ... } where measure uses yield previously emitted sp_measure(, NULL, NULL) -- empty leading slot + 2 trailing NULLs (one too many for the function's actual signature of (void (*_block)(...), void *_benv)). The stmt-form measure { ... } already worked because compile_call_stmt routes literal-block + yield-method to compile_yield_call_stmt's inline path; the expression-context counterpart was missing.

Adds compile_yield_call_expr: same setup as the stmt form (param locals, body locals, yield -> block-body splice via compile_stmt_with_block), but allocates a result temp typed as the function's return type and captures the body's last expression into it via compile_expr_remap. Yield-as-last, control-flow-as-last, and write-as-last fall back to plain compile_stmt_with_block; their result temp keeps its default value (good enough for the no-callers path; tightening these is a separate refinement).

compile_no_recv_call_expr routes to the new function when @meth_has_yield[mi] && has_literal_block(nid).

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
62f81b616f65
Parent
67d4dacb7392

spinel_codegen.rbmodifiedView file

@@ -24180,6 +24180,14 @@
2418024180 end
2418124181 mi = find_method_idx(mname)
2418224182 if mi >= 0
24183+ # Issue #396: yield-bearing fn called with a literal block in
24184+ # expression position (e.g. `x = measure { ... }`). The default
24185+ # `, NULL, NULL` yargs path below produces `sp_measure(, NULL, NULL)`
24186+ # which doesn't compile. Mirror compile_yield_call_stmt's inline
24187+ # path but capture the body's last expression into a result temp.
24188+ if @meth_has_yield[mi] == 1 && has_literal_block(nid) == 1
24189+ return compile_yield_call_expr(nid, mi)
24190+ end
2418324191 yargs = ""
2418424192 if @meth_has_yield[mi] == 1
2418524193 yargs = ", NULL, NULL"
@@ -39663,6 +39671,104 @@
3966339671 end
3966439672 end
3966539673
39674+ # Issue #396: expression-context counterpart of compile_yield_call_stmt.
39675+ # `x = measure { puts "block" }` where measure has `yield` in its body
39676+ # previously fell through to compile_no_recv_call_expr's default, which
39677+ # emitted `sp_measure(, NULL, NULL)` -- empty leading arg slot + 1 trailing
39678+ # NULL too many. Mirror the inlining the stmt-form does, but capture the
39679+ # body's last expression into a result temp and return that as the
39680+ # expression value.
39681+ def compile_yield_call_expr(nid, mi)
39682+ blk = @nd_block[nid]
39683+ if blk < 0
39684+ return "0"
39685+ end
39686+ bp_names = "".split(",")
39687+ bp = @nd_parameters[blk]
39688+ if bp >= 0
39689+ inner = @nd_parameters[bp]
39690+ if inner >= 0
39691+ reqs = parse_id_list(@nd_requireds[inner])
39692+ kbp = 0
39693+ while kbp < reqs.length
39694+ bp_names.push(@nd_name[reqs[kbp]])
39695+ kbp = kbp + 1
39696+ end
39697+ end
39698+ end
39699+ args_id_yc = @nd_arguments[nid]
39700+ arg_ids_yc = []
39701+ if args_id_yc >= 0
39702+ arg_ids_yc = get_args(args_id_yc)
39703+ end
39704+ pnames = @meth_param_names[mi].split(",")
39705+ ptypes = @meth_param_types[mi].split(",")
39706+ @block_counter = @block_counter + 1
39707+ suffix = "_y" + @block_counter.to_s
39708+ param_map_from = "".split(",")
39709+ param_map_to = "".split(",")
39710+ kp = 0
39711+ while kp < pnames.length
39712+ pt_yc = "int"
39713+ if kp < ptypes.length
39714+ pt_yc = ptypes[kp]
39715+ end
39716+ tname = pnames[kp] + suffix
39717+ val_yc = "0"
39718+ if kp < arg_ids_yc.length
39719+ val_yc = compile_expr(arg_ids_yc[kp])
39720+ end
39721+ emit(" " + c_type(pt_yc) + " lv_" + tname + " = " + val_yc + ";")
39722+ param_map_from.push(pnames[kp])
39723+ param_map_to.push(tname)
39724+ declare_var(tname, pt_yc)
39725+ kp = kp + 1
39726+ end
39727+ bid = @meth_body_ids[mi]
39728+ if bid >= 0
39729+ flocals_n = "".split(",")
39730+ flocals_t = "".split(",")
39731+ scan_locals(bid, flocals_n, flocals_t, pnames)
39732+ kf = 0
39733+ while kf < flocals_n.length
39734+ tname = flocals_n[kf] + suffix
39735+ emit(" " + c_type(flocals_t[kf]) + " lv_" + tname + " = " + c_default_val(flocals_t[kf]) + ";")
39736+ param_map_from.push(flocals_n[kf])
39737+ param_map_to.push(tname)
39738+ declare_var(tname, flocals_t[kf])
39739+ kf = kf + 1
39740+ end
39741+ end
39742+ rt_yc = @meth_return_types[mi]
39743+ result_tmp = new_temp
39744+ emit(" " + c_type(rt_yc) + " " + result_tmp + " = " + c_return_default(rt_yc) + ";")
39745+ if bid >= 0
39746+ stmts = get_stmts(bid)
39747+ n = stmts.length
39748+ ks = 0
39749+ while ks < n - 1
39750+ compile_stmt_with_block(stmts[ks], blk, bp_names, param_map_from, param_map_to)
39751+ ks = ks + 1
39752+ end
39753+ if n > 0
39754+ last_y = stmts.last
39755+ last_t = @nd_type[last_y]
39756+ if last_t == "YieldNode" || last_t == "IfNode" || last_t == "CaseNode" ||
39757+ last_t == "WhileNode" || last_t == "BeginNode" || last_t == "ReturnNode" ||
39758+ last_t == "LocalVariableWriteNode" || last_t == "InstanceVariableWriteNode" ||
39759+ last_t == "ClassVariableWriteNode" || last_t == "GlobalVariableWriteNode"
39760+ # Statement-shaped last expression: emit as stmt; result_tmp
39761+ # keeps its default value.
39762+ compile_stmt_with_block(last_y, blk, bp_names, param_map_from, param_map_to)
39763+ else
39764+ # Expression-shaped: capture the value via compile_expr_remap.
39765+ emit(" " + result_tmp + " = " + compile_expr_remap(last_y, param_map_from, param_map_to) + ";")
39766+ end
39767+ end
39768+ end
39769+ result_tmp
39770+ end
39771+
3966639772 def compile_stmt_with_block(nid, blk, bp_names, map_from, map_to)
3966739773 if nid < 0
3966839774 return