Commit 6cbe880f

6cbe880fc0cdMerged

Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

analyze: type a forwarded callable's params and arity-match hash forwarding

Close the remaining callable-forwarding gaps by typing a forwarded proc/lambda's params precisely:

  • A concrete call argument overrides a param still at its bare-int default (the fallback guess) instead of unifying to a POLY scalar -- so a symbol key, string element, or pair array reaches the param with its real type. A genuinely multi-type param still converges to POLY via the other call site.
  • The call-site param inference also types an inline lambda receiver's own params (a desugared &->(x){...} clone has no var write to find), fixing inline-lambda forwarding into arrays of any element type -- previously the clone's param defaulted to int and mistyped non-int elements.
  • Hash forwarding now covers proc/lambda values and inline lambdas across each / each_key / each_value: a 1-param callable gets the [k,v] pair as one array, a 2-param one gets k and v positionally (matching CRuby's proc auto-splat).

Details

Commit
6cbe880fc0cd
Parent
4431c2023866

src/analyze_pass.cmodifiedView file

@@ -2110,9 +2110,19 @@
21102110 typing not yet modeled, so decline to the pre-existing path. */
21112111 int wrap_pair = 0;
21122112 if (ty_is_hash(rt)) {
2113- if (ct == TY_METHOD) wrap_pair = (arity == 2);
2114- else if (arity == 2 && simple_ref && fwd_callable_arity(c, ex) == 1) wrap_pair = 1;
2115- else continue;
2113+ if (ct == TY_METHOD) {
2114+ wrap_pair = (arity == 2); /* each: pair as array; each_key/value: bare value */
2115+ } else {
2116+ /* a proc/lambda (value or inline): the call-site inference types its
2117+ params from the forwarded call. `each` to a 1-param callable gets the
2118+ [k,v] pair as one array; to a 2-param one, k and v positionally
2119+ (auto-splat by arity); each_key/each_value pass the bare key/value. */
2120+ int cpc = fwd_callable_arity(c, ex);
2121+ if (arity == 2 && cpc == 1) wrap_pair = 1;
2122+ else if (arity == 2 && cpc == 2) wrap_pair = 0;
2123+ else if (arity == 1) wrap_pair = 0;
2124+ else continue; /* arity-2 with unresolved callable arity */
2125+ }
21162126 }
21172127
21182128 int base = nt->count;
@@ -2221,6 +2231,30 @@
22212231 return changed;
22222232 }
22232233
2234+/* Propagate `proc.call(args)` argument types onto the proc literal `create`'s
2235+ required params: a concrete arg overrides a param still at its bare-int
2236+ default (the fallback guess, no real evidence), otherwise unify. Returns 1 if
2237+ any param type changed. Shared by the local-proc and inline-lambda call sites. */
2238+static int cs_type_params(Compiler *c, int create, const int *argv, int argc) {
2239+ NodeTable *nt = (NodeTable *)c->nt;
2240+ int pn = a_proc_params_node(c, create);
2241+ if (pn < 0) return 0;
2242+ int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn);
2243+ Scope *bs = comp_scope_of(c, create);
2244+ int changed = 0;
2245+ for (int k = 0; k < rn && k < argc; k++) {
2246+ const char *p = nt_str(nt, reqs[k], "name");
2247+ if (!p) continue;
2248+ LocalVar *lv = scope_local(bs, p);
2249+ if (!lv) continue;
2250+ TyKind at = infer_type(c, argv[k]);
2251+ if (at == TY_UNKNOWN || at == lv->type) continue;
2252+ TyKind merged = (lv->type == TY_INT) ? at : ty_unify(lv->type, at);
2253+ if (merged != lv->type) { lv->type = merged; changed = 1; }
2254+ }
2255+ return changed;
2256+}
2257+
22242258 int infer_block_params(Compiler *c) {
22252259 const NodeTable *nt = c->nt;
22262260 int changed = 0;
@@ -2451,15 +2485,23 @@
24512485 int recv = nt_ref(nt, id, "receiver");
24522486 if (recv < 0 || infer_type(c, recv) != TY_PROC) continue;
24532487 const char *rty = nt_type(nt, recv);
2454- if (!rty || strcmp(rty, "LocalVariableReadNode")) continue;
2455- const char *varname = nt_str(nt, recv, "name");
2456- if (!varname) continue;
2488+ if (!rty) continue;
24572489 int call_args = nt_ref(nt, id, "arguments");
24582490 int argc = 0; const int *argv = NULL;
24592491 if (call_args >= 0) argv = nt_arr(nt, call_args, "arguments", &argc);
24602492 if (argc == 0) continue;
2493+ /* The receiver is itself a proc/lambda literal -- e.g. a desugared inline
2494+ `&->(x){...}` clone, whose params no var write would let us find -- so type
2495+ its own params directly from the call args. */
2496+ if (!strcmp(rty, "LambdaNode") || is_proc_create(c, recv)) {
2497+ if (cs_type_params(c, recv, argv, argc)) changed = 1;
2498+ continue;
2499+ }
2500+ if (strcmp(rty, "LocalVariableReadNode")) continue;
2501+ const char *varname = nt_str(nt, recv, "name");
2502+ if (!varname) continue;
24612503 Scope *call_scope = comp_scope_of(c, id);
2462- /* Find proc literal(s) assigned to varname in the same scope */
2504+ /* otherwise a local holding a proc: type the proc literal assigned to it */
24632505 for (int w = 0; w < nt->count; w++) {
24642506 const char *wty = nt_type(nt, w);
24652507 if (!wty || strcmp(wty, "LocalVariableWriteNode")) continue;
@@ -2468,27 +2510,7 @@
24682510 if (comp_scope_of(c, w) != call_scope) continue;
24692511 int val = nt_ref(nt, w, "value");
24702512 if (val < 0 || !is_proc_create(c, val)) continue;
2471- int pn = a_proc_params_node(c, val);
2472- if (pn < 0) continue;
2473- int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn);
2474- Scope *bs = comp_scope_of(c, val);
2475- for (int k = 0; k < rn && k < argc; k++) {
2476- const char *p = nt_str(nt, reqs[k], "name");
2477- if (!p) continue;
2478- LocalVar *lv = scope_local(bs, p);
2479- if (!lv) continue;
2480- TyKind at = infer_type(c, argv[k]);
2481- if (at == TY_UNKNOWN || at == lv->type) continue;
2482- /* A container arg (array/hash/string/object) overrides a bare int param:
2483- the int is the proc/lambda default, which would otherwise unify with a
2484- container to POLY (a scalar) and lose the container type -- breaking a
2485- forwarded `&proc` into a poly/hash iterator, whose param is the pair
2486- array or element. A genuine multi-type param still converges to POLY
2487- via the other call site's unify. */
2488- int at_container = ty_is_array(at) || ty_is_hash(at) || at == TY_STRING || ty_is_object(at);
2489- TyKind merged = (lv->type == TY_INT && at_container) ? at : ty_unify(lv->type, at);
2490- if (merged != lv->type) { lv->type = merged; changed = 1; }
2491- }
2513+ if (cs_type_params(c, val, argv, argc)) changed = 1;
24922514 }
24932515 }
24942516