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

@@ -207,6 +207,22 @@
207207 }
208208 }
209209 if (rmi != mi) continue;
210+ /* `mi(&b)`: the call forwards the block of its enclosing method rather
211+ than passing a literal. The value `mi` yields is then whatever that
212+ forwarded block produces -- i.e. the enclosing method's yield value. */
213+ const char *blkty = nt_type(nt, blk);
214+ if (blkty && !strcmp(blkty, "BlockArgumentNode")) {
215+ Scope *encl = comp_scope_of(c, cid);
216+ int emi = encl ? (int)(encl - c->scopes) : -1;
217+ TyKind ft = (emi >= 0 && emi != mi) ? yield_value_type(c, emi) : TY_UNKNOWN;
218+ if (ft == TY_VOID) ft = TY_NIL;
219+ if (c->scopes[mi].yields || c->scopes[mi].is_lowered_yield) {
220+ if (ft != TY_UNKNOWN) { result = ft; break; }
221+ continue;
222+ }
223+ result = ty_unify(result, ft);
224+ continue;
225+ }
210226 int bb = nt_ref(nt, blk, "body");
211227 int bn = 0; const int *bd = bb >= 0 ? nt_arr(nt, bb, "body", &bn) : NULL;
212228 TyKind bt;
@@ -237,6 +253,18 @@
237253 if (c->scopes[mi].is_lowered_yield || is_yield || is_blk_param_call(c, last, mi)) {
238254 int blk = nt_ref(c->nt, call_id, "block");
239255 if (blk >= 0) {
256+ const char *bty = nt_type(c->nt, blk);
257+ if (bty && !strcmp(bty, "BlockArgumentNode")) {
258+ /* `callee(&b)` forwards the block active in the enclosing method, so
259+ the value `callee` yields is whatever that forwarded block produces
260+ -- i.e. the enclosing method's own per-call-site yield value. */
261+ Scope *encl = comp_scope_of(c, call_id);
262+ int emi = encl ? (int)(encl - c->scopes) : -1;
263+ if (emi >= 0 && emi != mi) {
264+ TyKind ft = yield_value_type(c, emi);
265+ if (ft != TY_UNKNOWN && ft != TY_VOID) return ft;
266+ }
267+ }
240268 int bbody = nt_ref(c->nt, blk, "body");
241269 int bn = 0; const int *bb = bbody >= 0 ? nt_arr(c->nt, bbody, "body", &bn) : NULL;
242270 if (bn > 0) {