Commit 0d38bda5

0d38bda5b6d9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #122 from ryanseys/rs-block-forward-anon

feat(codegen): support anonymous & block forwarding (Ruby 3.1+)

Details

Commit
0d38bda5b6d9
Parent
ba4335ae239b
Merge parent
c7ce56e2a773

spinel_codegen.rbmodifiedView file

@@ -744,6 +744,17 @@
744744 @needs_proc = 1
745745 return compile_proc_literal(nid)
746746 end
747+ # Anonymous `&` forwarding (Ruby 3.1+): `inner(&)` where the
748+ # enclosing method declared `def outer(&)`. The BlockArgumentNode
749+ # carries no expression, so `find_block_arg` returns -1; we forward
750+ # the enclosing method's anon-block param directly.
751+ blk = @nd_block[nid]
752+ if blk >= 0 && @nd_type[blk] == "BlockArgumentNode" && @nd_expression[blk] < 0
753+ if @current_method_block_param != ""
754+ @needs_proc = 1
755+ return "lv_" + @current_method_block_param
756+ end
757+ end
747758 ba = find_block_arg(nid)
748759 if ba >= 0
749760 @needs_proc = 1
@@ -4332,7 +4343,16 @@
43324343 if result != ""
43334344 result = result + ","
43344345 end
4335- result = result + @nd_name[blk]
4346+ # Anonymous `&` (Ruby 3.1+) — `def m(&); inner(&); end` —
4347+ # produces a BlockParameterNode with no name. Synthesize a
4348+ # stable internal name so the param gets a proper `lv_` slot
4349+ # 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+ end
4355+ result = result + bn
43364356 end
43374357 end
43384358 result