Commit 621daeea

621daeea4cafMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~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
621daeea4caf
Parent
8645f0f330b9

spinel_codegen.rbmodifiedView file

@@ -10832,6 +10832,14 @@
1083210832 end
1083310833 mi = find_method_idx(mname)
1083410834 if mi >= 0
10835+ # Issue #396: yield-bearing fn called with a literal block in
10836+ # expression position (e.g. `x = measure { ... }`). The default
10837+ # `, NULL, NULL` yargs path below produces `sp_measure(, NULL, NULL)`
10838+ # which doesn't compile. Mirror compile_yield_call_stmt's inline
10839+ # path but capture the body's last expression into a result temp.
10840+ if @meth_has_yield[mi] == 1 && has_literal_block(nid) == 1
10841+ return compile_yield_call_expr(nid, mi)
10842+ end
1083510843 yargs = ""
1083610844 if @meth_has_yield[mi] == 1
1083710845 yargs = ", NULL, NULL"
@@ -26326,6 +26334,104 @@
2632626334 end
2632726335 end
2632826336
26337+ # Issue #396: expression-context counterpart of compile_yield_call_stmt.
26338+ # `x = measure { puts "block" }` where measure has `yield` in its body
26339+ # previously fell through to compile_no_recv_call_expr's default, which
26340+ # emitted `sp_measure(, NULL, NULL)` -- empty leading arg slot + 1 trailing
26341+ # NULL too many. Mirror the inlining the stmt-form does, but capture the
26342+ # body's last expression into a result temp and return that as the
26343+ # expression value.
26344+ def compile_yield_call_expr(nid, mi)
26345+ blk = @nd_block[nid]
26346+ if blk < 0
26347+ return "0"
26348+ end
26349+ bp_names = "".split(",")
26350+ bp = @nd_parameters[blk]
26351+ if bp >= 0
26352+ inner = @nd_parameters[bp]
26353+ if inner >= 0
26354+ reqs = parse_id_list(@nd_requireds[inner])
26355+ kbp = 0
26356+ while kbp < reqs.length
26357+ bp_names.push(@nd_name[reqs[kbp]])
26358+ kbp = kbp + 1
26359+ end
26360+ end
26361+ end
26362+ args_id_yc = @nd_arguments[nid]
26363+ arg_ids_yc = []
26364+ if args_id_yc >= 0
26365+ arg_ids_yc = get_args(args_id_yc)
26366+ end
26367+ pnames = @meth_param_names[mi].split(",")
26368+ ptypes = @meth_param_types[mi].split(",")
26369+ @block_counter = @block_counter + 1
26370+ suffix = "_y" + @block_counter.to_s
26371+ param_map_from = "".split(",")
26372+ param_map_to = "".split(",")
26373+ kp = 0
26374+ while kp < pnames.length
26375+ pt_yc = "int"
26376+ if kp < ptypes.length
26377+ pt_yc = ptypes[kp]
26378+ end
26379+ tname = pnames[kp] + suffix
26380+ val_yc = "0"
26381+ if kp < arg_ids_yc.length
26382+ val_yc = compile_expr(arg_ids_yc[kp])
26383+ end
26384+ emit(" " + c_type(pt_yc) + " lv_" + tname + " = " + val_yc + ";")
26385+ param_map_from.push(pnames[kp])
26386+ param_map_to.push(tname)
26387+ declare_var(tname, pt_yc)
26388+ kp = kp + 1
26389+ end
26390+ bid = @meth_body_ids[mi]
26391+ if bid >= 0
26392+ flocals_n = "".split(",")
26393+ flocals_t = "".split(",")
26394+ scan_locals(bid, flocals_n, flocals_t, pnames)
26395+ kf = 0
26396+ while kf < flocals_n.length
26397+ tname = flocals_n[kf] + suffix
26398+ emit(" " + c_type(flocals_t[kf]) + " lv_" + tname + " = " + c_default_val(flocals_t[kf]) + ";")
26399+ param_map_from.push(flocals_n[kf])
26400+ param_map_to.push(tname)
26401+ declare_var(tname, flocals_t[kf])
26402+ kf = kf + 1
26403+ end
26404+ end
26405+ rt_yc = @meth_return_types[mi]
26406+ result_tmp = new_temp
26407+ emit(" " + c_type(rt_yc) + " " + result_tmp + " = " + c_return_default(rt_yc) + ";")
26408+ if bid >= 0
26409+ stmts = get_stmts(bid)
26410+ n = stmts.length
26411+ ks = 0
26412+ while ks < n - 1
26413+ compile_stmt_with_block(stmts[ks], blk, bp_names, param_map_from, param_map_to)
26414+ ks = ks + 1
26415+ end
26416+ if n > 0
26417+ last_y = stmts.last
26418+ last_t = @nd_type[last_y]
26419+ if last_t == "YieldNode" || last_t == "IfNode" || last_t == "CaseNode" ||
26420+ last_t == "WhileNode" || last_t == "BeginNode" || last_t == "ReturnNode" ||
26421+ last_t == "LocalVariableWriteNode" || last_t == "InstanceVariableWriteNode" ||
26422+ last_t == "ClassVariableWriteNode" || last_t == "GlobalVariableWriteNode"
26423+ # Statement-shaped last expression: emit as stmt; result_tmp
26424+ # keeps its default value.
26425+ compile_stmt_with_block(last_y, blk, bp_names, param_map_from, param_map_to)
26426+ else
26427+ # Expression-shaped: capture the value via compile_expr_remap.
26428+ emit(" " + result_tmp + " = " + compile_expr_remap(last_y, param_map_from, param_map_to) + ";")
26429+ end
26430+ end
26431+ end
26432+ result_tmp
26433+ end
26434+
2632926435 def compile_stmt_with_block(nid, blk, bp_names, map_from, map_to)
2633026436 if nid < 0
2633126437 return