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
# Yield/block tracking (parallel with meth_names / cls_meth_names) @meth_has_yield = [] @cls_meth_has_yield = "".split(",", -1) # #709: per-class type a yielding `initialize`'s `yield` returns, # taken from the first `C.new(...) { block }` site's block tail # expr. Keyed by class name. Drives `@ivar.push(yield)` ivar typing. @cls_ctor_yield_ret = {} # Block function accumulator (emitted before forward decls) @block_funcs = "" if t == "CallNode" return infer_call_type(nid) end # #709: `yield` as an expression inside a yielding `initialize` # takes the type recorded from the matching `C.new(...) { block }` # site, so `@free.push(yield)` widens @free to the block's element # type. Only fires when current_ctor_yield_ret is set (a class with # a yielding initialize and a known .new{block} call site). if t == "YieldNode" cyr = current_ctor_yield_ret if cyr != "" return cyr end end if t == "IfNode" then_type = "nil" body = @nd_body[nid] scan_new_calls(@root_id) end # #709: walk the AST for `C.new(...) { block }` where C's initialize # yields, and record the block's tail-expr type as C's ctor-yield # return type. First site wins (the one-site-drives-it convention). # `infer_type(YieldNode)` reads this so `@free.push(yield)` widens # @free to the right array variant. def infer_ctor_yield_ret scan_ctor_yield_ret(@root_id) end def scan_ctor_yield_ret(nid) if nid < 0 return end if @nd_type[nid] == "CallNode" && @nd_name[nid] == "new" && @nd_block[nid] >= 0 recv = @nd_receiver[nid] if recv >= 0 && @nd_type[recv] == "ConstantReadNode" cname = @nd_name[recv] ci = find_class_idx(cname) if ci >= 0 && @cls_ctor_yield_ret[cname] == nil init_idx = cls_find_method(ci, "initialize") if init_idx >= 0 && cls_method_has_yield(ci, init_idx) == 1 blk = @nd_block[nid] bbody = @nd_body[blk] if bbody >= 0 bs = get_stmts(bbody) if bs.length > 0 rt = infer_type(bs.last) if rt != "" && rt != "void" @cls_ctor_yield_ret[cname] = rt end end end end end end end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length scan_ctor_yield_ret(cs[k]) k = k + 1 end end # Look up the ctor-yield return type for the class currently being # analyzed; "" when unknown. Used by infer_type's YieldNode arm. def current_ctor_yield_ret if @current_class_idx < 0 return "" end cn = @cls_names[@current_class_idx] r = @cls_ctor_yield_ret[cn] if r == nil return "" end r end # Narrow pre-pass for `rewrite_instance_eval_calls`: walk top-level # CallNodes shaped `recv.method(args)` where recv resolves to an # obj_<C> via top-level scope, and let scan_new_calls' receiver-method infer_cls_meth_param_from_body # Pre-pass: scan for .new calls to infer constructor param types infer_constructor_types # Pre-pass: record ctor-yield return types from C.new{block} sites. infer_ctor_yield_ret # Bare `new(args)` in an inherited class method body widens # the subclass's `initialize` ptypes via the cls method's # already-widened ptypes. Runs after infer_constructor_typesspinel_codegen.rbmodifiedView file
Loading diff…
test/i709.rbaddedView file
Loading diff…
test/i709.rb.expectedaddedView file
Loading diff…