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
end mi = find_method_idx(mname) if mi >= 0 # Issue #396: yield-bearing fn called with a literal block in # expression position (e.g. `x = measure { ... }`). The default # `, NULL, NULL` yargs path below produces `sp_measure(, NULL, NULL)` # which doesn't compile. Mirror compile_yield_call_stmt's inline # path but capture the body's last expression into a result temp. if @meth_has_yield[mi] == 1 && has_literal_block(nid) == 1 return compile_yield_call_expr(nid, mi) end yargs = "" if @meth_has_yield[mi] == 1 yargs = ", NULL, NULL" end end # Issue #396: expression-context counterpart of compile_yield_call_stmt. # `x = measure { puts "block" }` where measure has `yield` in its body # previously fell through to compile_no_recv_call_expr's default, which # emitted `sp_measure(, NULL, NULL)` -- empty leading arg slot + 1 trailing # NULL too many. Mirror the inlining the stmt-form does, but capture the # body's last expression into a result temp and return that as the # expression value. def compile_yield_call_expr(nid, mi) blk = @nd_block[nid] if blk < 0 return "0" end bp_names = "".split(",") bp = @nd_parameters[blk] if bp >= 0 inner = @nd_parameters[bp] if inner >= 0 reqs = parse_id_list(@nd_requireds[inner]) kbp = 0 while kbp < reqs.length bp_names.push(@nd_name[reqs[kbp]]) kbp = kbp + 1 end end end args_id_yc = @nd_arguments[nid] arg_ids_yc = [] if args_id_yc >= 0 arg_ids_yc = get_args(args_id_yc) end pnames = @meth_param_names[mi].split(",") ptypes = @meth_param_types[mi].split(",") @block_counter = @block_counter + 1 suffix = "_y" + @block_counter.to_s param_map_from = "".split(",") param_map_to = "".split(",") kp = 0 while kp < pnames.length pt_yc = "int" if kp < ptypes.length pt_yc = ptypes[kp] end tname = pnames[kp] + suffix val_yc = "0" if kp < arg_ids_yc.length val_yc = compile_expr(arg_ids_yc[kp]) end emit(" " + c_type(pt_yc) + " lv_" + tname + " = " + val_yc + ";") param_map_from.push(pnames[kp]) param_map_to.push(tname) declare_var(tname, pt_yc) kp = kp + 1 end bid = @meth_body_ids[mi] if bid >= 0 flocals_n = "".split(",") flocals_t = "".split(",") scan_locals(bid, flocals_n, flocals_t, pnames) kf = 0 while kf < flocals_n.length tname = flocals_n[kf] + suffix emit(" " + c_type(flocals_t[kf]) + " lv_" + tname + " = " + c_default_val(flocals_t[kf]) + ";") param_map_from.push(flocals_n[kf]) param_map_to.push(tname) declare_var(tname, flocals_t[kf]) kf = kf + 1 end end rt_yc = @meth_return_types[mi] result_tmp = new_temp emit(" " + c_type(rt_yc) + " " + result_tmp + " = " + c_return_default(rt_yc) + ";") if bid >= 0 stmts = get_stmts(bid) n = stmts.length ks = 0 while ks < n - 1 compile_stmt_with_block(stmts[ks], blk, bp_names, param_map_from, param_map_to) ks = ks + 1 end if n > 0 last_y = stmts.last last_t = @nd_type[last_y] if last_t == "YieldNode" || last_t == "IfNode" || last_t == "CaseNode" || last_t == "WhileNode" || last_t == "BeginNode" || last_t == "ReturnNode" || last_t == "LocalVariableWriteNode" || last_t == "InstanceVariableWriteNode" || last_t == "ClassVariableWriteNode" || last_t == "GlobalVariableWriteNode" # Statement-shaped last expression: emit as stmt; result_tmp # keeps its default value. compile_stmt_with_block(last_y, blk, bp_names, param_map_from, param_map_to) else # Expression-shaped: capture the value via compile_expr_remap. emit(" " + result_tmp + " = " + compile_expr_remap(last_y, param_map_from, param_map_to) + ";") end end end result_tmp end def compile_stmt_with_block(nid, blk, bp_names, map_from, map_to) if nid < 0 returntest/yield_call_assigned_return.rbaddedView file
Loading diff…
test/yield_call_assigned_return.rb.expectedaddedView file
Loading diff…