Commit d68aba18
d68aba184eb4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Fix Hash.new type inference and sym/str key mismatch on typed hashes
- analyze.c: Hash.new returns TY_UNKNOWN so body-usage key patterns determine the hash variant (sym, str, or int) instead of seeding TY_SYM_POLY_HASH unconditionally.
- analyze.c: infer_write_types: when
d = h.dup/clonecan't resolve the receiver type in the current iteration (h is TY_UNKNOWN after reset), fall back to h's gc_root (prior-iteration stable type). - codegen.c: hash
[]with a statically-mismatched key type (e.g. sym key on a str-keyed hash) now returns the hash's default_v instead of passing NULL (sym-id 0) to sp_str_hash and segfaulting. key? / has_key? already had this guard; [] was missing it.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- d68aba184eb4
- Parent
- 89af54f49b71
src/analyze.cmodifiedView file
@@ -372,7 +372,7 @@
372372
if (cn && !strcmp(cn, "String")) return TY_STRING;373373
if (cn && !strcmp(cn, "StringIO")) return TY_STRINGIO;374374
if (cn && !strcmp(cn, "StringScanner")) return TY_STRINGSCANNER;375-
if (cn && !strcmp(cn, "Hash")) return TY_SYM_POLY_HASH;375+
if (cn && !strcmp(cn, "Hash")) return TY_UNKNOWN; /* hash type determined by key usage */376376
if (cn && !strcmp(cn, "Regexp")) return TY_REGEX;377377
}378378
}@@ -2437,6 +2437,23 @@
24372437
LocalVar *lv2 = scope_local(s2, nm);24382438
if (lv2 && (TyKind)lv2->gc_root != TY_UNKNOWN) newt = (TyKind)lv2->gc_root;24392439
}2440+
/* `d = h.dup/clone`: inherit receiver's hash type from prior iteration */2441+
if (newt == TY_UNKNOWN) {2442+
const char *rvty2 = nt_type(nt, val_id);2443+
if (rvty2 && !strcmp(rvty2, "CallNode")) {2444+
const char *rvnm2 = nt_str(nt, val_id, "name");2445+
int rvrecv2 = nt_ref(nt, val_id, "receiver");2446+
if (rvrecv2 >= 0 && rvnm2 &&2447+
(!strcmp(rvnm2, "dup") || !strcmp(rvnm2, "clone"))) {2448+
const char *rrt2 = nt_type(nt, rvrecv2);2449+
if (rrt2 && !strcmp(rrt2, "LocalVariableReadNode")) {2450+
const char *rrn2 = nt_str(nt, rvrecv2, "name");2451+
LocalVar *rlv2 = rrn2 ? scope_local(comp_scope_of(c, rvrecv2), rrn2) : NULL;2452+
if (rlv2 && ty_is_hash((TyKind)rlv2->gc_root)) newt = (TyKind)rlv2->gc_root;2453+
}2454+
}2455+
}2456+
}24402457
}24412458
}24422459
else if (!strcmp(ty, "LocalVariableOperatorWriteNode")) {src/codegen.cmodifiedView file
Loading diff…