Commit 2374f028

2374f028338dMerged

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

Description

Return a recursive &block-forwarding call's value at the block's type

A method that forwards its &block down a recursive call (countdown(n - 1) { blk.call }) dropped the result twice over: the block-bearing tail call was emitted for side-effect (stmt; return default), and the type tracer only inspected the tail (the recursive call), missing the governing blk.call in the base case.

  • Route a block-bearing tail call that resolves to a user &block-param method through compile_expr + return, so its value is the method's return value. yield-using methods and builtin iterators (each / times) keep the existing inline / side-effect path -- rerouting yield-method literal-block calls broke their block emit.
  • Extend the block-call tracer to scan the body's return points (not just the tail) for the governing return blk.call, so a recursive method's base case types the per-call-site result.

countdown(2) { "done" } (explicit &block) now returns "done". Implicit-yield recursion (countdown(n) { yield }, no &block param) is still open: it needs the yield->sp_Proc lowering, a separate change.

Refs #1362

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

Details

Commit
2374f028338d
Parent
24a8d574b037

spinel_codegen.rbmodifiedView file

@@ -51839,6 +51839,58 @@
5183951839 rt_inf
5184051840 end
5184151841
51842+ # True when `nid` is a CallNode that resolves to a user-defined method
51843+ # with a trailing `&block` param (top-level, a self-call in the current
51844+ # class, or a method on an obj-typed receiver). Restricted to &block-
51845+ # param methods: those forward the literal block as an sp_Proc and
51846+ # carry blk.call's value out, so a block-bearing TAIL call to one is
51847+ # the method's return value (e.g. the recursive `countdown(n - 1) { ...
51848+ # }`) and must not be dropped. yield-using methods are deliberately
51849+ # excluded -- their literal-block tail call is already inlined by the
51850+ # statement path, and rerouting it through compile_expr changes the
51851+ # block's emit (it broke yield_method_call_in_method_body). Builtin
51852+ # iterators (`each` / `times`) also fall through to the drop path.
51853+ def block_bearing_tail_is_user_call(nid)
51854+ if @nd_type[nid] != "CallNode"
51855+ return 0
51856+ end
51857+ mname = @nd_name[nid]
51858+ recv = @nd_receiver[nid]
51859+ if recv < 0
51860+ mi = find_method_idx(mname)
51861+ if mi >= 0
51862+ return meth_has_block_param(mi)
51863+ end
51864+ if @current_class_idx >= 0
51865+ owner_sc = find_method_owner(@current_class_idx, mname)
51866+ if owner_sc != ""
51867+ oci_sc = find_class_idx(owner_sc)
51868+ midx_sc = oci_sc >= 0 ? cls_find_method_direct(oci_sc, mname) : -1
51869+ if oci_sc >= 0 && midx_sc >= 0
51870+ return cls_method_has_block_param(oci_sc, midx_sc)
51871+ end
51872+ end
51873+ end
51874+ return 0
51875+ end
51876+ rt = base_type(infer_type(recv))
51877+ if is_obj_type(rt) == 1
51878+ cn = rt[4, rt.length - 4]
51879+ cci = find_class_idx(cn)
51880+ if cci >= 0
51881+ owner_rc = find_method_owner(cci, mname)
51882+ if owner_rc != ""
51883+ oci_rc = find_class_idx(owner_rc)
51884+ midx_rc = oci_rc >= 0 ? cls_find_method_direct(oci_rc, mname) : -1
51885+ if oci_rc >= 0 && midx_rc >= 0
51886+ return cls_method_has_block_param(oci_rc, midx_rc)
51887+ end
51888+ end
51889+ end
51890+ end
51891+ 0
51892+ end
51893+
5184251894 # Find the `vname = proc { ... }` / `vname = lambda { ... }` assignment
5184351895 # in body `bid` and return the proc-literal CallNode id (or -1). Used
5184451896 # to trace `pv.call` back to the proc body so a method returning a
@@ -51861,14 +51913,51 @@
5186151913 -1
5186251914 end
5186351915
51864- # Locate the `<bpname>.call(...)` whose value is the return of the
51865- # method whose tail lives in `stmts_body_id`, following a proc-var
51866- # indirection (`p = proc { blk.call }; p.call`) up to a small depth.
51867- # `decl_body_id` is the method body where proc-var assignments live
51868- # (it stays fixed as we recurse into proc bodies, since the procs are
51869- # declared there). Returns the CallNode id or -1.
51916+ # Is `expr` a governing `<bpname>.call(...)` — directly, or through a
51917+ # `pv = proc { ... blk.call ... }` proc-var indirection (the proc
51918+ # literal's own tail is followed)? `decl_body_id` is the method body
51919+ # where proc-var assignments live. Returns the CallNode id or -1.
51920+ def check_block_call_expr(expr, bpname, decl_body_id, depth)
51921+ if expr < 0 || depth > 4
51922+ return -1
51923+ end
51924+ if @nd_type[expr] != "CallNode" || @nd_name[expr] != "call"
51925+ return -1
51926+ end
51927+ r = @nd_receiver[expr]
51928+ if r < 0 || @nd_type[r] != "LocalVariableReadNode"
51929+ return -1
51930+ end
51931+ if @nd_name[r] == bpname
51932+ return expr
51933+ end
51934+ # `pv.call` where pv is a local proc capturing the block — descend
51935+ # into pv's proc body tail and keep looking for the governing blk.call.
51936+ plit = find_proc_literal_assign(decl_body_id, @nd_name[r])
51937+ if plit >= 0 && @nd_block[plit] >= 0
51938+ pst = get_stmts(@nd_body[@nd_block[plit]])
51939+ if pst.length > 0
51940+ ptail = pst.last
51941+ if @nd_type[ptail] == "ReturnNode"
51942+ pra = @nd_arguments[ptail]
51943+ if pra >= 0
51944+ pras = get_args(pra)
51945+ if pras.length == 1
51946+ ptail = pras[0]
51947+ end
51948+ end
51949+ end
51950+ return check_block_call_expr(ptail, bpname, decl_body_id, depth + 1)
51951+ end
51952+ end
51953+ -1
51954+ end
51955+
51956+ # Tail-position trace: the method's last statement (optionally wrapped
51957+ # in `return`), following the proc-var indirection. Returns the
51958+ # governing blk.call CallNode id or -1.
5187051959 def trace_block_call_return(stmts_body_id, bpname, decl_body_id, depth)
51871- if depth > 4 || stmts_body_id < 0
51960+ if stmts_body_id < 0
5187251961 return -1
5187351962 end
5187451963 st = get_stmts(stmts_body_id)
@@ -51885,30 +51974,87 @@
5188551974 end
5188651975 end
5188751976 end
51888- if @nd_type[last] == "CallNode" && @nd_name[last] == "call"
51889- r = @nd_receiver[last]
51890- if r >= 0 && @nd_type[r] == "LocalVariableReadNode"
51891- if @nd_name[r] == bpname
51892- return last
51977+ check_block_call_expr(last, bpname, decl_body_id, 0)
51978+ end
51979+
51980+ # Scan `nid` for a `return <bpname>.call` in a non-tail position — the
51981+ # governing block call of a recursive method lives in the base case
51982+ # (`return blk.call if n <= 0`), not the tail (which is the recursive
51983+ # call). Descends statement lists and control-flow branches but stops
51984+ # at nested def/lambda/block frames (their returns are not ours).
51985+ # Returns the CallNode id or -1.
51986+ def find_block_call_in_returns(nid, bpname, decl_body_id, depth)
51987+ if nid < 0 || depth > 8
51988+ return -1
51989+ end
51990+ t = @nd_type[nid]
51991+ if t == "DefNode" || t == "LambdaNode" || t == "BlockNode"
51992+ return -1
51993+ end
51994+ if t == "ReturnNode"
51995+ ra = @nd_arguments[nid]
51996+ if ra >= 0
51997+ ras = get_args(ra)
51998+ if ras.length == 1
51999+ return check_block_call_expr(ras[0], bpname, decl_body_id, 0)
5189352000 end
51894- # `pv.call` where pv is a local proc capturing the block — descend
51895- # into pv's proc body and keep looking for the governing blk.call.
51896- plit = find_proc_literal_assign(decl_body_id, @nd_name[r])
51897- if plit >= 0 && @nd_block[plit] >= 0
51898- return trace_block_call_return(@nd_body[@nd_block[plit]], bpname, decl_body_id, depth + 1)
52001+ end
52002+ return -1
52003+ end
52004+ if t == "StatementsNode"
52005+ ss = parse_id_list(@nd_stmts[nid])
52006+ k = 0
52007+ while k < ss.length
52008+ c = find_block_call_in_returns(ss[k], bpname, decl_body_id, depth + 1)
52009+ if c >= 0
52010+ return c
5189952011 end
52012+ k = k + 1
52013+ end
52014+ return -1
52015+ end
52016+ if @nd_body[nid] >= 0
52017+ c = find_block_call_in_returns(@nd_body[nid], bpname, decl_body_id, depth + 1)
52018+ if c >= 0
52019+ return c
52020+ end
52021+ end
52022+ if @nd_subsequent[nid] >= 0
52023+ c = find_block_call_in_returns(@nd_subsequent[nid], bpname, decl_body_id, depth + 1)
52024+ if c >= 0
52025+ return c
52026+ end
52027+ end
52028+ if @nd_else_clause[nid] >= 0
52029+ c = find_block_call_in_returns(@nd_else_clause[nid], bpname, decl_body_id, depth + 1)
52030+ if c >= 0
52031+ return c
5190052032 end
5190152033 end
5190252034 -1
5190352035 end
5190452036
5190552037 # Locate the `<bpname>.call(...)` whose value is the return of method
51906- # body `bid` (the last statement, optionally wrapped in `return`, and
51907- # possibly reached through a `p = proc { blk.call }; p.call` proc-var
51908- # indirection). Returns the CallNode id or -1. This is the call whose
51909- # result type a literal-block caller can refine.
52038+ # body `bid`: the tail expression first (optionally `return`-wrapped,
52039+ # possibly through a `p = proc { blk.call }; p.call` indirection), then
52040+ # any earlier `return blk.call` (a recursive method's base case).
52041+ # Returns the CallNode id or -1 — the call whose result type a
52042+ # literal-block caller can refine.
5191052043 def find_block_param_call_expr(bid, bpname)
51911- trace_block_call_return(bid, bpname, bid, 0)
52044+ c = trace_block_call_return(bid, bpname, bid, 0)
52045+ if c >= 0
52046+ return c
52047+ end
52048+ st = get_stmts(bid)
52049+ k = 0
52050+ while k < st.length
52051+ c = find_block_call_in_returns(st[k], bpname, bid, 0)
52052+ if c >= 0
52053+ return c
52054+ end
52055+ k = k + 1
52056+ end
52057+ -1
5191252058 end
5191352059
5191452060 # An `&block`-param method called with a literal block returns the
@@ -52840,6 +52986,18 @@
5284052986 return
5284152987 end
5284252988 end
52989+ # A block-bearing tail call to a user-defined method is the method's
52990+ # implicit return value, not a side-effecting builtin iterator like
52991+ # `each` / `times`. Route it through compile_expr so the value is
52992+ # returned rather than dropped — this is what carries a recursive
52993+ # block-forwarding call's result (`countdown(n - 1) { ... }`, the
52994+ # explicit-&block recursion). Builtins fall through to the
52995+ # statement-and-default path below.
52996+ if return_type != "void" && block_bearing_tail_is_user_call(last) == 1
52997+ val = compile_expr(last)
52998+ emit(" return " + val + ";")
52999+ return
53000+ end
5284353001 compile_stmt(last)
5284453002 if return_type != "void"
5284553003 emit(" return " + c_return_default(return_type) + ";")