Commit 895ac581

895ac581a530Merged

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

Description

Fix proc_ret stale-type bug when lambda body has inner locals

infer_write_types resets ALL non-param locals to TY_UNKNOWN at the start of each iteration. Previously, proc_ret was computed inline during the first pass: when f = lambda { case t when :a; x=10; x+1 } was processed, x had been reset to TY_UNKNOWN (not yet re-typed), so proc_node_ret saw TY_UNKNOWN for x+1 and computed TY_NIL for the case-without-else. This caused lv->proc_ret to be stuck at TY_NIL every iteration, making puts f.call(:a) emit "/* puts nil */".

Fix: move proc_ret computation to a second pass within infer_write_types, run after the first pass has typed all body-internal locals. This guarantees that x = 10 -> TY_INT is visible when proc_node_ret evaluates the lambda body return type.

Also add proc call-site param inference in infer_block_params so f.call(:a) propagates TY_SYMBOL to the proc param t.

Fixes case_local_in_lambda test.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
895ac581a530
Parent
507815b1f7ee

src/analyze.cmodifiedView file

@@ -3375,15 +3375,6 @@
33753375 continue;
33763376 }
33773377 lv->type = ty_unify(lv->type, newt);
3378- /* Record the proc's body return type so `<var>.call(...)` knows the
3379- result type (e.g. `sq = proc { |x| x*x }; sq.call(5)` -> int). Only
3380- a direct proc literal RHS carries it; escape-through-return/param is
3381- a later slice. */
3382- if (lv->type == TY_PROC && !strcmp(ty, "LocalVariableWriteNode")) {
3383- int vnode = nt_ref(nt, id, "value");
3384- TyKind pr = vnode >= 0 ? proc_ret_of(c, vnode) : TY_UNKNOWN;
3385- if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; }
3386- }
33873378 }
33883379
33893380 /* Multiple assignment `a, b = e0, e1`: each target gets its element's
@@ -3900,6 +3891,24 @@
39003891 if (*slot != before) changed = 1;
39013892 }
39023893
3894+ /* Second pass: re-compute proc_ret for proc-typed locals after body-internal
3895+ locals have been typed. The first pass resets all locals to TY_UNKNOWN, so
3896+ computing proc_ret there would see stale TY_UNKNOWN for variables assigned
3897+ inside the proc body. Running after the first pass ensures those locals
3898+ have their correct types (e.g. `x = 10` -> TY_INT) before proc_node_ret
3899+ evaluates the body's return type. */
3900+ for (int id = 0; id < nt->count; id++) {
3901+ const char *ty = nt_type(nt, id);
3902+ if (!ty || strcmp(ty, "LocalVariableWriteNode")) continue;
3903+ const char *nm = nt_str(nt, id, "name");
3904+ if (!nm) continue;
3905+ LocalVar *lv = scope_local(comp_scope_of(c, id), nm);
3906+ if (!lv || lv->type != TY_PROC) continue;
3907+ int vnode = nt_ref(nt, id, "value");
3908+ TyKind pr = vnode >= 0 ? proc_ret_of(c, vnode) : TY_UNKNOWN;
3909+ if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; }
3910+ }
3911+
39033912 /* detect change vs the stashed old types */
39043913 for (int s = 0; s < c->nscopes; s++)
39053914 for (int i = 0; i < c->scopes[s].nlocals; i++) {
@@ -4618,6 +4627,8 @@
46184627 return -1;
46194628 }
46204629
4630+static int a_proc_params_node(Compiler *c, int create); /* forward decl */
4631+
46214632 /* Bind block parameter types for supported iteration methods. */
46224633 static int infer_block_params(Compiler *c) {
46234634 const NodeTable *nt = c->nt;
@@ -4641,6 +4652,50 @@
46414652 }
46424653 }
46434654
4655+ /* Proc/lambda call-site param inference: `f.call(:a)` propagates arg types
4656+ to the proc's params (e.g. `t` gets TY_SYMBOL instead of the default TY_INT). */
4657+ for (int id = 0; id < nt->count; id++) {
4658+ const char *ty = nt_type(nt, id);
4659+ if (!ty || strcmp(ty, "CallNode")) continue;
4660+ const char *cname = nt_str(nt, id, "name");
4661+ if (!cname || (strcmp(cname, "call") && strcmp(cname, "()") && strcmp(cname, "[]"))) continue;
4662+ int recv = nt_ref(nt, id, "receiver");
4663+ if (recv < 0 || infer_type(c, recv) != TY_PROC) continue;
4664+ const char *rty = nt_type(nt, recv);
4665+ if (!rty || strcmp(rty, "LocalVariableReadNode")) continue;
4666+ const char *varname = nt_str(nt, recv, "name");
4667+ if (!varname) continue;
4668+ int call_args = nt_ref(nt, id, "arguments");
4669+ int argc = 0; const int *argv = NULL;
4670+ if (call_args >= 0) argv = nt_arr(nt, call_args, "arguments", &argc);
4671+ if (argc == 0) continue;
4672+ Scope *call_scope = comp_scope_of(c, id);
4673+ /* Find proc literal(s) assigned to varname in the same scope */
4674+ for (int w = 0; w < nt->count; w++) {
4675+ const char *wty = nt_type(nt, w);
4676+ if (!wty || strcmp(wty, "LocalVariableWriteNode")) continue;
4677+ const char *wname = nt_str(nt, w, "name");
4678+ if (!wname || strcmp(wname, varname)) continue;
4679+ if (comp_scope_of(c, w) != call_scope) continue;
4680+ int val = nt_ref(nt, w, "value");
4681+ if (val < 0 || !is_proc_create(c, val)) continue;
4682+ int pn = a_proc_params_node(c, val);
4683+ if (pn < 0) continue;
4684+ int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn);
4685+ Scope *bs = comp_scope_of(c, val);
4686+ for (int k = 0; k < rn && k < argc; k++) {
4687+ const char *p = nt_str(nt, reqs[k], "name");
4688+ if (!p) continue;
4689+ LocalVar *lv = scope_local(bs, p);
4690+ if (!lv) continue;
4691+ TyKind at = infer_type(c, argv[k]);
4692+ if (at == TY_UNKNOWN || at == lv->type) continue;
4693+ TyKind merged = ty_unify(lv->type, at);
4694+ if (merged != lv->type) { lv->type = merged; changed = 1; }
4695+ }
4696+ }
4697+ }
4698+
46444699 for (int id = 0; id < nt->count; id++) {
46454700 const char *ty = nt_type(nt, id);
46464701 if (!ty || strcmp(ty, "CallNode")) continue;