Commit 931b98e6

931b98e6276cMerged

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

Description

Lower self-recursive yield methods to forward the block as an sp_Proc

A method that uses implicit yield and forwards its block down a recursive call (countdown(n - 1) { yield }) dropped the block entirely: it can't use the inline-yield fast path (self-recursion) and the non-inline path forwarded NULL, so countdown(2) { "done" } returned 0.

Lower such methods to the &block form, reusing the now-working &block-recursion machinery (parts 1/4/5 + captured-&block + explicit recursion):

  • detect_lowered_yield_methods (analyze, run after the param-type rebuilders so the synthetic param survives) flags self-recursive yield methods, appends a synthetic __yblk__ proc param, and clears the yield-ABI flag. The registry rides the IR to codegen.
  • codegen emits yield in a lowered method as sp_proc_call(yblk), resolving yblk through fiber_var_ref so a forwarded { yield } proc literal uses its capture cell; that proc force-captures yblk (yield isn't a free-var read, so the scan never sees it).
  • the block-call type tracer treats a YieldNode as the governing block call, so the result is typed per call site (string / int).

countdown(2) { "done" } -> "done", countdown(3) { 42 } -> 42, at top level and in instance methods. This was the last open case.

Closes #1362

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

Details

Commit
931b98e6276c
Parent
e52f6798e3d9

compiler_helpers.rbmodifiedView file

@@ -119,6 +119,7 @@
119119 compiler_state_sa :iexec_block_pnames, :iexec_block_ptypes
120120 compiler_state_sa :attr_alias_keys, :attr_alias_ivars
121121 compiler_state_sa :cls_meth_blk_param_types
122+ compiler_state_sa :lowered_yield_methods
122123
123124 compiler_state_ia :meth_body_ids, :meth_rest_index, :meth_kwrest_index, :meth_has_yield, :cls_rest_idxs, :cls_is_value_type
124125 compiler_state_ia :cls_is_sra, :const_expr_ids, :const_mutated, :gvar_written, :module_body_ids, :ffi_buf_sizes, :ffi_reader_offsets
@@ -1068,6 +1069,35 @@
10681069 end
10691070 end
10701071
1072+ # Membership test for the self-recursive-yield registry (populated by
1073+ # analyze's detect_lowered_yield_methods, serialized to IR, read by
1074+ # both passes). `key` is "<ClassName>#<method>" / "#<method>".
1075+ def is_lowered_yield_method(key)
1076+ if @lowered_yield_methods == nil
1077+ return 0
1078+ end
1079+ if not_in(key, @lowered_yield_methods) == 0
1080+ return 1
1081+ end
1082+ 0
1083+ end
1084+
1085+ # True when the method currently being emitted/analyzed is a lowered
1086+ # self-recursive yield method (its `yield`s call the synthetic
1087+ # `__yblk__` proc param). Built from @current_class_idx /
1088+ # @current_method_name so it stays valid inside a forwarded proc
1089+ # literal body (those don't reset the enclosing-method state).
1090+ def current_method_is_lowered?
1091+ if @current_method_name == nil || @current_method_name == ""
1092+ return 0
1093+ end
1094+ cls = ""
1095+ if @current_class_idx >= 0 && @current_class_idx < @cls_names.length
1096+ cls = @cls_names[@current_class_idx]
1097+ end
1098+ is_lowered_yield_method(cls + "#" + @current_method_name)
1099+ end
1100+
10711101 # ---- Convenience: get stmts of a body node ----
10721102 def get_stmts(nid)
10731103 if nid < 0