Commit c7ce56e2
c7ce56e2a773Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): support anonymous & block forwarding (Ruby 3.1+)
def outer(&); inner(&); end declares an unnamed block parameter
and forwards it via the matching anonymous & in the call. Pre-fix,
two gaps dropped the block:
- collect_pnames_str read
@nd_name[blk]directly, so an anonymous BlockParameterNode (name = nil) registered as an empty pname — nolv_slot, no entry in pnames for find_block_param_name to spot. - block_forward_expr only handled BlockArgumentNodes whose
expression was a LocalVariableReadNode (the
&proc_varshape). The anonymous form (expression: -1) fell through to the no-block path.
Fix synthesizes the stable internal name __anon_block whenever
the BlockParameterNode is unnamed, so the param flows through
find_block_param_name and @current_method_block_param like a
regular &block. block_forward_expr gains a new branch that
detects BlockArgumentNode { expression < 0 } and forwards the
enclosing method's anon-block slot (lv___anon_block).
Test plan
- make bootstrap (gen2.c == gen3.c)
- make test passes (182 tests)
- test/anon_block_forward.rb covers top-level forwarding, anonymous-& with a prefix statement before the forward, and class-instance-method forwarding via typed-receiver dispatch.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- c7ce56e2a773
- Parent
- 04000f21a009
spinel_codegen.rbmodifiedView file
@@ -744,6 +744,17 @@
744744
@needs_proc = 1745745
return compile_proc_literal(nid)746746
end747+
# Anonymous `&` forwarding (Ruby 3.1+): `inner(&)` where the748+
# enclosing method declared `def outer(&)`. The BlockArgumentNode749+
# carries no expression, so `find_block_arg` returns -1; we forward750+
# the enclosing method's anon-block param directly.751+
blk = @nd_block[nid]752+
if blk >= 0 && @nd_type[blk] == "BlockArgumentNode" && @nd_expression[blk] < 0753+
if @current_method_block_param != ""754+
@needs_proc = 1755+
return "lv_" + @current_method_block_param756+
end757+
end747758
ba = find_block_arg(nid)748759
if ba >= 0749760
@needs_proc = 1@@ -4332,7 +4343,16 @@
43324343
if result != ""43334344
result = result + ","43344345
end4335-
result = result + @nd_name[blk]4346+
# Anonymous `&` (Ruby 3.1+) — `def m(&); inner(&); end` —4347+
# produces a BlockParameterNode with no name. Synthesize a4348+
# stable internal name so the param gets a proper `lv_` slot4349+
# and downstream lookups (find_block_param_name,4350+
# @current_method_block_param) work the same as for `&block`.4351+
bn = @nd_name[blk]4352+
if bn == ""4353+
bn = "__anon_block"4354+
end4355+
result = result + bn43364356
end43374357
end43384358
resulttest/anon_block_forward.rbaddedView file
Loading diff…