Commit 91432ca1

91432ca12f4eMerged

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

Description

feat(codegen): forward &expr block-argument call args

g(&block) (a captured proc forwarded via &expr in call argument position) silently dropped the block at every forwarding call site. Prism emits this as PM_BLOCK_ARGUMENT_NODE, and the parser had no case for it — the codegen saw the call as taking no block at all, so the captured &block never reached the callee's &block slot.

Reproducer:

class Forwarder
  def outer(&block)
    inner(&block)
  end

  def inner(&block)
    block.call
  end
end

Forwarder.new.outer { puts "hi" }   # Ruby: hi  /  pre-fix Spinel: nothing

Parser. PM_BLOCK_ARGUMENT_NODE → "BlockArgumentNode" with the inner expression on R("expression", ...). Prism stores &expr in the CallNode's block slot (not arguments), so the codegen reads @nd_block[nid] and routes by node type. Because Prism never places a BlockArgumentNode inside an ArgumentsNode, no positional-arg filtering helper is needed — get_args(args_id) already excludes it.

Helpers.

  • find_block_arg(nid) returns the inner expression of a BlockArgumentNode whose payload is a captured proc local (LocalVariableReadNode). &:sym (SymbolNode) and &nil (NilNode) shapes return -1; the call site falls through to the no-block path in those cases — they would need symbol-to-proc / nil-as-no-block lowering and are out of scope.
  • block_forward_expr(nid) resolves the call-site block-forwarding expression to a single C expression: a literal block compiles to sp_proc_new(...); a &proc_var is the captured sp_Proc * local; absent or unsupported block returns "". Sets @needs_proc when emitting either form. Replaces the conditional bp resolution at Sites A, B, and the receiverless has_block_param path with a single ternary.

Site A, Site B, and the receiverless path all dispatch through block_forward_expr now — both literal blocks and &proc_var flow through the same lane. The receiverless path's gate generalizes from @nd_block[nid] >= 0 (which would mis-route BlockArgumentNode to compile_proc_literal) to checking block_forward_expr != "".

Receiverless has_block_param detection now mirrors cls_method_has_block_param and only inspects the trailing param slot — Ruby syntax requires &block to be the trailing param, so a proc-typed slot in any other position is a positional proc argument and must not trigger the block-forwarding lane. Pre-fix the loop ORed across every slot, which would have routed methods like def m(callback) (positional proc) through the forwarding tail when the call site provided a literal block, producing an arity-mismatched C call.

compile_call_args_with_defaults gains omit_trailing mirroring the existing parameter on compile_typed_call_args. The receiverless forwarding branch passes omit_trailing=1 so optional positional defaults still get filled in (issue #49 behavior preserved) while the &block slot is left for the caller to append explicitly. Pre-fix the branch used compile_call_args(nid), which would silently drop optional-with-defaults positional params and emit a wrong-arity call.

  • make bootstrap (gen2.c == gen3.c)
  • make test 162/162 pass (161 at base + 1 new)
  • test/block_forward_block_arg.rb covers 3 cases — Site A (self-call) inner(&block), Site B (typed-receiver) sink.receive(&block), and top-level receiverless inner_top(&block).

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

Details

Commit
91432ca12f4e
Parent
9f101b63bdce

spinel_codegen.rbmodifiedView file

@@ -697,13 +697,55 @@
697697 end
698698
699699 # Returns 1 if @nd_block[nid] is a literal BlockNode (do/end body),
700- # 0 otherwise. Used by &block-forwarding call sites to decide
701- # whether to emit a proc literal for the trailing block slot.
700+ # 0 otherwise. Pairs with find_block_arg to dispatch correctly at
701+ # &block-forwarding call sites (literal block vs. `&proc_var`).
702702 def has_literal_block(nid)
703703 blk = @nd_block[nid]
704704 (blk >= 0 && @nd_type[blk] == "BlockNode") ? 1 : 0
705705 end
706706
707+ # Returns the inner expression of a BlockArgumentNode whose payload
708+ # is a captured proc local (the `&block` form). Returns -1 for
709+ # absent block-arg, or for shapes the codegen doesn't yet forward
710+ # — `&:sym` (SymbolNode) and `&nil` (NilNode), which would need
711+ # symbol-to-proc / nil-as-no-block lowering. Call sites fall
712+ # through to the no-block path in those cases.
713+ def find_block_arg(nid)
714+ blk = @nd_block[nid]
715+ if blk < 0
716+ return -1
717+ end
718+ if @nd_type[blk] != "BlockArgumentNode"
719+ return -1
720+ end
721+ inner = @nd_expression[blk]
722+ if inner < 0
723+ return -1
724+ end
725+ if @nd_type[inner] != "LocalVariableReadNode"
726+ return -1
727+ end
728+ inner
729+ end
730+
731+ # Resolves the call-site block-forwarding expression: returns the C
732+ # expression for the proc to forward at a `&block`-taking call site
733+ # (a literal block compiles to sp_proc_new(...); a `&proc_var` is
734+ # the captured `sp_Proc *` local), or "" if the call site provides
735+ # no block. Sets @needs_proc when a forwarded proc is emitted.
736+ def block_forward_expr(nid)
737+ if has_literal_block(nid) == 1
738+ @needs_proc = 1
739+ return compile_proc_literal(nid)
740+ end
741+ ba = find_block_arg(nid)
742+ if ba >= 0
743+ @needs_proc = 1
744+ return compile_expr(ba)
745+ end
746+ ""
747+ end
748+
707749 # Flatten a constant reference into an internal name.
708750 # C -> C
709751 # ::C -> C
@@ -13520,20 +13562,25 @@
1352013562 if @meth_has_yield[mi] == 1
1352113563 yargs = ", NULL, NULL"
1352213564 end
13523- # Check if function has a &block param and caller provides a block
13565+ # Check if function has a &block param and caller provides a block.
13566+ # Ruby syntax requires `&block` to be the trailing param — a proc-typed
13567+ # slot in any other position is a positional proc argument, not a block
13568+ # param. Mirrors cls_method_has_block_param.
1352413569 ptypes = @meth_param_types[mi].split(",")
13525- has_block_param = 0
13526- pk = 0
13527- while pk < ptypes.length
13528- if ptypes[pk] == "proc"
13529- has_block_param = 1
13530- end
13531- pk = pk + 1
13532- end
13570+ has_block_param = (ptypes.length > 0 && ptypes.last == "proc") ? 1 : 0
1353313571 if has_block_param == 1
13534- if @nd_block[nid] >= 0
13535- block_proc = compile_proc_literal(nid)
13536- return "sp_" + sanitize_name(mname) + "(" + compile_call_args(nid) + ", " + block_proc + ")"
13572+ # Forward the call site's literal block or `&proc_var` into the
13573+ # callee's &block slot. Use compile_call_args_with_defaults so
13574+ # optional positional params get their defaults filled in, and
13575+ # pass omit_trailing=1 so the &block slot isn't default-padded
13576+ # with "0" — we append the actual proc explicitly below.
13577+ block_proc = block_forward_expr(nid)
13578+ if block_proc != ""
13579+ ca = compile_call_args_with_defaults(nid, mi, 1)
13580+ if ca == ""
13581+ return "sp_" + sanitize_name(mname) + "(" + block_proc + ")"
13582+ end
13583+ return "sp_" + sanitize_name(mname) + "(" + ca + ", " + block_proc + ")"
1353713584 end
1353813585 end
1353913586 return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")"
@@ -13625,9 +13672,8 @@
1362513672 end
1362613673 bp = ""
1362713674 if has_proc == 1
13628- if has_literal_block(nid) == 1
13629- bp = compile_proc_literal(nid)
13630- else
13675+ bp = block_forward_expr(nid)
13676+ if bp == ""
1363113677 # The callee declares &block but the call site provides
1363213678 # none — fill the slot with NULL so the C call has the
1363313679 # right arity.
@@ -16479,13 +16525,11 @@
1647916525 end
1648016526 bp = ""
1648116527 if has_proc == 1
16482- if has_literal_block(nid) == 1
16483- bp = compile_proc_literal(nid)
16484- else
16528+ bp = block_forward_expr(nid)
16529+ if bp == ""
1648516530 # The callee declares &block but the call site provides
1648616531 # none — fill the slot with NULL so the C call has the
16487- # right arity (compile_typed_call_args has already been
16488- # told to skip default-padding for this slot).
16532+ # right arity.
1648916533 bp = "0"
1649016534 end
1649116535 end
@@ -17109,7 +17153,11 @@
1710917153 result
1711017154 end
1711117155
17112- def compile_call_args_with_defaults(nid, mi)
17156+ def compile_call_args_with_defaults(nid, mi, omit_trailing = 0)
17157+ # `omit_trailing` is the number of trailing param slots to leave out
17158+ # entirely — block-forwarding call sites pass 1 so the &block slot
17159+ # isn't default-padded with "0" (the actual proc is appended by the
17160+ # caller after this returns).
1711317161 args_id = @nd_arguments[nid]
1711417162 arg_ids = []
1711517163 if args_id >= 0
@@ -17118,6 +17166,19 @@
1711817166 pnames = @meth_param_names[mi].split(",")
1711917167 ptypes = @meth_param_types[mi].split(",")
1712017168 defaults = @meth_has_defaults[mi].split(",")
17169+ if omit_trailing > 0
17170+ kept = "".split(",")
17171+ pk = 0
17172+ limit = pnames.length - omit_trailing
17173+ if limit < 0
17174+ limit = 0
17175+ end
17176+ while pk < limit
17177+ kept.push(pnames[pk])
17178+ pk = pk + 1
17179+ end
17180+ pnames = kept
17181+ end
1712117182
1712217183 # Check if args contain a KeywordHashNode - extract kw pairs
1712317184 kw_names = "".split(",")