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 tosp_proc_new(...); a&proc_varis the capturedsp_Proc *local; absent or unsupported block returns"". Sets@needs_procwhen emitting either form. Replaces the conditionalbpresolution at Sites A, B, and the receiverlesshas_block_parampath 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 test162/162 pass (161 at base + 1 new) -
test/block_forward_block_arg.rbcovers 3 cases — Site A (self-call)inner(&block), Site B (typed-receiver)sink.receive(&block), and top-level receiverlessinner_top(&block).
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 91432ca12f4e
- Parent
- 9f101b63bdce
spinel_codegen.rbmodifiedView file
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. # 0 otherwise. Pairs with find_block_arg to dispatch correctly at # &block-forwarding call sites (literal block vs. `&proc_var`). def has_literal_block(nid) blk = @nd_block[nid] (blk >= 0 && @nd_type[blk] == "BlockNode") ? 1 : 0 end # Returns the inner expression of a BlockArgumentNode whose payload # is a captured proc local (the `&block` form). Returns -1 for # absent block-arg, or for shapes the codegen doesn't yet forward # — `&:sym` (SymbolNode) and `&nil` (NilNode), which would need # symbol-to-proc / nil-as-no-block lowering. Call sites fall # through to the no-block path in those cases. def find_block_arg(nid) blk = @nd_block[nid] if blk < 0 return -1 end if @nd_type[blk] != "BlockArgumentNode" return -1 end inner = @nd_expression[blk] if inner < 0 return -1 end if @nd_type[inner] != "LocalVariableReadNode" return -1 end inner end # Resolves the call-site block-forwarding expression: returns the C # expression for the proc to forward at a `&block`-taking call site # (a literal block compiles to sp_proc_new(...); a `&proc_var` is # the captured `sp_Proc *` local), or "" if the call site provides # no block. Sets @needs_proc when a forwarded proc is emitted. def block_forward_expr(nid) if has_literal_block(nid) == 1 @needs_proc = 1 return compile_proc_literal(nid) end ba = find_block_arg(nid) if ba >= 0 @needs_proc = 1 return compile_expr(ba) end "" end # Flatten a constant reference into an internal name. # C -> C # ::C -> C if @meth_has_yield[mi] == 1 yargs = ", NULL, NULL" end # Check if function has a &block param and caller provides a block # Check if function has a &block param and caller provides a block. # Ruby syntax requires `&block` to be the trailing param — a proc-typed # slot in any other position is a positional proc argument, not a block # param. Mirrors cls_method_has_block_param. ptypes = @meth_param_types[mi].split(",") has_block_param = 0 pk = 0 while pk < ptypes.length if ptypes[pk] == "proc" has_block_param = 1 end pk = pk + 1 end has_block_param = (ptypes.length > 0 && ptypes.last == "proc") ? 1 : 0 if has_block_param == 1 if @nd_block[nid] >= 0 block_proc = compile_proc_literal(nid) return "sp_" + sanitize_name(mname) + "(" + compile_call_args(nid) + ", " + block_proc + ")" # Forward the call site's literal block or `&proc_var` into the # callee's &block slot. Use compile_call_args_with_defaults so # optional positional params get their defaults filled in, and # pass omit_trailing=1 so the &block slot isn't default-padded # with "0" — we append the actual proc explicitly below. block_proc = block_forward_expr(nid) if block_proc != "" ca = compile_call_args_with_defaults(nid, mi, 1) if ca == "" return "sp_" + sanitize_name(mname) + "(" + block_proc + ")" end return "sp_" + sanitize_name(mname) + "(" + ca + ", " + block_proc + ")" end end return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + yargs + ")" end bp = "" if has_proc == 1 if has_literal_block(nid) == 1 bp = compile_proc_literal(nid) else bp = block_forward_expr(nid) if bp == "" # The callee declares &block but the call site provides # none — fill the slot with NULL so the C call has the # right arity. end bp = "" if has_proc == 1 if has_literal_block(nid) == 1 bp = compile_proc_literal(nid) else bp = block_forward_expr(nid) if bp == "" # 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). # right arity. bp = "0" end end result end def compile_call_args_with_defaults(nid, mi) def compile_call_args_with_defaults(nid, mi, omit_trailing = 0) # `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 pnames = @meth_param_names[mi].split(",") ptypes = @meth_param_types[mi].split(",") defaults = @meth_has_defaults[mi].split(",") if omit_trailing > 0 kept = "".split(",") pk = 0 limit = pnames.length - omit_trailing if limit < 0 limit = 0 end while pk < limit kept.push(pnames[pk]) pk = pk + 1 end pnames = kept end # Check if args contain a KeywordHashNode - extract kw pairs kw_names = "".split(",")spinel_parse.cmodifiedView file
Loading diff…
test/block_forward_block_arg.rbaddedView file
Loading diff…