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
(pts.length > 0 && pts.last == "proc") ? 1 : 0 end # Instance/top-level counterpart of cls_method_has_block_param, # indexed by the flat method table position. A trailing proc-typed # slot is the method's `&block` capture. def meth_has_block_param(mi) pts = @meth_param_types[mi].split(",", -1) (pts.length > 0 && pts.last == "proc") ? 1 : 0 end # Returns the name of a method's `&block` parameter (the trailing # proc-typed slot in pnames), or "" if the method doesn't take # one. Ruby syntax requires `&block` to be the trailing param, so # `, NULL, NULL` yargs path below produces `sp_measure(, NULL, NULL)` # which doesn't compile. Mirror compile_yield_call_stmt's inline # path but capture the body's last expression into a result temp. if @meth_has_yield[mi] == 1 && has_literal_block(nid) == 1 if @meth_has_yield[mi] == 1 && has_literal_block(nid) == 1 && meth_has_block_param(mi) == 0 return compile_yield_call_expr(nid, mi) end yargs = "" block_proc = block_forward_expr(nid, blk_types_128) if block_proc != "" ca = compile_call_args_with_defaults(nid, mi, 1) # Append the trailing `_block, _benv` yield-ABI slots (yargs is # ", NULL, NULL" here) when the callee also uses block_given?/yield # — its C signature carries those slots in addition to the &block # param. The block reaches the body through the &block slot; # block_given? reads `lv_<blk> != NULL` and yield falls back to it, # so NULL yield slots are correct. if ca == "" return "sp_" + sanitize_name(mname) + "(" + block_proc + ")" return "sp_" + sanitize_name(mname) + "(" + block_proc + yargs + ")" end return "sp_" + sanitize_name(mname) + "(" + ca + ", " + block_proc + ")" return "sp_" + sanitize_name(mname) + "(" + ca + ", " + block_proc + yargs + ")" end end ca_ndef = compile_call_args_with_defaults(nid, mi) if recv < 0 mi = find_method_idx(mname) if mi >= 0 if @meth_has_yield[mi] == 1 # A method that captures its block as a named `&blk` param (and # only counts as a yield-method because it also calls # block_given?) must NOT be inlined: the inline path renames the # block local to `blk_y<N>` but leaves the body's `blk.call` # referencing the original `lv_blk`. Fall through to the regular # call path, which forwards the literal block into the &blk slot. if @meth_has_yield[mi] == 1 && meth_has_block_param(mi) == 0 compile_yield_call_stmt(nid, mi) return 1 end if midx < 0 return 0 end if cls_method_has_yield(cls_idx, midx) == 1 if cls_method_has_yield(cls_idx, midx) == 1 && cls_method_has_block_param(cls_idx, midx) == 0 compile_yield_method_call_stmt(nid, cls_idx, midx, mname) return 1 endtest/block_param_call_block_given.rbaddedView file
Loading diff…
test/block_param_call_block_given.rb.expectedaddedView file
Loading diff…