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 test 167/167 pass
  • test/block_given_block_param.rb exercises top-level def 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

@@ -148,6 +148,7 @@
148148 @current_method_name = ""
149149 @current_lexical_scope = ""
150150 @current_method_return = ""
151+ @current_method_block_param = ""
151152 @in_main = 0
152153 @in_loop = 0
153154 @hoisted_strlen_var = ""
@@ -10008,6 +10009,20 @@
1000810009 (pts.length > 0 && pts.last == "proc") ? 1 : 0
1000910010 end
1001010011
10012+ # Returns the name of a method's `&block` parameter (the trailing
10013+ # proc-typed slot in pnames), or "" if the method doesn't take
10014+ # one. Ruby syntax requires `&block` to be the trailing param, so
10015+ # a proc-typed slot in any other position is a positional proc
10016+ # argument. Mirrors cls_method_has_block_param's trailing-only
10017+ # check. Used at method-emit time to set @current_method_block_param
10018+ # so block_given? can resolve to (lv_<name> != NULL).
10019+ def find_block_param_name(pnames, ptypes)
10020+ if ptypes.length > 0 && ptypes.last == "proc"
10021+ return pnames.last
10022+ end
10023+ ""
10024+ end
10025+
1001110026 def cls_find_method_direct(ci, mname)
1001210027 mnames = @cls_meth_names[ci].split(";")
1001310028 j = 0
@@ -10493,6 +10508,7 @@
1049310508 @current_class_idx = ci
1049410509 @current_method_name = mname
1049510510 @current_method_return = rt
10511+ @current_method_block_param = find_block_param_name(pnames, ptypes)
1049610512 @indent = 1
1049710513 @in_gc_scope = 0
1049810514
@@ -10555,6 +10571,7 @@
1055510571 pop_scope
1055610572 @current_class_idx = -1
1055710573 @current_method_name = ""
10574+ @current_method_block_param = ""
1055810575 @in_yield_method = 0
1055910576 @indent = 0
1056010577 emit_raw(" return " + c_return_default(rt) + ";")
@@ -10567,6 +10584,7 @@
1056710584 @current_class_idx = ci
1056810585 @current_method_name = mname
1056910586 @current_method_return = rt
10587+ @current_method_block_param = find_block_param_name(pnames, ptypes)
1057010588 @indent = 1
1057110589 @in_gc_scope = 0
1057210590
@@ -10591,6 +10609,7 @@
1059110609 pop_scope
1059210610 @current_class_idx = -1
1059310611 @current_method_name = ""
10612+ @current_method_block_param = ""
1059410613 @indent = 0
1059510614 emit_raw(" return " + c_return_default(rt) + ";")
1059610615 emit_raw("}")
@@ -10683,6 +10702,7 @@
1068310702
1068410703 pnames = @meth_param_names[mi].split(",")
1068510704 ptypes = @meth_param_types[mi].split(",")
10705+ @current_method_block_param = find_block_param_name(pnames, ptypes)
1068610706
1068710707 yp = ""
1068810708 if @meth_has_yield[mi] == 1
@@ -10744,6 +10764,7 @@
1074410764 rt = @meth_return_types[mi]
1074510765 pop_scope
1074610766 @current_method_name = ""
10767+ @current_method_block_param = ""
1074710768 @in_yield_method = 0
1074810769 @indent = 0
1074910770 emit_raw(" return " + c_return_default(rt) + ";")
@@ -13333,6 +13354,15 @@
1333313354 end
1333413355 end
1333513356 if mname == "block_given?"
13357+ # &block parameter form takes priority: when the enclosing method
13358+ # declares `def m(&block)`, the block is bound to `lv_block`, not
13359+ # the implicit yield slot — so check the explicit param first.
13360+ # `body_has_yield` flags any method containing block_given? as a
13361+ # yield-method, which would otherwise route through the `_block`
13362+ # slot and miss the actually-bound `&block` param.
13363+ if @current_method_block_param != ""
13364+ return "(lv_" + @current_method_block_param + " != NULL)"
13365+ end
1333613366 if @in_yield_method == 1
1333713367 return "(_block != NULL)"
1333813368 end