Commit d9f94342
d9f943427aa2Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): support block_given? in &block-only methods
Previously block_given? only returned the right answer inside
methods detected as yield-using (@in_yield_method == 1). Methods
that captured the block solely via &block and never invoked yield
fell through to the constant "0" — losing the answer for the one
case where the runtime does know whether a block was forwarded.
Track the enclosing method's &block param name in
@current_method_block_param, set/restored at all three method-emit
sites (emit_instance_method, emit_class_level_method,
emit_toplevel_method) via a new find_block_param_name helper.
The helper checks the trailing proc-typed slot only — Ruby syntax
requires &block to be the trailing param, so a proc-typed slot in
any other position is a positional proc argument. Mirrors
cls_method_has_block_param's trailing-only invariant.
The block_given? handler now checks @current_method_block_param
before @in_yield_method. body_has_yield flags any method
containing block_given? as a yield-method, which would otherwise
route through the implicit _block slot — but a &block-declared
method binds the actual block to lv_<name>, not _block, so the
yield-slot lowering returns the wrong answer (always NULL) for
forwarded-block call sites. The reorder makes the explicit
&block param win when both signals are present.
-
make bootstrap(gen2.c == gen3.c) -
make test167/167 pass -
test/block_given_block_param.rbexercises top-leveldef m(&block)(with and without a regular leading param) called without a block — the receiverless default-padding path that transits this commit's lowering directly.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- d9f943427aa2
- Parent
- 9a2b3b9764dc
spinel_codegen.rbmodifiedView file
@current_method_name = "" @current_lexical_scope = "" @current_method_return = "" @current_method_block_param = "" @in_main = 0 @in_loop = 0 @hoisted_strlen_var = "" (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 # a proc-typed slot in any other position is a positional proc # argument. Mirrors cls_method_has_block_param's trailing-only # check. Used at method-emit time to set @current_method_block_param # so block_given? can resolve to (lv_<name> != NULL). def find_block_param_name(pnames, ptypes) if ptypes.length > 0 && ptypes.last == "proc" return pnames.last end "" end def cls_find_method_direct(ci, mname) mnames = @cls_meth_names[ci].split(";") j = 0 @current_class_idx = ci @current_method_name = mname @current_method_return = rt @current_method_block_param = find_block_param_name(pnames, ptypes) @indent = 1 @in_gc_scope = 0 pop_scope @current_class_idx = -1 @current_method_name = "" @current_method_block_param = "" @in_yield_method = 0 @indent = 0 emit_raw(" return " + c_return_default(rt) + ";") @current_class_idx = ci @current_method_name = mname @current_method_return = rt @current_method_block_param = find_block_param_name(pnames, ptypes) @indent = 1 @in_gc_scope = 0 pop_scope @current_class_idx = -1 @current_method_name = "" @current_method_block_param = "" @indent = 0 emit_raw(" return " + c_return_default(rt) + ";") emit_raw("}") pnames = @meth_param_names[mi].split(",") ptypes = @meth_param_types[mi].split(",") @current_method_block_param = find_block_param_name(pnames, ptypes) yp = "" if @meth_has_yield[mi] == 1 rt = @meth_return_types[mi] pop_scope @current_method_name = "" @current_method_block_param = "" @in_yield_method = 0 @indent = 0 emit_raw(" return " + c_return_default(rt) + ";") end end if mname == "block_given?" # &block parameter form takes priority: when the enclosing method # declares `def m(&block)`, the block is bound to `lv_block`, not # the implicit yield slot — so check the explicit param first. # `body_has_yield` flags any method containing block_given? as a # yield-method, which would otherwise route through the `_block` # slot and miss the actually-bound `&block` param. if @current_method_block_param != "" return "(lv_" + @current_method_block_param + " != NULL)" end if @in_yield_method == 1 return "(_block != NULL)" endtest/block_given_block_param.rbaddedView file
Loading diff…