Commit 90c8e5c4

90c8e5c48b09Merged

Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): forward &block at self-call sites

m { ... } from inside another method of the same class silently dropped the literal block. The self-call dispatch path — compile_no_recv_call_expr's @current_class_idx >= 0 branch — always emitted sp_C_m(self, ca) with no block tail, so the block went nowhere and the binary segfaulted in sp_proc_call(NULL, ...).

Reproducer:

class Caller
  def kick
    inner { puts "hi" }
  end

  def inner(&block)
    block.call
  end
end

Caller.new.kick   # Ruby: hi  /  pre-fix Spinel: SIGSEGV

Symmetric to the typed-receiver fix; reuses has_literal_block, cls_method_has_block_param, build_call_tail, and the omit_trailing 4th arg of compile_typed_call_args. The new code is just the same bp/tail template adapted to the self-call form, which carries self instead of a typed-receiver C-temp.

Test plan

  • make bootstrap (gen2.c == gen3.c)
  • make test 161/161 pass (160 at base + 1 new)
  • test/block_forward_self_call.rb covers 3 cases — bare self-call, self-call passing arg through to block, self-call inside a loop forwarding a fresh block per iteration.

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

Details

Commit
90c8e5c48b09
Parent
848774d4ced4

spinel_codegen.rbmodifiedView file

@@ -13837,23 +13837,35 @@
1383713837 if cidx >= 0
1383813838 owner = find_method_owner(@current_class_idx, mname)
1383913839 # Look up the method's owning class so we can fill in defaults
13840- # from @cls_meth_defaults (issue #49).
13840+ # from @cls_meth_defaults (issue #49) and check for a &block slot.
1384113841 owner_ci = find_class_idx(owner)
1384213842 owner_midx = -1
1384313843 if owner_ci >= 0
1384413844 owner_midx = cls_find_method_direct(owner_ci, mname)
1384513845 end
13846+ # Omit the trailing &block slot from default-padding when the
13847+ # callee declares one — we'll fill it explicitly from the
13848+ # call site's literal block below.
13849+ has_proc = cls_method_has_block_param(owner_ci, owner_midx)
1384613850 ca = ""
1384713851 if owner_midx >= 0
13848- ca = compile_typed_call_args(nid, owner_ci, owner_midx, 0)
13852+ ca = compile_typed_call_args(nid, owner_ci, owner_midx, has_proc)
1384913853 else
1385013854 ca = compile_call_args(nid)
1385113855 end
13852- if ca != ""
13853- return "sp_" + owner + "_" + sanitize_name(mname) + "(self, " + ca + ")"
13854- else
13855- return "sp_" + owner + "_" + sanitize_name(mname) + "(self)"
13856+ bp = ""
13857+ if has_proc == 1
13858+ if has_literal_block(nid) == 1
13859+ @needs_proc = 1
13860+ bp = compile_proc_literal(nid)
13861+ else
13862+ # The callee declares &block but the call site provides
13863+ # none — fill the slot with NULL so the C call has the
13864+ # right arity.
13865+ bp = "0"
13866+ end
1385613867 end
13868+ return "sp_" + owner + "_" + sanitize_name(mname) + "(self" + build_call_tail(ca, bp) + ")"
1385713869 end
1385813870 # Check attr_readers (bare method call like `x` meaning self.x)
1385913871 readers = @cls_attr_readers[@current_class_idx].split(";")