Commit 848774d4
848774d4ced4Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): forward &block at typed-receiver call sites
recv.m { ... } silently dropped the literal block — the call site
emitted sp_Cls_m(rc, 0) and the binary segfaulted inside
sp_proc_call(NULL, ...).
Reproducer:
class App
def run(&block)
block.call
end
end
App.new.run { puts "hi" } # Ruby: hi / pre-fix Spinel: SIGSEGV
The typed-receiver dispatch in compile_object_method_expr (the
"Site B" branch) had no &block forwarding logic at all — it always
emitted sp_Cls_m(rc, ca) regardless of whether the callee declared
&block.
Pieces
has_literal_block(nid) helper. Returns 1 when @nd_block[nid]
is a literal BlockNode (do/end body), 0 otherwise. Used by
&block-forwarding call sites to decide whether to emit a proc
literal for the trailing block slot.
cls_method_has_block_param(ci, midx) predicate. Mirrors the
existing cls_method_has_yield: returns 1 when the (ci, midx)
method declares any proc-typed param. Call sites use it to decide
whether to omit the trailing &block slot from default-padding.
compile_typed_call_args 4th arg omit_trailing. The issue-#49
default-padding logic emitted "0" for any omitted trailing param,
including the &block slot. The block-forwarding code would then
append the actual proc, producing sp_App_run(_t1, 0, sp_proc_new(...))
— three args where two were expected. Fix: callers compute has_proc
before ca and pass it as omit_trailing; the &block slot is
excluded from default-padding and filled explicitly by the
forwarding logic. The three non-block callers (current-class
self-call, int-class fallback, constructor path) pass 0.
build_call_tail(ca, bp) helper. Concatenates a comma-prefixed
tail from non-empty pieces — mirrors build_params_str. Replaces
the four-branch if/elsif tail construction at Site B.
Site B bp/tail template. compile_object_method_expr
precomputes has_proc, resolves the literal block via
has_literal_block, then concatenates (rc, ca, bp) via
build_call_tail.
Test plan
-
make bootstrap(gen2.c == gen3.c) -
make test160/160 pass (was 159 at base) -
test/block_forward_recv_typed.rbcovers 6 cases — basic receivered, return-value-used, mixed regular+&block, multiple block invocations, block-arg interpolation, value-used-twice.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 848774d4ced4
- Parent
- e7bbaf3efdd4
spinel_codegen.rbmodifiedView file
result end # Returns 1 if @nd_block[nid] is a literal BlockNode (do/end body), # 0 otherwise. Used by &block-forwarding call sites to decide # whether to emit a proc literal for the trailing block slot. def has_literal_block(nid) blk = @nd_block[nid] (blk >= 0 && @nd_type[blk] == "BlockNode") ? 1 : 0 end # Flatten a constant reference into an internal name. # C -> C # ::C -> C 0 end # Returns 1 if the (ci, midx) method declares a `&block` parameter, # 0 otherwise. Ruby syntax requires `&block` to be the trailing # param, so we check only the last slot — a proc-typed slot in any # other position is a positional proc argument, not a block param. # Mirrors cls_method_has_yield: call sites use it to decide whether # to omit the trailing &block slot from default-padding. def cls_method_has_block_param(ci, midx) if ci < 0 || midx < 0 return 0 end all_ptypes = @cls_meth_ptypes[ci].split("|") if midx >= all_ptypes.length return 0 end pts = all_ptypes[midx].split(",") (pts.length > 0 && pts.last == "proc") ? 1 : 0 end def cls_find_method_direct(ci, mname) mnames = @cls_meth_names[ci].split(";") j = 0 result end # Builds the trailing portion of a call-args list — each non-empty # piece prefixed with ", ", empties skipped. Returns "" when both # are empty. Mirrors build_params_str: callers concatenate the # result onto a self/recv prefix to form the full arg list. def build_call_tail(ca, bp) result = "" if ca != "" result = result + ", " + ca end if bp != "" result = result + ", " + bp end result end # ---- Emit top-level methods ---- def emit_toplevel_methods i = 0 end ca = "" if owner_midx >= 0 ca = compile_typed_call_args(nid, owner_ci, owner_midx) ca = compile_typed_call_args(nid, owner_ci, owner_midx, 0) else ca = compile_call_args(nid) end if oci2 >= 0 midx2 = cls_find_method_direct(oci2, mname) end # Omit the trailing &block slot from default-padding when the # callee declares one — we'll fill it explicitly from the # call site's literal block below. has_proc = cls_method_has_block_param(oci2, midx2) ca = "" if midx2 >= 0 ca = compile_typed_call_args(nid, oci2, midx2) ca = compile_typed_call_args(nid, oci2, midx2, has_proc) else ca = compile_call_args(nid) end if owner == cname if ca != "" return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")" bp = "" if has_proc == 1 if has_literal_block(nid) == 1 @needs_proc = 1 bp = compile_proc_literal(nid) else return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ")" # The callee declares &block but the call site provides # none — fill the slot with NULL so the C call has the # right arity (compile_typed_call_args has already been # told to skip default-padding for this slot). bp = "0" end end tail = build_call_tail(ca, bp) if owner == cname return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + tail + ")" else if ca != "" return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ", " + ca + ")" else return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ")" end return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + tail + ")" end end end end ca2 = "" if midx3 >= 0 ca2 = compile_typed_call_args(nid, oci3, midx3) ca2 = compile_typed_call_args(nid, oci3, midx3, 0) else ca2 = compile_call_args(nid) end if init_ci >= 0 init_idx = cls_find_method_direct(init_ci, "initialize") if init_idx >= 0 return compile_typed_call_args(nid, init_ci, init_idx) return compile_typed_call_args(nid, init_ci, init_idx, 0) end end return "" result end def compile_typed_call_args(nid, target_ci, target_midx) def compile_typed_call_args(nid, target_ci, target_midx, omit_trailing) # Like compile_call_args but casts arguments to match target method param # types AND fills in defaults from @cls_meth_defaults for trailing # parameters the caller omitted (issue #49). Returns "" only when the # method takes no parameters at all. # # `omit_trailing` is the number of trailing param slots to leave out # entirely — block-forwarding call sites pass 1 so the &block slot # isn't default-padded with "0" (the actual proc is appended by the # caller after this returns). args_id = @nd_arguments[nid] arg_ids = [] if args_id >= 0 if target_midx < all_defaults.length defaults = all_defaults[target_midx].split(",") end # Drop the trailing slots the caller will fill explicitly (the # &block slot when block-forwarding) — otherwise a surplus # positional arg would mis-cast against the omitted slot's type # and the loop's default-padding would emit "0" for the slot the # caller is about to fill itself. if omit_trailing > 0 kept = [] pk = 0 limit = ptypes.length - omit_trailing if limit < 0 limit = 0 end while pk < limit kept.push(ptypes[pk]) pk = pk + 1 end ptypes = kept end total = ptypes.length if arg_ids.length > total total = arg_ids.lengthtest/block_forward_recv_typed.rbaddedView file
Loading diff…