Commit e9d77ad8

e9d77ad83523Merged

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

Description

Forward a literal block into a &blk param that also uses block_given?

A method that captures its block as a named &blk parameter and also calls block_given? was misclassified as a yield-method and inlined at literal-block call sites. The yield-inline path renames the block local to blk_y<N> but leaves the body's blk.call referencing the original lv_blk, so the C build failed with 'lv_blk' undeclared.

Skip the yield-inline path when the callee declares a trailing &block param (top-level, instance, and singleton/class-method dispatch) and let the regular call path forward the literal block into that slot. Since such a method still carries the _block, _benv yield-ABI slots in its C signature (block_given? and yield both fall back to the &block param), pad those trailing slots with NULL at the forwarding call site.

Fixes #1316

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
e9d77ad83523
Parent
e294590a0b6e

spinel_codegen.rbmodifiedView file

@@ -11085,6 +11085,14 @@
1108511085 (pts.length > 0 && pts.last == "proc") ? 1 : 0
1108611086 end
1108711087
11088+ # Instance/top-level counterpart of cls_method_has_block_param,
11089+ # indexed by the flat method table position. A trailing proc-typed
11090+ # slot is the method's `&block` capture.
11091+ def meth_has_block_param(mi)
11092+ pts = @meth_param_types[mi].split(",", -1)
11093+ (pts.length > 0 && pts.last == "proc") ? 1 : 0
11094+ end
11095+
1108811096 # Returns the name of a method's `&block` parameter (the trailing
1108911097 # proc-typed slot in pnames), or "" if the method doesn't take
1109011098 # one. Ruby syntax requires `&block` to be the trailing param, so
@@ -19267,7 +19275,7 @@
1926719275 # `, NULL, NULL` yargs path below produces `sp_measure(, NULL, NULL)`
1926819276 # which doesn't compile. Mirror compile_yield_call_stmt's inline
1926919277 # path but capture the body's last expression into a result temp.
19270- if @meth_has_yield[mi] == 1 && has_literal_block(nid) == 1
19278+ if @meth_has_yield[mi] == 1 && has_literal_block(nid) == 1 && meth_has_block_param(mi) == 0
1927119279 return compile_yield_call_expr(nid, mi)
1927219280 end
1927319281 yargs = ""
@@ -19293,10 +19301,16 @@
1929319301 block_proc = block_forward_expr(nid, blk_types_128)
1929419302 if block_proc != ""
1929519303 ca = compile_call_args_with_defaults(nid, mi, 1)
19304+ # Append the trailing `_block, _benv` yield-ABI slots (yargs is
19305+ # ", NULL, NULL" here) when the callee also uses block_given?/yield
19306+ # — its C signature carries those slots in addition to the &block
19307+ # param. The block reaches the body through the &block slot;
19308+ # block_given? reads `lv_<blk> != NULL` and yield falls back to it,
19309+ # so NULL yield slots are correct.
1929619310 if ca == ""
19297- return "sp_" + sanitize_name(mname) + "(" + block_proc + ")"
19311+ return "sp_" + sanitize_name(mname) + "(" + block_proc + yargs + ")"
1929819312 end
19299- return "sp_" + sanitize_name(mname) + "(" + ca + ", " + block_proc + ")"
19313+ return "sp_" + sanitize_name(mname) + "(" + ca + ", " + block_proc + yargs + ")"
1930019314 end
1930119315 end
1930219316 ca_ndef = compile_call_args_with_defaults(nid, mi)
@@ -40622,7 +40636,13 @@
4062240636 if recv < 0
4062340637 mi = find_method_idx(mname)
4062440638 if mi >= 0
40625- if @meth_has_yield[mi] == 1
40639+ # A method that captures its block as a named `&blk` param (and
40640+ # only counts as a yield-method because it also calls
40641+ # block_given?) must NOT be inlined: the inline path renames the
40642+ # block local to `blk_y<N>` but leaves the body's `blk.call`
40643+ # referencing the original `lv_blk`. Fall through to the regular
40644+ # call path, which forwards the literal block into the &blk slot.
40645+ if @meth_has_yield[mi] == 1 && meth_has_block_param(mi) == 0
4062640646 compile_yield_call_stmt(nid, mi)
4062740647 return 1
4062840648 end
@@ -49275,7 +49295,7 @@
4927549295 if midx < 0
4927649296 return 0
4927749297 end
49278- if cls_method_has_yield(cls_idx, midx) == 1
49298+ if cls_method_has_yield(cls_idx, midx) == 1 && cls_method_has_block_param(cls_idx, midx) == 0
4927949299 compile_yield_method_call_stmt(nid, cls_idx, midx, mname)
4928049300 return 1
4928149301 end