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
continue; } lv->type = ty_unify(lv->type, newt); /* Record the proc's body return type so `<var>.call(...)` knows the result type (e.g. `sq = proc { |x| x*x }; sq.call(5)` -> int). Only a direct proc literal RHS carries it; escape-through-return/param is a later slice. */ if (lv->type == TY_PROC && !strcmp(ty, "LocalVariableWriteNode")) { int vnode = nt_ref(nt, id, "value"); TyKind pr = vnode >= 0 ? proc_ret_of(c, vnode) : TY_UNKNOWN; if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; } } } /* Multiple assignment `a, b = e0, e1`: each target gets its element's if (*slot != before) changed = 1; } /* Second pass: re-compute proc_ret for proc-typed locals after body-internal locals have been typed. The first pass resets all locals to TY_UNKNOWN, so computing proc_ret there would see stale TY_UNKNOWN for variables assigned inside the proc body. Running after the first pass ensures those locals have their correct types (e.g. `x = 10` -> TY_INT) before proc_node_ret evaluates the body's return type. */ for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "LocalVariableWriteNode")) continue; const char *nm = nt_str(nt, id, "name"); if (!nm) continue; LocalVar *lv = scope_local(comp_scope_of(c, id), nm); if (!lv || lv->type != TY_PROC) continue; int vnode = nt_ref(nt, id, "value"); TyKind pr = vnode >= 0 ? proc_ret_of(c, vnode) : TY_UNKNOWN; if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; } } /* detect change vs the stashed old types */ for (int s = 0; s < c->nscopes; s++) for (int i = 0; i < c->scopes[s].nlocals; i++) { return -1;}static int a_proc_params_node(Compiler *c, int create); /* forward decl *//* Bind block parameter types for supported iteration methods. */static int infer_block_params(Compiler *c) { const NodeTable *nt = c->nt; } } /* Proc/lambda call-site param inference: `f.call(:a)` propagates arg types to the proc's params (e.g. `t` gets TY_SYMBOL instead of the default TY_INT). */ for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "CallNode")) continue; const char *cname = nt_str(nt, id, "name"); if (!cname || (strcmp(cname, "call") && strcmp(cname, "()") && strcmp(cname, "[]"))) continue; int recv = nt_ref(nt, id, "receiver"); if (recv < 0 || infer_type(c, recv) != TY_PROC) continue; const char *rty = nt_type(nt, recv); if (!rty || strcmp(rty, "LocalVariableReadNode")) continue; const char *varname = nt_str(nt, recv, "name"); if (!varname) continue; int call_args = nt_ref(nt, id, "arguments"); int argc = 0; const int *argv = NULL; if (call_args >= 0) argv = nt_arr(nt, call_args, "arguments", &argc); if (argc == 0) continue; Scope *call_scope = comp_scope_of(c, id); /* Find proc literal(s) assigned to varname in the same scope */ for (int w = 0; w < nt->count; w++) { const char *wty = nt_type(nt, w); if (!wty || strcmp(wty, "LocalVariableWriteNode")) continue; const char *wname = nt_str(nt, w, "name"); if (!wname || strcmp(wname, varname)) continue; if (comp_scope_of(c, w) != call_scope) continue; int val = nt_ref(nt, w, "value"); if (val < 0 || !is_proc_create(c, val)) continue; int pn = a_proc_params_node(c, val); if (pn < 0) continue; int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn); Scope *bs = comp_scope_of(c, val); for (int k = 0; k < rn && k < argc; k++) { const char *p = nt_str(nt, reqs[k], "name"); if (!p) continue; LocalVar *lv = scope_local(bs, p); if (!lv) continue; TyKind at = infer_type(c, argv[k]); if (at == TY_UNKNOWN || at == lv->type) continue; TyKind merged = ty_unify(lv->type, at); if (merged != lv->type) { lv->type = merged; changed = 1; } } } } for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "CallNode")) continue;