Commit d6f31986
d6f319863881Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
First-class Proc: closures via heap cells (capture + mutation)
A proc that captures an enclosing local now compiles instead of aborting. Captured locals live in heap cells (mrb_int *) shared by pointer between the enclosing scope and the proc, so mutation propagates both ways and the cell outlives the enclosing frame (the proc's cap keeps it alive).
Mechanism (modeled on legacy's spinel -c output for closures):
- analyze marks an enclosing local is_cell when a proc uses it AND it belongs to the enclosing scope (a param, or assigned outside any proc body). A name assigned only inside the proc stays proc-local -- Ruby's block-local rule.
- codegen declares a cell local as
mrb_int *_cell_x(params copy their incoming value in); all reads/writes route through emit_local_ref to(*_cell_x), or to(*((cap *)_cap)->x)inside the proc body. - each capturing proc emits a cap struct + scan; the proc-creation site allocs the cap and shares the enclosing cell pointers.
Two deliberate choices vs legacy: (1) uniform celling for a captured var's whole scope, rather than legacy's promote-at-capture-point + redirect-after -- the rewrite's emitter has no statement-position state, and is_cell is a clean static property. (2) cap_scan marks the cap struct itself (sp_gc_mark(p)) before the cells; sp_Proc_scan does not mark cap, so marking only the cells (as legacy's emitted cap_scan does) would let the cap be swept out from under a live proc -- a latent UAF its tiny test never triggers. Verified under GC pressure (50k allocations between proc creation and call; captured value persists).
Also: an assignment expression now infers as its assigned value's type
(x = v -> typeof v), so a proc/method body ending in an assignment types
correctly.
Scope: int captures only (cell is mrb_int *); non-int capture defers loudly.
make test: 165 pass / 5 fail / 674 error (was 164 / 5 / 675); proc_closure green, no regressions.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- d6f319863881
- Parent
- d126222f3d75
src/analyze.cmodifiedView file
if (!strcmp(ty, "NilNode")) return TY_NIL; if (!strcmp(ty, "RangeNode")) return TY_RANGE; if (!strcmp(ty, "LambdaNode")) return TY_PROC; /* an assignment expression evaluates to the assigned value */ if (!strcmp(ty, "LocalVariableWriteNode")) return infer_type(c, nt_ref(nt, id, "value")); if (!strcmp(ty, "LocalVariableReadNode")) { const char *nm = nt_str(nt, id, "name"); #undef ADD_NAME}/* ---- proc capture detection (closures) ---- A local read inside a proc body that isn't bound by the proc (param or a local the body itself writes) is a captured/free variable; its enclosing local must live in a heap cell so the closure and the enclosing scope share mutable storage. Mark those enclosing locals is_cell. */typedef struct { const char **v; int n, cap; } ANameSet;static int aname_has(ANameSet *s, const char *nm) { if (!nm) return 1; for (int i = 0; i < s->n; i++) if (!strcmp(s->v[i], nm)) return 1; return 0;}static void aname_add(ANameSet *s, const char *nm) { if (aname_has(s, nm)) return; if (s->n >= s->cap) { s->cap = s->cap ? s->cap * 2 : 8; s->v = realloc(s->v, sizeof(char *) * (size_t)s->cap); } s->v[s->n++] = nm;}static int a_nested_block(const char *ty) { return ty && (!strcmp(ty, "BlockNode") || !strcmp(ty, "LambdaNode")); }static int a_is_local_node(const char *ty) { return ty && (!strcmp(ty, "LocalVariableReadNode") || !strcmp(ty, "LocalVariableWriteNode") || !strcmp(ty, "LocalVariableTargetNode") || !strcmp(ty, "LocalVariableOperatorWriteNode") || !strcmp(ty, "LocalVariableOrWriteNode") || !strcmp(ty, "LocalVariableAndWriteNode"));}static int a_is_write_node(const char *ty) { return ty && (!strcmp(ty, "LocalVariableWriteNode") || !strcmp(ty, "LocalVariableTargetNode") || !strcmp(ty, "LocalVariableOperatorWriteNode") || !strcmp(ty, "LocalVariableOrWriteNode") || !strcmp(ty, "LocalVariableAndWriteNode"));}/* Mark every node id in the subtree (crossing nested blocks: a node inside an inner block is still "inside a proc"). */static void a_mark_subtree(Compiler *c, int id, char *inproc) { if (id < 0) return; inproc[id] = 1; int nr = nt_num_refs(c->nt, id); for (int i = 0; i < nr; i++) { int ch = nt_ref_at(c->nt, id, i); if (ch >= 0) a_mark_subtree(c, ch, inproc); } int na = nt_num_arrs(c->nt, id); for (int i = 0; i < na; i++) { int n = 0; const int *ids = nt_arr_at(c->nt, id, i, &n); for (int k = 0; k < n; k++) if (ids[k] >= 0) a_mark_subtree(c, ids[k], inproc); }}/* Names used (read or written) directly in the proc body, not crossing nested blocks. */static void a_collect_used(Compiler *c, int id, ANameSet *out) { if (id < 0) return; const char *ty = nt_type(c->nt, id); if (!ty) return; if (a_is_local_node(ty)) aname_add(out, nt_str(c->nt, id, "name")); int nr = nt_num_refs(c->nt, id); for (int i = 0; i < nr; i++) { int ch = nt_ref_at(c->nt, id, i); if (ch >= 0 && !a_nested_block(nt_type(c->nt, ch))) a_collect_used(c, ch, out); } int na = nt_num_arrs(c->nt, id); for (int i = 0; i < na; i++) { int n = 0; const int *ids = nt_arr_at(c->nt, id, i, &n); for (int k = 0; k < n; k++) if (ids[k] >= 0 && !a_nested_block(nt_type(c->nt, ids[k]))) a_collect_used(c, ids[k], out); }}static int a_proc_params_node(Compiler *c, int create) { const char *ty = nt_type(c->nt, create); if (ty && !strcmp(ty, "LambdaNode")) return nt_ref(c->nt, create, "parameters"); int block = nt_ref(c->nt, create, "block"); if (block < 0) return -1; int bp = nt_ref(c->nt, block, "parameters"); return bp < 0 ? -1 : nt_ref(c->nt, bp, "parameters");}static int a_proc_body(Compiler *c, int create) { const char *ty = nt_type(c->nt, create); if (ty && !strcmp(ty, "LambdaNode")) return nt_ref(c->nt, create, "body"); int block = nt_ref(c->nt, create, "block"); return block >= 0 ? nt_ref(c->nt, block, "body") : -1;}/* A name used inside a proc is captured iff it belongs to the enclosing scope: it is an enclosing parameter, or it is assigned somewhere in the enclosing scope OUTSIDE any proc body. (A name assigned only inside the proc is a proc-local, not a capture -- Ruby's block-local rule.) Captured enclosing locals get a heap cell. */static void mark_proc_captures(Compiler *c) { const NodeTable *nt = c->nt; char *inproc = (char *)calloc((size_t)nt->count, 1); if (!inproc) return; for (int id = 0; id < nt->count; id++) if (is_proc_create(c, id)) { int body = a_proc_body(c, id); if (body >= 0) a_mark_subtree(c, body, inproc); } for (int id = 0; id < nt->count; id++) { if (!is_proc_create(c, id)) continue; int body = a_proc_body(c, id); if (body < 0) continue; int encl = c->nscope[id]; ANameSet params = {0}, used = {0}; int pn = a_proc_params_node(c, id); if (pn >= 0) { int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn); for (int k = 0; k < rn; k++) aname_add(¶ms, nt_str(nt, reqs[k], "name")); } a_collect_used(c, body, &used); Scope *es = &c->scopes[encl]; for (int u = 0; u < used.n; u++) { const char *nm = used.v[u]; if (aname_has(¶ms, nm)) continue; /* the proc's own param */ LocalVar *lv = scope_local(es, nm); if (!lv) continue; /* not an enclosing local */ int owned = lv->is_param; for (int w = 0; w < nt->count && !owned; w++) { if (c->nscope[w] != encl || inproc[w]) continue; if (!a_is_write_node(nt_type(nt, w))) continue; const char *wn = nt_str(nt, w, "name"); if (wn && !strcmp(wn, nm)) owned = 1; } if (owned) lv->is_cell = 1; } free(params.v); free(used.v); } free(inproc);}void analyze_program(Compiler *c) { /* scope 0 = top level */ Scope *top = comp_scope_new(c, NULL, -1); /* recompute returns: a method returning such a param is now poly */ for (int iter = 0; iter < 8; iter++) if (!infer_return_types(c)) break; /* mark locals captured by escaping procs (they need heap cells) */ mark_proc_captures(c); /* Reachability: an instance/free method is live only if its name is referenced somewhere -- as a call name, an alias target, or a symbol literal (covering send/method/define_method). Names never mentionedsrc/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…