Commit a2f2f0c7
a2f2f0c75325Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Recursive &block forwarding: capture, cell cast, and unified block return
A method that forwards its escaping &block into a nested block (countdown(n-1) { blk.call }) and is called with blocks of differing return types now compiles
and runs:
- mark_proc_captures also processes plain method-call blocks that get lifted to standalone proc functions (a method keeping a real, non-inlined &block), so a forwarded &block param is celled. Trampoline / yield-lowered methods are excluded (their blocks are spliced or specialized per call site).
- a TY_PROC cell read in the enclosing scope is cast back to sp_Proc*.
- yield_value_type unifies the block value type across ALL call sites for a non-inlined &block method (string + int block -> poly); inlined and self-recursive-lowered yield methods keep their per-call-site type.
- a method's unified block return type (Scope.blk_ret) is propagated to the blocks passed to it, so they return that common type (poly via _sp_proc_poly_ret) and the sp_proc_call ABI stays consistent.
Fixes recursive_block_forward_value.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- a2f2f0c75325
- Parent
- 9ff8a6a34432
src/analyze.cmodifiedView file
@@ -259,10 +259,15 @@
259259
if (rmi != mi) continue;260260
int bb = nt_ref(nt, blk, "body");261261
int bn = 0; const int *bd = bb >= 0 ? nt_arr(nt, bb, "body", &bn) : NULL;262-
if (bn == 0) { result = TY_NIL; break; }263-
TyKind bt = infer_type(c, bd[bn - 1]);264-
result = bt == TY_VOID ? TY_NIL : bt; /* a void last-expr's block value is nil */265-
break;262+
TyKind bt = (bn == 0) ? TY_NIL : infer_type(c, bd[bn - 1]);263+
if (bt == TY_VOID) bt = TY_NIL;264+
/* A yield-inlined (or self-recursive-lowered) method is specialized per265+
call site, so its internal block type is the first concrete block. A266+
non-inlined method (an escaping &block called via the proc ABI) has one267+
body, so its block value type must unify ALL call sites (string + int268+
block -> poly). */269+
if (c->scopes[mi].yields || c->scopes[mi].is_lowered_yield) { result = bt; break; }270+
result = ty_unify(result, bt);266271
}267272
g_yvt_depth--;268273
return result;@@ -6880,6 +6885,13 @@
68806885
r = ty_unify(r, return_node_type(c, id));68816886
}68826887
if (r != sc->ret) { sc->ret = r; changed = 1; }6888+
/* For a method with a &block param, record the value type its block yields6889+
(unified across all call sites). Blocks passed to it are emitted returning6890+
this common type so the sp_proc_call ABI is consistent. */6891+
if (sc->blk_param && sc->blk_param[0] && !sc->yields && !sc->is_lowered_yield) {6892+
TyKind bvt = yield_value_type(c, (int)(sc - c->scopes));6893+
if (bvt != TY_UNKNOWN && sc->blk_ret != (int)bvt) { sc->blk_ret = (int)bvt; changed = 1; }6894+
}68836895
/* When the method returns a proc, record the proc's body return type so a68846896
caller's `m.call(...)` resolves its result type (factory pattern). */68856897
if (r == TY_PROC) {@@ -7159,15 +7171,49 @@
71597171
scope OUTSIDE any proc body. (A name assigned only inside the proc is a71607172
proc-local, not a capture -- Ruby's block-local rule.) Captured enclosing71617173
locals get a heap cell. */7174+
/* A plain block `m(args) { ... }` passed to a method that keeps a real &block7175+
parameter (not yield-inlined) is lifted to a standalone proc function, so it7176+
captures enclosing variables exactly like a proc literal. */7177+
static int a_block_is_lifted(Compiler *c, int id) {7178+
const NodeTable *nt = c->nt;7179+
const char *ty = nt_type(nt, id);7180+
if (!ty || strcmp(ty, "CallNode")) return 0;7181+
int blk = nt_ref(nt, id, "block");7182+
if (blk < 0 || !nt_type(nt, blk) || strcmp(nt_type(nt, blk), "BlockNode")) return 0;7183+
const char *name = nt_str(nt, id, "name");7184+
if (!name) return 0;7185+
int recv = nt_ref(nt, id, "receiver");7186+
int mi = -1;7187+
if (recv < 0) {7188+
mi = comp_method_index(c, name);7189+
if (mi < 0) { Scope *self = comp_scope_of(c, id); if (self && self->class_id >= 0) mi = comp_method_in_chain(c, self->class_id, name, NULL); }7190+
} else {7191+
TyKind rt = infer_type(c, recv);7192+
if (ty_is_object(rt)) mi = comp_method_in_chain(c, ty_object_class(rt), name, NULL);7193+
}7194+
if (mi < 0) return 0;7195+
Scope *m = &c->scopes[mi];7196+
if (!m->blk_param || !m->blk_param[0] || m->yields || m->is_lowered_yield) return 0;7197+
/* instance_eval/exec trampolines splice their block at the call site rather7198+
than lifting it to a proc, so they are not lifted-block captures. */7199+
if (m->class_id >= 0 && !m->is_cmethod && m->name &&7200+
comp_trampoline_kind(c, m->class_id, m->name, NULL)) return 0;7201+
return 1;7202+
}7203+
7204+
static int a_proc_create_or_lifted(Compiler *c, int id) {7205+
return is_proc_create(c, id) || a_block_is_lifted(c, id);7206+
}7207+
71627208
static void mark_proc_captures(Compiler *c) {71637209
const NodeTable *nt = c->nt;71647210
char *inproc = (char *)calloc((size_t)nt->count, 1);71657211
if (!inproc) return;71667212
for (int id = 0; id < nt->count; id++)7167-
if (is_proc_create(c, id)) { int body = a_proc_body(c, id); if (body >= 0) a_mark_subtree(c, body, inproc); }7213+
if (a_proc_create_or_lifted(c, id)) { int body = a_proc_body(c, id); if (body >= 0) a_mark_subtree(c, body, inproc); }71687214
71697215
for (int id = 0; id < nt->count; id++) {7170-
if (!is_proc_create(c, id)) continue;7216+
if (!a_proc_create_or_lifted(c, id)) continue;71717217
int body = a_proc_body(c, id);71727218
if (body < 0) continue;71737219
int encl = c->nscope[id];src/codegen.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…