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
}; for (int id = 0; id < nt->count; id++) { const char *ty = nt_type(nt, id); if (!ty || strcmp(ty, "CallNode")) continue; if (!ty) continue; /* Index-or-write on an unknown param hash (h[k] ||= v / &&= / op=): infer the variant from the key + value types, mirroring the []= case below. These are not CallNodes, so the CallNode path never reaches them; without this a hash passed in empty (`{}` infers TY_UNKNOWN) stays unresolved and the method's return type degrades to poly, which the caller then rejects. */ if (!strcmp(ty, "IndexOrWriteNode") || !strcmp(ty, "IndexAndWriteNode") || !strcmp(ty, "IndexOperatorWriteNode")) { int wrecv = nt_ref(nt, id, "receiver"); if (wrecv < 0) continue; const char *wrty = nt_type(nt, wrecv); if (!wrty || strcmp(wrty, "LocalVariableReadNode")) continue; const char *wrnm = nt_str(nt, wrecv, "name"); if (!wrnm) continue; Scope *ws = comp_scope_of(c, id); LocalVar *wlv = scope_local(ws, wrnm); if (!wlv || !wlv->is_param || wlv->type != TY_UNKNOWN) continue; int wargs = nt_ref(nt, id, "arguments"); int wan = 0; const int *wargv = wargs >= 0 ? nt_arr(nt, wargs, "arguments", &wan) : NULL; if (wan < 1) continue; TyKind wkt = infer_type(c, wargv[0]); TyKind wvt = infer_type(c, nt_ref(nt, id, "value")); TyKind wwant = TY_UNKNOWN; if (wkt == TY_STRING) wwant = (wvt == TY_STRING) ? TY_STR_STR_HASH : TY_STR_POLY_HASH; else if (wkt == TY_SYMBOL) wwant = TY_SYM_POLY_HASH; else if (wkt == TY_INT) wwant = TY_POLY_POLY_HASH; if (wwant == TY_UNKNOWN) continue; wlv->type = wwant; changed = 1; continue; } if (strcmp(ty, "CallNode")) continue; const char *name = nt_str(nt, id, "name"); if (!name) continue; int recv = nt_ref(nt, id, "receiver");test/index_or_write_empty_hash.rbaddedView file
Loading diff…
test/index_or_write_empty_hash.rb.expectedaddedView file
Loading diff…