Commit 87061dae

87061dae35a7Merged

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

Description

analyze,codegen: thread block into a yielding constructor's yield

Pool.new(1) { ... } dropped the block — yield in initialize emitted 0, so @free.push(yield) pushed 0 and @free stayed int_array even when the block returned a string.

Two coupled parts: (a) Block threading: when initialize yields, sp__new takes a trailing sp_Proc *_ctor_blk; the call site emits the block as a proc literal (captures handled via compile_proc_literal), and yield in the inlined initialize body lowers to sp_proc_call, casting the mrb_int result back to the yield-return type. (b) Yield-return typing: a pre-pass records each class's ctor-yield return type from the first C.new(...) { block } site's block tail expr; infer_type(YieldNode) returns it inside initialize, so @free.push(yield) widens @free to the block's element type (str_array for a string block). One site drives the type.

Proc fn return now casts pointer-typed tails to mrb_int (the ABI type) to avoid -Wint-conversion. Non-yielding constructors are byte-identical (the param is only added when the ctor yields). Limitation: the standalone sp__initialize wrapper (super path) doesn't thread the block yet — only the inlined _new path does.

Fixes #709

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

Details

Commit
87061dae35a7
Parent
962f5c7039ad

spinel_analyze.rbmodifiedView file

@@ -440,6 +440,10 @@
440440 # Yield/block tracking (parallel with meth_names / cls_meth_names)
441441 @meth_has_yield = []
442442 @cls_meth_has_yield = "".split(",", -1)
443+ # #709: per-class type a yielding `initialize`'s `yield` returns,
444+ # taken from the first `C.new(...) { block }` site's block tail
445+ # expr. Keyed by class name. Drives `@ivar.push(yield)` ivar typing.
446+ @cls_ctor_yield_ret = {}
443447
444448 # Block function accumulator (emitted before forward decls)
445449 @block_funcs = ""
@@ -2478,6 +2482,17 @@
24782482 if t == "CallNode"
24792483 return infer_call_type(nid)
24802484 end
2485+ # #709: `yield` as an expression inside a yielding `initialize`
2486+ # takes the type recorded from the matching `C.new(...) { block }`
2487+ # site, so `@free.push(yield)` widens @free to the block's element
2488+ # type. Only fires when current_ctor_yield_ret is set (a class with
2489+ # a yielding initialize and a known .new{block} call site).
2490+ if t == "YieldNode"
2491+ cyr = current_ctor_yield_ret
2492+ if cyr != ""
2493+ return cyr
2494+ end
2495+ end
24812496 if t == "IfNode"
24822497 then_type = "nil"
24832498 body = @nd_body[nid]
@@ -13040,6 +13055,64 @@
1304013055 scan_new_calls(@root_id)
1304113056 end
1304213057
13058+ # #709: walk the AST for `C.new(...) { block }` where C's initialize
13059+ # yields, and record the block's tail-expr type as C's ctor-yield
13060+ # return type. First site wins (the one-site-drives-it convention).
13061+ # `infer_type(YieldNode)` reads this so `@free.push(yield)` widens
13062+ # @free to the right array variant.
13063+ def infer_ctor_yield_ret
13064+ scan_ctor_yield_ret(@root_id)
13065+ end
13066+ def scan_ctor_yield_ret(nid)
13067+ if nid < 0
13068+ return
13069+ end
13070+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "new" && @nd_block[nid] >= 0
13071+ recv = @nd_receiver[nid]
13072+ if recv >= 0 && @nd_type[recv] == "ConstantReadNode"
13073+ cname = @nd_name[recv]
13074+ ci = find_class_idx(cname)
13075+ if ci >= 0 && @cls_ctor_yield_ret[cname] == nil
13076+ init_idx = cls_find_method(ci, "initialize")
13077+ if init_idx >= 0 && cls_method_has_yield(ci, init_idx) == 1
13078+ blk = @nd_block[nid]
13079+ bbody = @nd_body[blk]
13080+ if bbody >= 0
13081+ bs = get_stmts(bbody)
13082+ if bs.length > 0
13083+ rt = infer_type(bs.last)
13084+ if rt != "" && rt != "void"
13085+ @cls_ctor_yield_ret[cname] = rt
13086+ end
13087+ end
13088+ end
13089+ end
13090+ end
13091+ end
13092+ end
13093+ cs = []
13094+ push_child_ids(nid, cs)
13095+ k = 0
13096+ while k < cs.length
13097+ scan_ctor_yield_ret(cs[k])
13098+ k = k + 1
13099+ end
13100+ end
13101+
13102+ # Look up the ctor-yield return type for the class currently being
13103+ # analyzed; "" when unknown. Used by infer_type's YieldNode arm.
13104+ def current_ctor_yield_ret
13105+ if @current_class_idx < 0
13106+ return ""
13107+ end
13108+ cn = @cls_names[@current_class_idx]
13109+ r = @cls_ctor_yield_ret[cn]
13110+ if r == nil
13111+ return ""
13112+ end
13113+ r
13114+ end
13115+
1304313116 # Narrow pre-pass for `rewrite_instance_eval_calls`: walk top-level
1304413117 # CallNodes shaped `recv.method(args)` where recv resolves to an
1304513118 # obj_<C> via top-level scope, and let scan_new_calls' receiver-method
@@ -19831,6 +19904,8 @@
1983119904 infer_cls_meth_param_from_body
1983219905 # Pre-pass: scan for .new calls to infer constructor param types
1983319906 infer_constructor_types
19907+ # Pre-pass: record ctor-yield return types from C.new{block} sites.
19908+ infer_ctor_yield_ret
1983419909 # Bare `new(args)` in an inherited class method body widens
1983519910 # the subclass's `initialize` ptypes via the cls method's
1983619911 # already-widened ptypes. Runs after infer_constructor_types