Commit c848a813
c848a8133f0aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Merge pull request #1501 from mattn/fix-hash-literal-widen
analyze: widen a hash literal's type to its destination variable
Details
- Commit
- c848a8133f0a
- Parent
- 25c139b6f26c
- Merge parent
- eafe4aefa204
src/analyze.cmodifiedView file
@@ -2330,5 +2330,54 @@
23302330
TyKind lt = comp_ntype(c, st[n - 1]);23312331
if (ty_is_object(lt)) { int q = ty_object_class(lt); if (q >= 0 && q < c->nclasses) c->classes[q].is_value_type = 0; }23322332
}2333+
2334+
/* Reconcile a hash literal's node type with the variable it initializes.2335+
A literal like `{ begin: ... }` infers a narrow variant (SYM_POLY_HASH)2336+
from its own keys, but the variable may later be promoted to a wider hash2337+
(e.g. POLY_POLY_HASH from a mixed-key `dic[sym_or_int] = ...`). Codegen2338+
emits the literal from its own node type, so without this the constructor2339+
(sp_SymPolyHash_new) disagrees with the variable's declared C type2340+
(sp_PolyPolyHash *) -- an incompatible-pointer assignment that corrupts2341+
the hash at runtime. Align the literal to the variable's type so the right2342+
constructor and key/value boxing are emitted. Widen only (the variable2343+
type is the union over all writes, so it is never narrower). */2344+
for (int id = 0; id < c->nt->count; id++) {2345+
const char *ty = nt_type(c->nt, id);2346+
if (!ty) continue;2347+
int is_lv = !strcmp(ty, "LocalVariableWriteNode") ||2348+
!strcmp(ty, "LocalVariableOrWriteNode") ||2349+
!strcmp(ty, "LocalVariableAndWriteNode") ||2350+
!strcmp(ty, "LocalVariableOperatorWriteNode");2351+
int is_iv = !strcmp(ty, "InstanceVariableWriteNode") ||2352+
!strcmp(ty, "InstanceVariableOrWriteNode") ||2353+
!strcmp(ty, "InstanceVariableAndWriteNode") ||2354+
!strcmp(ty, "InstanceVariableOperatorWriteNode");2355+
if (!is_lv && !is_iv) continue;2356+
int v = nt_ref(c->nt, id, "value");2357+
if (v < 0) continue;2358+
const char *vty = nt_type(c->nt, v);2359+
if (!vty || (strcmp(vty, "HashNode") && strcmp(vty, "KeywordHashNode"))) continue;2360+
TyKind litt = c->ntype[v];2361+
if (!ty_is_hash(litt)) continue;2362+
const char *nm = nt_str(c->nt, id, "name");2363+
if (!nm) continue;2364+
TyKind dstt = TY_UNKNOWN;2365+
if (is_lv) {2366+
Scope *s = comp_scope_of(c, id);2367+
LocalVar *lv = s ? scope_local(s, nm) : NULL;2368+
if (lv) dstt = lv->type;2369+
} else {2370+
Scope *s = comp_scope_of(c, id);2371+
if (s && s->class_id >= 0 && s->class_id < c->nclasses) {2372+
int iv = comp_ivar_index(&c->classes[s->class_id], nm);2373+
if (iv >= 0) dstt = c->classes[s->class_id].ivar_types[iv];2374+
}2375+
}2376+
/* Only widen toward POLY_POLY_HASH, the universally-boxed variant whose2377+
constructor accepts any keys/values -- a safe target for any narrower2378+
literal. Other cross-variant widenings are left alone. */2379+
if (dstt == TY_POLY_POLY_HASH && litt != TY_POLY_POLY_HASH)2380+
c->ntype[v] = dstt;2381+
}23332382
}23342383