Commit 6453c0f3
6453c0f3c647Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Infer the value type of a forwarded &block so the forwarder can inline
A method whose &block parameter is only forwarded (def m(&b); bar(&b); end)
is classified as inlinable, but its return type was inferred as nil: both
yield_value_type and method_call_ret saw the forwarding call site bar(&b)
carry a BlockArgumentNode (which has no block body) and fell back to nil/void.
That made the inliner bail (emit_inline_expr requires a scalar return) and the
method was emitted neither inline nor as a real function -- an undefined symbol.
Resolve a forwarded block to the enclosing method's own yield value: bar(&b)
yields whatever the block active in m produces, i.e. yield_value_type(m).
With the type recovered, the existing inliner (which already maps a forwarded
&block to the active block at the splice) emits correct code for forwarding to
both a yielding target and an explicit &block target, including anonymous &,
multi-hop chains, and multi-site forwarders.
Refs #1288
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 6453c0f3c647
- Parent
- 71972a243064
src/analyze_util.cmodifiedView file
} } if (rmi != mi) continue; /* `mi(&b)`: the call forwards the block of its enclosing method rather than passing a literal. The value `mi` yields is then whatever that forwarded block produces -- i.e. the enclosing method's yield value. */ const char *blkty = nt_type(nt, blk); if (blkty && !strcmp(blkty, "BlockArgumentNode")) { Scope *encl = comp_scope_of(c, cid); int emi = encl ? (int)(encl - c->scopes) : -1; TyKind ft = (emi >= 0 && emi != mi) ? yield_value_type(c, emi) : TY_UNKNOWN; if (ft == TY_VOID) ft = TY_NIL; if (c->scopes[mi].yields || c->scopes[mi].is_lowered_yield) { if (ft != TY_UNKNOWN) { result = ft; break; } continue; } result = ty_unify(result, ft); continue; } int bb = nt_ref(nt, blk, "body"); int bn = 0; const int *bd = bb >= 0 ? nt_arr(nt, bb, "body", &bn) : NULL; TyKind bt; if (c->scopes[mi].is_lowered_yield || is_yield || is_blk_param_call(c, last, mi)) { int blk = nt_ref(c->nt, call_id, "block"); if (blk >= 0) { const char *bty = nt_type(c->nt, blk); if (bty && !strcmp(bty, "BlockArgumentNode")) { /* `callee(&b)` forwards the block active in the enclosing method, so the value `callee` yields is whatever that forwarded block produces -- i.e. the enclosing method's own per-call-site yield value. */ Scope *encl = comp_scope_of(c, call_id); int emi = encl ? (int)(encl - c->scopes) : -1; if (emi >= 0 && emi != mi) { TyKind ft = yield_value_type(c, emi); if (ft != TY_UNKNOWN && ft != TY_VOID) return ft; } } int bbody = nt_ref(c->nt, blk, "body"); int bn = 0; const int *bb = bbody >= 0 ? nt_arr(c->nt, bbody, "body", &bn) : NULL; if (bn > 0) {test/block_forward.rbaddedView file
Loading diff…
test/block_forward.rb.expectedaddedView file
Loading diff…