Commit e01a9f02

e01a9f022012Merged

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

Description

Carry an &block-param method's value at the block's real type

A method that takes a &block parameter and returns blk.call(...) carries the block's value through sp_proc_call's uniform mrb_int slot. When the literal block returns a pointer type (a string, etc.) the result was typed as int and printed as a raw address.

Refine the call's type per call site to the literal block body's return type (block_param_call_type, a sibling of the yield inline path). The method's C signature is unchanged, so distinct literal blocks at different call sites coexist. The carried pointer is GC-safe for immediate use; a result held across an allocation can be collected (a known limitation shared with the lam_box path).

Refs #1362

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

Details

Commit
e01a9f022012
Parent
88277dcb77d9

spinel_codegen.rbmodifiedView file

@@ -3564,6 +3564,15 @@
35643564 if yci_t != ""
35653565 return yci_t
35663566 end
3567+ # Sibling of the yield path for `&block`-param methods (`def m(&b);
3568+ # b.call(...); end`) called with a literal block: type the result as
3569+ # the block body's return type so a pointer value isn't printed as a
3570+ # raw int. yield_call_inline_type returns "" for these (it gates on
3571+ # no block param), so the two are mutually exclusive.
3572+ bpc_t = block_param_call_type(nid)
3573+ if bpc_t != ""
3574+ return bpc_t
3575+ end
35673576 end
35683577 # Cache lookup. analyze.rb's annotate_all_node_types fills
35693578 # @nd_inferred_type for every reachable node OUTSIDE block bodies
@@ -51747,6 +51756,139 @@
5174751756 rt_inf
5174851757 end
5174951758
51759+ # Locate the `<bpname>.call(...)` whose value is the return of method
51760+ # body `bid` (the last statement, optionally wrapped in `return`).
51761+ # Returns the CallNode id or -1. This is the call whose result type a
51762+ # literal-block caller can refine.
51763+ def find_block_param_call_expr(bid, bpname)
51764+ st = get_stmts(bid)
51765+ if st.length == 0
51766+ return -1
51767+ end
51768+ last = st.last
51769+ if @nd_type[last] == "ReturnNode"
51770+ ra = @nd_arguments[last]
51771+ if ra >= 0
51772+ ras = get_args(ra)
51773+ if ras.length == 1
51774+ last = ras[0]
51775+ end
51776+ end
51777+ end
51778+ if @nd_type[last] == "CallNode" && @nd_name[last] == "call"
51779+ r = @nd_receiver[last]
51780+ if r >= 0 && @nd_type[r] == "LocalVariableReadNode" && @nd_name[r] == bpname
51781+ return last
51782+ end
51783+ end
51784+ -1
51785+ end
51786+
51787+ # An `&block`-param method called with a literal block returns the
51788+ # block's value through sp_proc_call, which carries it in a uniform
51789+ # `mrb_int` slot — the method's C signature never changes. When the
51790+ # block body yields a pointer type (string, etc.) the result is
51791+ # mistyped as int and printed as a raw address. Refine the call's type
51792+ # per call site to the literal block's return type so readers cast the
51793+ # carried pointer back. Because the ABI is unchanged, distinct literal
51794+ # blocks at different call sites coexist. (The carried pointer is
51795+ # GC-safe for immediate use; a result held across an allocation can be
51796+ # collected -- a known #1362 limitation shared with the lam_box path.)
51797+ def block_param_call_type(nid)
51798+ blk = @nd_block[nid]
51799+ if blk < 0
51800+ return ""
51801+ end
51802+ mname = @nd_name[nid]
51803+ recv = @nd_receiver[nid]
51804+ bid = -1
51805+ bpname = ""
51806+ decl_rt = "int"
51807+ if recv < 0
51808+ mi = find_method_idx(mname)
51809+ if mi >= 0 && meth_has_block_param(mi) == 1
51810+ bid = @meth_body_ids[mi]
51811+ decl_rt = @meth_return_types[mi]
51812+ bpname = find_block_param_name(@meth_param_names[mi].split(",", -1), @meth_param_types[mi].split(",", -1))
51813+ end
51814+ else
51815+ rt = base_type(infer_type(recv))
51816+ if is_obj_type(rt) == 1
51817+ cn = rt[4, rt.length - 4]
51818+ cci = find_class_idx(cn)
51819+ if cci >= 0
51820+ owner = find_method_owner(cci, mname)
51821+ if owner != ""
51822+ oci = find_class_idx(owner)
51823+ midx = -1
51824+ if oci >= 0
51825+ midx = cls_find_method_direct(oci, mname)
51826+ end
51827+ if oci >= 0 && midx >= 0 && cls_method_has_block_param(oci, midx) == 1
51828+ bodies = @cls_meth_bodies[oci].split(";", -1)
51829+ if midx < bodies.length
51830+ bid = bodies[midx].to_i
51831+ end
51832+ decl_rt = cls_method_return(oci, mname)
51833+ bpname = find_block_param_name(cls_meth_pnames_get(oci, midx), cls_meth_ptypes_get(oci, midx))
51834+ end
51835+ end
51836+ end
51837+ end
51838+ end
51839+ if bid < 0 || bpname == ""
51840+ return ""
51841+ end
51842+ # Only refine the unhelpful int default; a concrete analyze return
51843+ # type is authoritative (recomputing here lacks the method's scope).
51844+ if base_type(decl_rt) != "int"
51845+ return ""
51846+ end
51847+ callx = find_block_param_call_expr(bid, bpname)
51848+ if callx < 0
51849+ return ""
51850+ end
51851+ bbody = @nd_body[blk]
51852+ if bbody < 0
51853+ return ""
51854+ end
51855+ bst = get_stmts(bbody)
51856+ if bst.length == 0
51857+ return ""
51858+ end
51859+ bp_names = "".split(",", -1)
51860+ bp = @nd_parameters[blk]
51861+ if bp >= 0
51862+ inner = @nd_parameters[bp]
51863+ if inner >= 0
51864+ reqs = parse_id_list(@nd_requireds[inner])
51865+ kbp = 0
51866+ while kbp < reqs.length
51867+ bp_names.push(@nd_name[reqs[kbp]])
51868+ kbp = kbp + 1
51869+ end
51870+ end
51871+ end
51872+ arg_ids = []
51873+ aid = @nd_arguments[callx]
51874+ if aid >= 0
51875+ arg_ids = get_args(aid)
51876+ end
51877+ push_scope
51878+ kp = 0
51879+ while kp < bp_names.length
51880+ at = "int"
51881+ if kp < arg_ids.length
51882+ at = infer_type(arg_ids[kp])
51883+ end
51884+ declare_var(bp_names[kp], at)
51885+ kp = kp + 1
51886+ end
51887+ rt_inf = infer_type(bst.last)
51888+ pop_scope
51889+ rt_inf
51890+ end
51891+
5175051892 # Two-level yield inline: `outer do |x| yield x*2 end` appearing
5175151893 # inside an already-inlined caller (`inner`) where `outer` is itself a
5175251894 # yield-using top-level method. The callee body's yield gets replaced