Commit 16798029
1679802946bdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #1493 from ryanseys/rs-g17-empty-hash-orwrite
Seed empty-hash parameters from index-or-write usage
Details
- Commit
- 1679802946bd
- Parent
- 70231669e78f
- Merge parent
- 6bce1d166a68
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 the1447+
variant from the key + value types, mirroring the []= case below. These are1448+
not CallNodes, so the CallNode path never reaches them; without this a hash1449+
passed in empty (`{}` infers TY_UNKNOWN) stays unresolved and the method's1450+
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");test/index_or_write_empty_hash.rbaddedView file
Loading diff…
test/index_or_write_empty_hash.rb.expectedaddedView file
Loading diff…