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
yieldin 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
compiler_state_sa :iexec_block_pnames, :iexec_block_ptypes compiler_state_sa :attr_alias_keys, :attr_alias_ivars compiler_state_sa :cls_meth_blk_param_types compiler_state_sa :lowered_yield_methods compiler_state_ia :meth_body_ids, :meth_rest_index, :meth_kwrest_index, :meth_has_yield, :cls_rest_idxs, :cls_is_value_type compiler_state_ia :cls_is_sra, :const_expr_ids, :const_mutated, :gvar_written, :module_body_ids, :ffi_buf_sizes, :ffi_reader_offsets end end # Membership test for the self-recursive-yield registry (populated by # analyze's detect_lowered_yield_methods, serialized to IR, read by # both passes). `key` is "<ClassName>#<method>" / "#<method>". def is_lowered_yield_method(key) if @lowered_yield_methods == nil return 0 end if not_in(key, @lowered_yield_methods) == 0 return 1 end 0 end # True when the method currently being emitted/analyzed is a lowered # self-recursive yield method (its `yield`s call the synthetic # `__yblk__` proc param). Built from @current_class_idx / # @current_method_name so it stays valid inside a forwarded proc # literal body (those don't reset the enclosing-method state). def current_method_is_lowered? if @current_method_name == nil || @current_method_name == "" return 0 end cls = "" if @current_class_idx >= 0 && @current_class_idx < @cls_names.length cls = @cls_names[@current_class_idx] end is_lowered_yield_method(cls + "#" + @current_method_name) end # ---- Convenience: get stmts of a body node ---- def get_stmts(nid) if nid < 0spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/recursive_implicit_yield_value.rbaddedView file
Loading diff…
test/recursive_implicit_yield_value.rb.expectedaddedView file
Loading diff…