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 test 160/160 pass (was 159 at base)
  • test/block_forward_recv_typed.rb covers 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

@@ -695,6 +695,14 @@
695695 result
696696 end
697697
698+ # Returns 1 if @nd_block[nid] is a literal BlockNode (do/end body),
699+ # 0 otherwise. Used by &block-forwarding call sites to decide
700+ # whether to emit a proc literal for the trailing block slot.
701+ def has_literal_block(nid)
702+ blk = @nd_block[nid]
703+ (blk >= 0 && @nd_type[blk] == "BlockNode") ? 1 : 0
704+ end
705+
698706 # Flatten a constant reference into an internal name.
699707 # C -> C
700708 # ::C -> C
@@ -10260,6 +10268,24 @@
1026010268 0
1026110269 end
1026210270
10271+ # Returns 1 if the (ci, midx) method declares a `&block` parameter,
10272+ # 0 otherwise. Ruby syntax requires `&block` to be the trailing
10273+ # param, so we check only the last slot — a proc-typed slot in any
10274+ # other position is a positional proc argument, not a block param.
10275+ # Mirrors cls_method_has_yield: call sites use it to decide whether
10276+ # to omit the trailing &block slot from default-padding.
10277+ def cls_method_has_block_param(ci, midx)
10278+ if ci < 0 || midx < 0
10279+ return 0
10280+ end
10281+ all_ptypes = @cls_meth_ptypes[ci].split("|")
10282+ if midx >= all_ptypes.length
10283+ return 0
10284+ end
10285+ pts = all_ptypes[midx].split(",")
10286+ (pts.length > 0 && pts.last == "proc") ? 1 : 0
10287+ end
10288+
1026310289 def cls_find_method_direct(ci, mname)
1026410290 mnames = @cls_meth_names[ci].split(";")
1026510291 j = 0
@@ -10883,6 +10909,21 @@
1088310909 result
1088410910 end
1088510911
10912+ # Builds the trailing portion of a call-args list — each non-empty
10913+ # piece prefixed with ", ", empties skipped. Returns "" when both
10914+ # are empty. Mirrors build_params_str: callers concatenate the
10915+ # result onto a self/recv prefix to form the full arg list.
10916+ def build_call_tail(ca, bp)
10917+ result = ""
10918+ if ca != ""
10919+ result = result + ", " + ca
10920+ end
10921+ if bp != ""
10922+ result = result + ", " + bp
10923+ end
10924+ result
10925+ end
10926+
1088610927 # ---- Emit top-level methods ----
1088710928 def emit_toplevel_methods
1088810929 i = 0
@@ -13804,7 +13845,7 @@
1380413845 end
1380513846 ca = ""
1380613847 if owner_midx >= 0
13807- ca = compile_typed_call_args(nid, owner_ci, owner_midx)
13848+ ca = compile_typed_call_args(nid, owner_ci, owner_midx, 0)
1380813849 else
1380913850 ca = compile_call_args(nid)
1381013851 end
@@ -16672,24 +16713,34 @@
1667216713 if oci2 >= 0
1667316714 midx2 = cls_find_method_direct(oci2, mname)
1667416715 end
16716+ # Omit the trailing &block slot from default-padding when the
16717+ # callee declares one — we'll fill it explicitly from the
16718+ # call site's literal block below.
16719+ has_proc = cls_method_has_block_param(oci2, midx2)
1667516720 ca = ""
1667616721 if midx2 >= 0
16677- ca = compile_typed_call_args(nid, oci2, midx2)
16722+ ca = compile_typed_call_args(nid, oci2, midx2, has_proc)
1667816723 else
1667916724 ca = compile_call_args(nid)
1668016725 end
16681- if owner == cname
16682- if ca != ""
16683- return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")"
16726+ bp = ""
16727+ if has_proc == 1
16728+ if has_literal_block(nid) == 1
16729+ @needs_proc = 1
16730+ bp = compile_proc_literal(nid)
1668416731 else
16685- return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ")"
16732+ # The callee declares &block but the call site provides
16733+ # none — fill the slot with NULL so the C call has the
16734+ # right arity (compile_typed_call_args has already been
16735+ # told to skip default-padding for this slot).
16736+ bp = "0"
1668616737 end
16738+ end
16739+ tail = build_call_tail(ca, bp)
16740+ if owner == cname
16741+ return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + tail + ")"
1668716742 else
16688- if ca != ""
16689- return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ", " + ca + ")"
16690- else
16691- return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + ")"
16692- end
16743+ return "sp_" + owner + "_" + sanitize_name(mname) + "((sp_" + owner + " *)" + rc + tail + ")"
1669316744 end
1669416745 end
1669516746 end
@@ -16740,7 +16791,7 @@
1674016791 end
1674116792 ca2 = ""
1674216793 if midx3 >= 0
16743- ca2 = compile_typed_call_args(nid, oci3, midx3)
16794+ ca2 = compile_typed_call_args(nid, oci3, midx3, 0)
1674416795 else
1674516796 ca2 = compile_call_args(nid)
1674616797 end
@@ -17209,7 +17260,7 @@
1720917260 if init_ci >= 0
1721017261 init_idx = cls_find_method_direct(init_ci, "initialize")
1721117262 if init_idx >= 0
17212- return compile_typed_call_args(nid, init_ci, init_idx)
17263+ return compile_typed_call_args(nid, init_ci, init_idx, 0)
1721317264 end
1721417265 end
1721517266 return ""
@@ -17409,11 +17460,16 @@
1740917460 result
1741017461 end
1741117462
17412- def compile_typed_call_args(nid, target_ci, target_midx)
17463+ def compile_typed_call_args(nid, target_ci, target_midx, omit_trailing)
1741317464 # Like compile_call_args but casts arguments to match target method param
1741417465 # types AND fills in defaults from @cls_meth_defaults for trailing
1741517466 # parameters the caller omitted (issue #49). Returns "" only when the
1741617467 # method takes no parameters at all.
17468+ #
17469+ # `omit_trailing` is the number of trailing param slots to leave out
17470+ # entirely — block-forwarding call sites pass 1 so the &block slot
17471+ # isn't default-padded with "0" (the actual proc is appended by the
17472+ # caller after this returns).
1741717473 args_id = @nd_arguments[nid]
1741817474 arg_ids = []
1741917475 if args_id >= 0
@@ -17429,6 +17485,24 @@
1742917485 if target_midx < all_defaults.length
1743017486 defaults = all_defaults[target_midx].split(",")
1743117487 end
17488+ # Drop the trailing slots the caller will fill explicitly (the
17489+ # &block slot when block-forwarding) — otherwise a surplus
17490+ # positional arg would mis-cast against the omitted slot's type
17491+ # and the loop's default-padding would emit "0" for the slot the
17492+ # caller is about to fill itself.
17493+ if omit_trailing > 0
17494+ kept = []
17495+ pk = 0
17496+ limit = ptypes.length - omit_trailing
17497+ if limit < 0
17498+ limit = 0
17499+ end
17500+ while pk < limit
17501+ kept.push(ptypes[pk])
17502+ pk = pk + 1
17503+ end
17504+ ptypes = kept
17505+ end
1743217506 total = ptypes.length
1743317507 if arg_ids.length > total
1743417508 total = arg_ids.length