Commit d126222f

d126222f3d75Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

First-class Proc: propagate proc return type through params and returns

A proc value that escapes its literal -- passed as an argument, returned from a method, or copied between locals -- needs its body return type to follow it, or a later .call mistypes the (mrb_int-laundered) result.

Add proc_ret_of(), one resolver for the proc-return-type of any proc-valued expression: literal (body type), local (recorded proc_ret), or method call (the callee's new Scope.ret_proc_ret). Then:

  • infer_return_types records ret_proc_ret when a method returns a proc;
  • bind_call_params carries a proc argument's return type to the param;
  • the local-write recorder uses proc_ret_of, covering var copies and factory-call RHSs (not just literals).

Enables def mk; proc { |n| n*2 }; end; mk.call, transform(x, a_proc), and b = a_proc; b.call.

make test: 164 pass / 5 fail / 675 error (was 163 / 5 / 676), no regressions.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
d126222f3d75
Parent
b6b3e2069774

src/analyze.cmodifiedView file

@@ -115,20 +115,40 @@
115115 return bn > 0 ? infer_type(c, bb[bn - 1]) : TY_NIL;
116116 }
117117
118-/* The return type of a proc-typed expression's `.call`. Resolves the proc's
119- create site (literal directly, or via a local's recorded proc_ret). */
120-static TyKind proc_call_ret(Compiler *c, int recv) {
118+/* The body return type (`#call`'s result) of a proc-valued expression, or
119+ TY_UNKNOWN if not statically known. Resolves a literal directly, a local's
120+ recorded proc_ret, and a method call's recorded ret_proc_ret. */
121+static TyKind proc_ret_of(Compiler *c, int node) {
121122 const NodeTable *nt = c->nt;
122- const char *ty = nt_type(nt, recv);
123- if (!ty) return TY_POLY;
123+ const char *ty = nt_type(nt, node);
124+ if (!ty) return TY_UNKNOWN;
125+ if (!strcmp(ty, "LambdaNode") || is_proc_literal(c, node)) return proc_node_ret(c, node);
124126 if (!strcmp(ty, "LocalVariableReadNode")) {
125- Scope *s = comp_scope_of(c, recv);
126- LocalVar *lv = scope_local(s, nt_str(nt, recv, "name"));
127- if (lv && (TyKind)lv->proc_ret != TY_UNKNOWN) return (TyKind)lv->proc_ret;
128- return TY_POLY;
127+ Scope *s = comp_scope_of(c, node);
128+ LocalVar *lv = scope_local(s, nt_str(nt, node, "name"));
129+ return lv ? (TyKind)lv->proc_ret : TY_UNKNOWN;
130+ }
131+ if (!strcmp(ty, "CallNode")) {
132+ /* a method call that returns a proc -> the callee's recorded proc return */
133+ int recv = nt_ref(nt, node, "receiver");
134+ const char *name = nt_str(nt, node, "name");
135+ int mi = -1;
136+ if (recv < 0) {
137+ mi = comp_method_index(c, name);
138+ if (mi < 0) { Scope *self = comp_scope_of(c, node); if (self->class_id >= 0) mi = comp_method_in_chain(c, self->class_id, name, NULL); }
139+ } else {
140+ TyKind rt = infer_type(c, recv);
141+ if (ty_is_object(rt)) mi = comp_method_in_chain(c, ty_object_class(rt), name, NULL);
142+ }
143+ if (mi >= 0) return (TyKind)c->scopes[mi].ret_proc_ret;
129144 }
130- if (!strcmp(ty, "LambdaNode") || is_proc_literal(c, recv)) return proc_node_ret(c, recv);
131- return TY_POLY;
145+ return TY_UNKNOWN;
146+}
147+
148+/* The return type of a proc-typed expression's `.call`; poly when unknown. */
149+static TyKind proc_call_ret(Compiler *c, int recv) {
150+ TyKind r = proc_ret_of(c, recv);
151+ return r == TY_UNKNOWN ? TY_POLY : r;
132152 }
133153
134154 static TyKind infer_call(Compiler *c, int id) {
@@ -1185,10 +1205,8 @@
11851205 a later slice. */
11861206 if (lv->type == TY_PROC && !strcmp(ty, "LocalVariableWriteNode")) {
11871207 int vnode = nt_ref(nt, id, "value");
1188- if (vnode >= 0 && is_proc_create(c, vnode)) {
1189- TyKind pr = proc_node_ret(c, vnode);
1190- if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; }
1191- }
1208+ TyKind pr = vnode >= 0 ? proc_ret_of(c, vnode) : TY_UNKNOWN;
1209+ if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; }
11921210 }
11931211 }
11941212
@@ -1303,6 +1321,12 @@
13031321 if (!p) continue;
13041322 TyKind merged = ty_unify(p->type, at);
13051323 if (merged != p->type) { p->type = merged; changed = 1; }
1324+ /* a proc passed as an argument carries its body return type to the param,
1325+ so `f.call(...)` inside the method knows its result type */
1326+ if (merged == TY_PROC) {
1327+ TyKind pr = proc_ret_of(c, argv[k]);
1328+ if (pr != TY_UNKNOWN && p->proc_ret != (int)pr) { p->proc_ret = (int)pr; changed = 1; }
1329+ }
13061330 }
13071331 return changed;
13081332 }
@@ -1657,6 +1681,24 @@
16571681 r = ty_unify(r, return_node_type(c, id));
16581682 }
16591683 if (r != sc->ret) { sc->ret = r; changed = 1; }
1684+ /* When the method returns a proc, record the proc's body return type so a
1685+ caller's `m.call(...)` resolves its result type (factory pattern). */
1686+ if (r == TY_PROC) {
1687+ TyKind pr = TY_UNKNOWN;
1688+ if (sc->body >= 0) {
1689+ int bn = 0; const int *bb = nt_arr(nt, sc->body, "body", &bn);
1690+ if (bn > 0) pr = proc_ret_of(c, bb[bn - 1]);
1691+ }
1692+ for (int id = 0; id < nt->count; id++) {
1693+ const char *ty = nt_type(nt, id);
1694+ if (ty && !strcmp(ty, "ReturnNode") && comp_scope_of(c, id) == sc) {
1695+ int a = nt_ref(nt, id, "arguments"); int an = 0;
1696+ const int *av = a >= 0 ? nt_arr(nt, a, "arguments", &an) : NULL;
1697+ if (an > 0) pr = ty_unify(pr == TY_UNKNOWN ? TY_UNKNOWN : pr, proc_ret_of(c, av[0]));
1698+ }
1699+ }
1700+ if (pr != TY_UNKNOWN && sc->ret_proc_ret != (int)pr) { sc->ret_proc_ret = (int)pr; changed = 1; }
1701+ }
16601702 }
16611703 return changed;
16621704 }