Commit 3cfa76fe
3cfa76fe6ee1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Fix hash block method codegen: stale type cache + box-on-assign
- emit_hash_collect_expr: use scope-patching to temporarily set block param LocalVar.type before calling infer_type, so arithmetic on typed hash values (e.g. v*10 where v is TY_INT from str_int_hash) uses int_arith_fn instead of poly_mul
- emit_transform_hash_expr: remove shadow mechanism (it caused type inconsistency between inner narrowed type and outer stale-poly type); instead add box-on-assign when scope declares param as TY_POLY but hash value is typed (e.g. str_int_hash → box int to sp_RbVal)
- Arithmetic stale cache fix: compute eff_res from fresh rt/a0 when res (from comp_ntype cache) is not a basic numeric type, preventing TY_INT_ARRAY stale value from selecting wrong arithmetic path
Fixes hash_block_methods_variants test.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 3cfa76fe6ee1
- Parent
- 6a51743721bb
src/codegen.cmodifiedView file
@@ -637,6 +637,9 @@
637637
buf_puts(b, ")");638638
}639639
640+
static int patch_lv_reads(Compiler *c, int id, const char *nm, TyKind ty,641+
int *ids_out, TyKind *ty_out, int cap);642+
640643
/* hash.map / collect { |k, v| ... } as an expression -> an array of the block641644
values, built via a loop over the hash entries in the statement prelude. */642645
static int emit_hash_collect_expr(Compiler *c, int id, Buf *b) {@@ -651,36 +654,101 @@
651654
TyKind rt = comp_ntype(c, recv);652655
const char *hn = ty_hash_cname(rt);653656
if (!hn) return 0;654-
const char *p0 = block_param_name(c, block, 0); if (p0) p0 = rename_local(p0);655-
const char *p1 = block_param_name(c, block, 1); if (p1) p1 = rename_local(p1);657+
TyKind kt = ty_hash_key(rt), vt = ty_hash_val(rt);658+
(void)kt;659+
660+
const char *p0_orig = block_param_name(c, block, 0);661+
const char *p1_orig = block_param_name(c, block, 1);662+
const char *p0 = p0_orig ? rename_local(p0_orig) : NULL;663+
const char *p1 = p1_orig ? rename_local(p1_orig) : NULL;664+
656665
int body = nt_ref(nt, block, "body");657666
int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;658667
if (bn < 1) return 0;668+
669+
/* Detect type mismatch: block params unified with other blocks in same scope.670+
For 2-param |k,v|: p0=key, p1=value. For 1-param |v|: p0=value. */671+
Scope *pscope = comp_scope_of(c, block);672+
TyKind p0_actual = p1_orig ? kt : vt; /* p0 is key if 2 params, else value */673+
TyKind p1_actual = vt;674+
LocalVar *p0_lv = p0_orig ? scope_local(pscope, p0_orig) : NULL;675+
LocalVar *p1_lv = p1_orig ? scope_local(pscope, p1_orig) : NULL;676+
TyKind p0_decl = p0_lv ? p0_lv->type : TY_UNKNOWN;677+
TyKind p1_decl = p1_lv ? p1_lv->type : TY_UNKNOWN;678+
int ns0 = p0_orig && p0_actual != TY_UNKNOWN && p0_decl != TY_UNKNOWN && p0_decl != p0_actual;679+
int ns1 = p1_orig && p1_actual != TY_UNKNOWN && p1_decl != TY_UNKNOWN && p1_decl != p1_actual;680+
int st0 = -1, sri0 = -1, srn0 = 0; char sro0[112]; sro0[0] = '\0';681+
int st1 = -1, sri1 = -1, srn1 = 0; char sro1[112]; sro1[0] = '\0';682+
659683
int trecv = ++g_tmp, tres = ++g_tmp, ti = ++g_tmp;660-
emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre); buf_printf(g_pre, " _t%d = ", trecv); emit_expr(c, recv, g_pre); buf_puts(g_pre, ";\n");684+
{ Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb);685+
emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre);686+
buf_printf(g_pre, " _t%d = ", trecv); buf_puts(g_pre, rb.p ? rb.p : ""); buf_puts(g_pre, ";\n"); free(rb.p); }687+
661688
if (is_sel || is_rej) {662689
/* select/reject: produce a same-type hash with matching pairs */663-
emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre); buf_printf(g_pre, " _t%d = sp_%sHash_new();\n", tres, hn);690+
emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre);691+
buf_printf(g_pre, " _t%d = sp_%sHash_new();\n", tres, hn);664692
emit_indent(g_pre, g_indent); buf_printf(g_pre, "SP_GC_ROOT(_t%d);\n", tres);665-
emit_indent(g_pre, g_indent); buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < _t%d->len; _t%d++) {\n", ti, ti, trecv, ti);666-
if (p0) { emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "lv_%s = _t%d->order[_t%d];\n", p0, trecv, ti); }667-
if (p1) { emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "lv_%s = sp_%sHash_get(_t%d, _t%d->order[_t%d]);\n", p1, hn, trecv, trecv, ti); }693+
emit_indent(g_pre, g_indent);694+
buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < _t%d->len; _t%d++) {\n", ti, ti, trecv, ti);695+
if (p0_orig) {696+
emit_indent(g_pre, g_indent + 1);697+
if (ns0) {698+
st0 = ++g_tmp; emit_ctype(c, p0_actual, g_pre);699+
buf_printf(g_pre, " lv__bp%d = _t%d->order[_t%d];\n", st0, trecv, ti);700+
for (int ri = 0; ri < g_nren; ri++) {701+
if (strcmp(g_ren_from[ri], p0_orig) == 0) {702+
sri0 = ri; strncpy(sro0, g_ren_to[ri], sizeof sro0 - 1);703+
snprintf(g_ren_to[ri], sizeof g_ren_to[0], "_bp%d", st0); break;704+
}705+
}706+
if (sri0 < 0) { sri0 = g_nren; srn0 = 1;707+
snprintf(g_ren_from[g_nren], sizeof g_ren_from[0], "%s", p0_orig);708+
snprintf(g_ren_to[g_nren++], sizeof g_ren_to[0], "_bp%d", st0);709+
}710+
}711+
else buf_printf(g_pre, "lv_%s = _t%d->order[_t%d];\n", p0, trecv, ti);712+
}713+
if (p1_orig) {714+
emit_indent(g_pre, g_indent + 1);715+
if (ns1) {716+
st1 = ++g_tmp; emit_ctype(c, p1_actual, g_pre);717+
buf_printf(g_pre, " lv__bp%d = sp_%sHash_get(_t%d, _t%d->order[_t%d]);\n", st1, hn, trecv, trecv, ti);718+
for (int ri = 0; ri < g_nren; ri++) {719+
if (strcmp(g_ren_from[ri], p1_orig) == 0) {720+
sri1 = ri; strncpy(sro1, g_ren_to[ri], sizeof sro1 - 1);721+
snprintf(g_ren_to[ri], sizeof g_ren_to[0], "_bp%d", st1); break;722+
}723+
}724+
if (sri1 < 0) { sri1 = g_nren; srn1 = 1;725+
snprintf(g_ren_from[g_nren], sizeof g_ren_from[0], "%s", p1_orig);726+
snprintf(g_ren_to[g_nren++], sizeof g_ren_to[0], "_bp%d", st1);727+
}728+
}729+
else buf_printf(g_pre, "lv_%s = sp_%sHash_get(_t%d, _t%d->order[_t%d]);\n", p1, hn, trecv, trecv, ti);730+
}731+
if (ns0 && p0_lv) p0_lv->type = p0_actual;732+
if (ns1 && p1_lv) p1_lv->type = p1_actual;668733
for (int j = 0; j < bn - 1; j++) emit_stmt(c, bb[j], g_pre, g_indent + 1);669734
int save = g_indent; g_indent++;670735
Buf vb; memset(&vb, 0, sizeof vb); emit_expr(c, bb[bn - 1], &vb); g_indent = save;736+
if (ns0 && p0_lv) p0_lv->type = p0_decl;737+
if (ns1 && p1_lv) p1_lv->type = p1_decl;738+
if (sri1 >= 0) { if (srn1) g_nren = sri1; else strncpy(g_ren_to[sri1], sro1, sizeof g_ren_to[0]-1); }739+
if (sri0 >= 0) { if (srn0) g_nren = sri0; else strncpy(g_ren_to[sri0], sro0, sizeof g_ren_to[0]-1); }671740
emit_indent(g_pre, g_indent + 1);672-
/* emit: if (cond) sp_XXHash_set(out, key, val); */673-
TyKind kt = ty_hash_key(rt), vt = ty_hash_val(rt);741+
TyKind vtt = ty_hash_val(rt);674742
buf_printf(g_pre, "if (%s(%s)) { ", is_rej ? "!" : "", vb.p ? vb.p : "0"); free(vb.p);675743
if (rt == TY_POLY_POLY_HASH) {676744
buf_printf(g_pre, "sp_%sHash_set(_t%d, ", hn, tres);677-
if (p0) buf_printf(g_pre, "lv_%s", p0); else buf_printf(g_pre, "_t%d->order[_t%d]", trecv, ti);745+
if (p0_orig) buf_printf(g_pre, "_t%d->order[_t%d]", trecv, ti);746+
else buf_printf(g_pre, "_t%d->order[_t%d]", trecv, ti);678747
buf_printf(g_pre, ", sp_%sHash_get(_t%d, _t%d->order[_t%d])); }", hn, trecv, trecv, ti);679748
}680749
else {681750
buf_printf(g_pre, "sp_%sHash_set(_t%d, _t%d->order[_t%d], sp_%sHash_%s(_t%d, _t%d->order[_t%d])); }",682-
hn, tres, trecv, ti, hn, vt == TY_INT ? "get_opt" : "get", trecv, trecv, ti);683-
(void)kt;751+
hn, tres, trecv, ti, hn, vtt == TY_INT ? "get_opt" : "get", trecv, trecv, ti);684752
}685753
buf_puts(g_pre, "\n");686754
emit_indent(g_pre, g_indent); buf_puts(g_pre, "}\n");@@ -691,17 +759,71 @@
691759
int res_poly = (restype == TY_POLY_ARRAY);692760
const char *rk = res_poly ? "Poly" : array_kind(restype);693761
if (!rk) return 0;694-
emit_indent(g_pre, g_indent); buf_printf(g_pre, "sp_%sArray *_t%d = sp_%sArray_new();\n", rk, tres, rk);762+
emit_indent(g_pre, g_indent);763+
buf_printf(g_pre, "sp_%sArray *_t%d = sp_%sArray_new();\n", rk, tres, rk);695764
emit_indent(g_pre, g_indent); buf_printf(g_pre, "SP_GC_ROOT(_t%d);\n", tres);696-
emit_indent(g_pre, g_indent); buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < _t%d->len; _t%d++) {\n", ti, ti, trecv, ti);697-
if (p0) { emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "lv_%s = _t%d->order[_t%d];\n", p0, trecv, ti); }698-
if (p1) { emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "lv_%s = sp_%sHash_get(_t%d, _t%d->order[_t%d]);\n", p1, hn, trecv, trecv, ti); }765+
emit_indent(g_pre, g_indent);766+
buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < _t%d->len; _t%d++) {\n", ti, ti, trecv, ti);767+
if (p0_orig) {768+
emit_indent(g_pre, g_indent + 1);769+
if (ns0) {770+
st0 = ++g_tmp; emit_ctype(c, p0_actual, g_pre);771+
if (p1_orig) buf_printf(g_pre, " lv__bp%d = _t%d->order[_t%d];\n", st0, trecv, ti);772+
else buf_printf(g_pre, " lv__bp%d = sp_%sHash_get(_t%d, _t%d->order[_t%d]);\n", st0, hn, trecv, trecv, ti);773+
for (int ri = 0; ri < g_nren; ri++) {774+
if (strcmp(g_ren_from[ri], p0_orig) == 0) {775+
sri0 = ri; strncpy(sro0, g_ren_to[ri], sizeof sro0 - 1);776+
snprintf(g_ren_to[ri], sizeof g_ren_to[0], "_bp%d", st0); break;777+
}778+
}779+
if (sri0 < 0) { sri0 = g_nren; srn0 = 1;780+
snprintf(g_ren_from[g_nren], sizeof g_ren_from[0], "%s", p0_orig);781+
snprintf(g_ren_to[g_nren++], sizeof g_ren_to[0], "_bp%d", st0);782+
}783+
}784+
else {785+
if (p1_orig) buf_printf(g_pre, "lv_%s = _t%d->order[_t%d];\n", p0, trecv, ti);786+
else buf_printf(g_pre, "lv_%s = sp_%sHash_get(_t%d, _t%d->order[_t%d]);\n", p0, hn, trecv, trecv, ti);787+
}788+
}789+
if (p1_orig) {790+
emit_indent(g_pre, g_indent + 1);791+
if (ns1) {792+
st1 = ++g_tmp; emit_ctype(c, p1_actual, g_pre);793+
buf_printf(g_pre, " lv__bp%d = sp_%sHash_get(_t%d, _t%d->order[_t%d]);\n", st1, hn, trecv, trecv, ti);794+
for (int ri = 0; ri < g_nren; ri++) {795+
if (strcmp(g_ren_from[ri], p1_orig) == 0) {796+
sri1 = ri; strncpy(sro1, g_ren_to[ri], sizeof sro1 - 1);797+
snprintf(g_ren_to[ri], sizeof g_ren_to[0], "_bp%d", st1); break;798+
}799+
}800+
if (sri1 < 0) { sri1 = g_nren; srn1 = 1;801+
snprintf(g_ren_from[g_nren], sizeof g_ren_from[0], "%s", p1_orig);802+
snprintf(g_ren_to[g_nren++], sizeof g_ren_to[0], "_bp%d", st1);803+
}804+
}805+
else buf_printf(g_pre, "lv_%s = sp_%sHash_get(_t%d, _t%d->order[_t%d]);\n", p1, hn, trecv, trecv, ti);806+
}807+
/* Temporarily set scope LocalVar types so infer_type reads the shadow type.808+
This ensures both c->ntype cache and emit_call see the correct operand types. */809+
if (ns0 && p0_lv) p0_lv->type = p0_actual;810+
if (ns1 && p1_lv) p1_lv->type = p1_actual;811+
TyKind bret = infer_type(c, bb[bn - 1]);812+
if (bret == TY_UNKNOWN) bret = (ns1 ? p1_actual : p0_actual);699813
for (int j = 0; j < bn - 1; j++) emit_stmt(c, bb[j], g_pre, g_indent + 1);700814
int save = g_indent; g_indent++;701815
Buf vb; memset(&vb, 0, sizeof vb); emit_expr(c, bb[bn - 1], &vb); g_indent = save;816+
if (ns0 && p0_lv) p0_lv->type = p0_decl;817+
if (ns1 && p1_lv) p1_lv->type = p1_decl;818+
if (sri1 >= 0) { if (srn1) g_nren = sri1; else strncpy(g_ren_to[sri1], sro1, sizeof g_ren_to[0]-1); }819+
if (sri0 >= 0) { if (srn0) g_nren = sri0; else strncpy(g_ren_to[sri0], sro0, sizeof g_ren_to[0]-1); }702820
emit_indent(g_pre, g_indent + 1);703821
buf_printf(g_pre, "sp_%sArray_push(_t%d, ", rk, tres);704-
if (res_poly && comp_ntype(c, bb[bn - 1]) != TY_POLY) { Buf bx; memset(&bx, 0, sizeof bx); emit_boxed_text(c, comp_ntype(c, bb[bn - 1]), vb.p ? vb.p : "", &bx); buf_puts(g_pre, bx.p ? bx.p : ""); free(bx.p); }822+
if (res_poly && bret != TY_POLY) {823+
Buf bx; memset(&bx, 0, sizeof bx);824+
emit_boxed_text(c, bret, vb.p ? vb.p : "", &bx);825+
buf_puts(g_pre, bx.p ? bx.p : ""); free(bx.p);826+
}705827
else buf_puts(g_pre, vb.p ? vb.p : "");706828
buf_puts(g_pre, ");\n"); free(vb.p);707829
emit_indent(g_pre, g_indent); buf_puts(g_pre, "}\n");@@ -785,15 +907,12 @@
785907
}786908
TyKind skt = ty_hash_key(rt), svt = ty_hash_val(rt);787909
TyKind dvt = ty_hash_val(dt);788-
/* Detect type mismatch: block param unified with other blocks in same scope */789-
TyKind param_ty = keys ? skt : svt;790-
Scope *pscope = comp_scope_of(c, block);791-
LocalVar *param_lv = p0_orig ? scope_local(pscope, p0_orig) : NULL;792-
TyKind declared_ty = param_lv ? param_lv->type : TY_UNKNOWN;793-
int need_shadow = p0_orig && param_ty != TY_UNKNOWN &&794-
declared_ty != TY_UNKNOWN && declared_ty != param_ty;795-
int shadow_tmp = -1, shadow_ren_idx = -1, shadow_ren_new = 0;796-
char shadow_ren_old[112]; shadow_ren_old[0] = '\0';910+
/* When the scope declares v as TY_POLY but the hash has typed (non-poly) values,911+
box the value on assignment so the poly variable receives sp_RbVal. */912+
Scope *pscope_tv = comp_scope_of(c, block);913+
LocalVar *p0_lv_tv = p0_orig ? scope_local(pscope_tv, p0_orig) : NULL;914+
TyKind p0_scope_ty = p0_lv_tv ? p0_lv_tv->type : TY_UNKNOWN;915+
int needs_box_assign = (p0_scope_ty == TY_POLY && (!keys ? svt != TY_POLY : skt != TY_POLY));797916
int ts = ++g_tmp, td = ++g_tmp, ti = ++g_tmp, tk = ++g_tmp;798917
Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb); /* recv preludes flush to g_pre first */799918
emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre); buf_printf(g_pre, " _t%d = ", ts); buf_puts(g_pre, rb.p ? rb.p : ""); buf_puts(g_pre, ";\n"); free(rb.p);@@ -802,49 +921,28 @@
802921
emit_indent(g_pre, g_indent + 1); emit_ctype(c, skt, g_pre); buf_printf(g_pre, " _t%d = _t%d->order[_t%d];\n", tk, ts, ti);803922
if (p0) {804923
emit_indent(g_pre, g_indent + 1);805-
if (need_shadow) {806-
shadow_tmp = ++g_tmp;807-
emit_ctype(c, param_ty, g_pre);808-
if (keys) buf_printf(g_pre, " lv__bp%d = _t%d;\n", shadow_tmp, tk);809-
else buf_printf(g_pre, " lv__bp%d = sp_%sHash_get(_t%d, _t%d);\n", shadow_tmp, shn, ts, tk);810-
for (int ri = 0; ri < g_nren; ri++) {811-
if (strcmp(g_ren_from[ri], p0_orig) == 0) {812-
shadow_ren_idx = ri;813-
strncpy(shadow_ren_old, g_ren_to[ri], sizeof shadow_ren_old - 1);814-
snprintf(g_ren_to[ri], sizeof g_ren_to[0], "_bp%d", shadow_tmp);815-
break;816-
}924+
if (keys) {925+
if (needs_box_assign) {926+
Buf bx; memset(&bx, 0, sizeof bx); char gk[64]; snprintf(gk, sizeof gk, "_t%d", tk);927+
emit_boxed_text(c, skt, gk, &bx);928+
buf_printf(g_pre, "lv_%s = %s;\n", p0, bx.p ? bx.p : ""); free(bx.p);817929
}818-
if (shadow_ren_idx < 0) {819-
shadow_ren_idx = g_nren; shadow_ren_new = 1;820-
snprintf(g_ren_from[g_nren], sizeof g_ren_from[0], "%s", p0_orig);821-
snprintf(g_ren_to[g_nren], sizeof g_ren_to[0], "_bp%d", shadow_tmp);822-
g_nren++;823-
}930+
else buf_printf(g_pre, "lv_%s = _t%d;\n", p0, tk);824931
}825932
else {826-
if (keys) buf_printf(g_pre, "lv_%s = _t%d;\n", p0, tk);933+
if (needs_box_assign) {934+
char gv[128]; snprintf(gv, sizeof gv, "sp_%sHash_get(_t%d, _t%d)", shn, ts, tk);935+
Buf bx; memset(&bx, 0, sizeof bx);936+
emit_boxed_text(c, svt, gv, &bx);937+
buf_printf(g_pre, "lv_%s = %s;\n", p0, bx.p ? bx.p : ""); free(bx.p);938+
}827939
else buf_printf(g_pre, "lv_%s = sp_%sHash_get(_t%d, _t%d);\n", p0, shn, ts, tk);828940
}829941
}830-
/* Patch ntype for block param reads so emit_call sees the correct type */831-
int patched_ids[64]; TyKind patched_old[64]; int npatched = 0;832-
if (need_shadow) {833-
for (int j = 0; j < bn && npatched < 64; j++)834-
npatched += patch_lv_reads(c, bb[j], p0_orig, param_ty,835-
patched_ids + npatched, patched_old + npatched, 64 - npatched);836-
}837942
for (int j = 0; j < bn - 1; j++) emit_stmt(c, bb[j], g_pre, g_indent + 1);838943
int save = g_indent; g_indent++;839944
Buf vb; memset(&vb, 0, sizeof vb); emit_expr(c, bb[bn - 1], &vb); g_indent = save;840-
/* Restore patched ntypes and rename */841-
for (int j = 0; j < npatched; j++) c->ntype[patched_ids[j]] = patched_old[j];842-
if (shadow_ren_idx >= 0) {843-
if (shadow_ren_new) g_nren = shadow_ren_idx;844-
else strncpy(g_ren_to[shadow_ren_idx], shadow_ren_old, sizeof g_ren_to[0] - 1);845-
}846945
TyKind bret = comp_ntype(c, bb[bn - 1]);847-
if (need_shadow && bret == TY_UNKNOWN) bret = param_ty;848946
emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "sp_%sHash_set(_t%d, ", dhn, td);849947
if (keys) {850948
/* new key = block result; value carried over (box if dst value is poly) */@@ -4986,13 +5084,19 @@
49865084
return;49875085
}49885086
}4989-
if (res == TY_INT) {5087+
/* Re-derive result type when cache may be stale due to block-param widening */5088+
TyKind eff_res = res;5089+
if (eff_res != TY_INT && eff_res != TY_FLOAT && eff_res != TY_BIGINT) {5090+
if (rt == TY_FLOAT || a0 == TY_FLOAT) eff_res = TY_FLOAT;5091+
else if (rt == TY_INT && (a0 == TY_INT || a0 == TY_UNKNOWN)) eff_res = TY_INT;5092+
}5093+
if (eff_res == TY_INT) {49905094
buf_printf(b, "%s(", int_arith_fn(name));49915095
emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);49925096
buf_puts(b, ")");49935097
return;49945098
}4995-
if (res == TY_FLOAT && strcmp(name, "%") && strcmp(name, "**")) {5099+
if (eff_res == TY_FLOAT && strcmp(name, "%") && strcmp(name, "**")) {49965100
buf_puts(b, "(");49975101
emit_expr(c, recv, b);49985102
buf_printf(b, " %s ", name);