Commit 6bce1d16

6bce1d166a68Merged

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

Description

analyze: seed empty-hash params from index-or-write usage

h[k] ||= v (and &&= / op=) on a hash parameter passed in empty was rejected: an empty {} literal infers as TY_UNKNOWN, and the parameter was never seeded because the index-or-write nodes are not CallNodes, so infer_hash_params (which already seeds unknown params from [] / fetch / []= usage) never saw them. The method's return type then degraded to poly and the caller rejected the call.

infer_hash_params now also inspects IndexOrWriteNode / IndexAndWriteNode / IndexOperatorWriteNode on an unknown hash parameter, inferring the hash variant from the key and value types exactly as the []= case does. Because this runs inside the post-fixpoint re-inference loop, the seeded parameter type propagates and the method's return type resolves.

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

Details

Commit
6bce1d166a68
Parent
913a83318df6

src/analyze_pass.cmodifiedView file

@@ -1442,7 +1442,37 @@
14421442 };
14431443 for (int id = 0; id < nt->count; id++) {
14441444 const char *ty = nt_type(nt, id);
1445- if (!ty || strcmp(ty, "CallNode")) continue;
1445+ if (!ty) continue;
1446+ /* Index-or-write on an unknown param hash (h[k] ||= v / &&= / op=): infer the
1447+ variant from the key + value types, mirroring the []= case below. These are
1448+ not CallNodes, so the CallNode path never reaches them; without this a hash
1449+ passed in empty (`{}` infers TY_UNKNOWN) stays unresolved and the method's
1450+ return type degrades to poly, which the caller then rejects. */
1451+ if (!strcmp(ty, "IndexOrWriteNode") || !strcmp(ty, "IndexAndWriteNode") ||
1452+ !strcmp(ty, "IndexOperatorWriteNode")) {
1453+ int wrecv = nt_ref(nt, id, "receiver");
1454+ if (wrecv < 0) continue;
1455+ const char *wrty = nt_type(nt, wrecv);
1456+ if (!wrty || strcmp(wrty, "LocalVariableReadNode")) continue;
1457+ const char *wrnm = nt_str(nt, wrecv, "name");
1458+ if (!wrnm) continue;
1459+ Scope *ws = comp_scope_of(c, id);
1460+ LocalVar *wlv = scope_local(ws, wrnm);
1461+ if (!wlv || !wlv->is_param || wlv->type != TY_UNKNOWN) continue;
1462+ int wargs = nt_ref(nt, id, "arguments");
1463+ int wan = 0; const int *wargv = wargs >= 0 ? nt_arr(nt, wargs, "arguments", &wan) : NULL;
1464+ if (wan < 1) continue;
1465+ TyKind wkt = infer_type(c, wargv[0]);
1466+ TyKind wvt = infer_type(c, nt_ref(nt, id, "value"));
1467+ TyKind wwant = TY_UNKNOWN;
1468+ if (wkt == TY_STRING) wwant = (wvt == TY_STRING) ? TY_STR_STR_HASH : TY_STR_POLY_HASH;
1469+ else if (wkt == TY_SYMBOL) wwant = TY_SYM_POLY_HASH;
1470+ else if (wkt == TY_INT) wwant = TY_POLY_POLY_HASH;
1471+ if (wwant == TY_UNKNOWN) continue;
1472+ wlv->type = wwant; changed = 1;
1473+ continue;
1474+ }
1475+ if (strcmp(ty, "CallNode")) continue;
14461476 const char *name = nt_str(nt, id, "name");
14471477 if (!name) continue;
14481478 int recv = nt_ref(nt, id, "receiver");