Commit 5dbb0a3f

5dbb0a3fcdeaMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Fix transform_keys/values block param type when unified with other blocks

When multiple blocks in the same method scope share a block param name (e.g. two |v| blocks, one over SymIntHash and one over IntStrHash), the LocalVar type gets unified to TY_POLY. This caused emit_call to see TY_POLY as the receiver type for methods like upcase, hitting the unsupported-call exit.

Fix: emit_transform_hash_expr detects when the block param's declared type differs from the expected hash key/value type. In that case:

  • A correctly-typed shadow variable lv__bp{N} is declared inside the loop body and assigned the key or value directly.
  • A rename redirects reads of the original param name to the shadow.
  • c->ntype is patched for LocalVariableReadNodes within the block body so that comp_ntype returns the correct specific type during emit_call.

Also handle transform_values with empty block body ({ }) by setting all values to the nil sentinel (NULL for strings, SP_INT_NIL for ints).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
5dbb0a3fcdea
Parent
bdb61ab35b01

src/codegen.cmodifiedView file

@@ -654,6 +654,35 @@
654654 return 1;
655655 }
656656
657+/* Recursively patch c->ntype[id]=ty for every LocalVariableReadNode named nm
658+ within the subtree at id. Saved old values in ids_out/ty_out (max cap).
659+ Returns count of patched nodes. */
660+static int patch_lv_reads(Compiler *c, int id, const char *nm, TyKind ty,
661+ int *ids_out, TyKind *ty_out, int cap) {
662+ if (id < 0 || id >= c->nt->count || cap <= 0) return 0;
663+ int n = 0;
664+ const char *node_ty = nt_type(c->nt, id);
665+ if (node_ty && !strcmp(node_ty, "LocalVariableReadNode")) {
666+ const char *vname = nt_str(c->nt, id, "name");
667+ if (vname && !strcmp(vname, nm)) {
668+ ids_out[0] = id; ty_out[0] = c->ntype[id]; c->ntype[id] = ty;
669+ return 1;
670+ }
671+ }
672+ int nr = nt_num_refs(c->nt, id);
673+ for (int i = 0; i < nr && n < cap; i++) {
674+ int r = nt_ref_at(c->nt, id, i);
675+ n += patch_lv_reads(c, r, nm, ty, ids_out + n, ty_out + n, cap - n);
676+ }
677+ int na = nt_num_arrs(c->nt, id);
678+ for (int i = 0; i < na && n < cap; i++) {
679+ int cn = 0; const int *ids = nt_arr_at(c->nt, id, i, &cn);
680+ for (int j = 0; j < cn && n < cap; j++)
681+ n += patch_lv_reads(c, ids[j], nm, ty, ids_out + n, ty_out + n, cap - n);
682+ }
683+ return n;
684+}
685+
657686 /* hash.transform_keys { |k| nk } / transform_values { |v| nv }: rebuild the
658687 hash applying the block to every key (or value), keeping the other half.
659688 Returns 1 if handled. */
@@ -672,12 +701,43 @@
672701 TyKind dt = comp_ntype(c, id);
673702 const char *dhn = ty_hash_cname(dt);
674703 if (!dhn) return 0;
675- const char *p0 = block_param_name(c, block, 0); if (p0) p0 = rename_local(p0);
704+ const char *p0_orig = block_param_name(c, block, 0);
705+ const char *p0 = p0_orig ? rename_local(p0_orig) : NULL;
676706 int body = nt_ref(nt, block, "body");
677707 int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
678- if (bn < 1) return 0;
708+ /* Empty block: transform_values { } → all values become nil (keep key set) */
709+ if (bn < 1) {
710+ if (keys) return 0;
711+ int ts2 = ++g_tmp, td2 = ++g_tmp, ti2 = ++g_tmp, tk2 = ++g_tmp;
712+ Buf rb2; memset(&rb2, 0, sizeof rb2); emit_expr(c, recv, &rb2);
713+ emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre);
714+ buf_printf(g_pre, " _t%d = ", ts2); buf_puts(g_pre, rb2.p ? rb2.p : ""); buf_puts(g_pre, ";\n"); free(rb2.p);
715+ emit_indent(g_pre, g_indent); emit_ctype(c, rt, g_pre);
716+ buf_printf(g_pre, " _t%d = sp_%sHash_new(); SP_GC_ROOT(_t%d);\n", td2, shn, td2);
717+ emit_indent(g_pre, g_indent);
718+ buf_printf(g_pre, "for (mrb_int _t%d = 0; _t%d < _t%d->len; _t%d++) {\n", ti2, ti2, ts2, ti2);
719+ emit_indent(g_pre, g_indent + 1); emit_ctype(c, ty_hash_key(rt), g_pre);
720+ buf_printf(g_pre, " _t%d = _t%d->order[_t%d];\n", tk2, ts2, ti2);
721+ emit_indent(g_pre, g_indent + 1);
722+ { TyKind vt2 = ty_hash_val(rt);
723+ const char *nil_v = (vt2 == TY_INT) ? "SP_INT_NIL" :
724+ (vt2 == TY_POLY) ? "sp_box_nil()" : "NULL";
725+ buf_printf(g_pre, "sp_%sHash_set(_t%d, _t%d, %s);\n", shn, td2, tk2, nil_v); }
726+ emit_indent(g_pre, g_indent); buf_puts(g_pre, "}\n");
727+ buf_printf(b, "_t%d", td2);
728+ return 1;
729+ }
679730 TyKind skt = ty_hash_key(rt), svt = ty_hash_val(rt);
680731 TyKind dvt = ty_hash_val(dt);
732+ /* Detect type mismatch: block param unified with other blocks in same scope */
733+ TyKind param_ty = keys ? skt : svt;
734+ Scope *pscope = comp_scope_of(c, block);
735+ LocalVar *param_lv = p0_orig ? scope_local(pscope, p0_orig) : NULL;
736+ TyKind declared_ty = param_lv ? param_lv->type : TY_UNKNOWN;
737+ int need_shadow = p0_orig && param_ty != TY_UNKNOWN &&
738+ declared_ty != TY_UNKNOWN && declared_ty != param_ty;
739+ int shadow_tmp = -1, shadow_ren_idx = -1, shadow_ren_new = 0;
740+ char shadow_ren_old[112]; shadow_ren_old[0] = '\0';
681741 int ts = ++g_tmp, td = ++g_tmp, ti = ++g_tmp, tk = ++g_tmp;
682742 Buf rb; memset(&rb, 0, sizeof rb); emit_expr(c, recv, &rb); /* recv preludes flush to g_pre first */
683743 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);
@@ -686,13 +746,49 @@
686746 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);
687747 if (p0) {
688748 emit_indent(g_pre, g_indent + 1);
689- if (keys) buf_printf(g_pre, "lv_%s = _t%d;\n", p0, tk);
690- else buf_printf(g_pre, "lv_%s = sp_%sHash_get(_t%d, _t%d);\n", p0, shn, ts, tk);
749+ if (need_shadow) {
750+ shadow_tmp = ++g_tmp;
751+ emit_ctype(c, param_ty, g_pre);
752+ if (keys) buf_printf(g_pre, " lv__bp%d = _t%d;\n", shadow_tmp, tk);
753+ else buf_printf(g_pre, " lv__bp%d = sp_%sHash_get(_t%d, _t%d);\n", shadow_tmp, shn, ts, tk);
754+ for (int ri = 0; ri < g_nren; ri++) {
755+ if (strcmp(g_ren_from[ri], p0_orig) == 0) {
756+ shadow_ren_idx = ri;
757+ strncpy(shadow_ren_old, g_ren_to[ri], sizeof shadow_ren_old - 1);
758+ snprintf(g_ren_to[ri], sizeof g_ren_to[0], "_bp%d", shadow_tmp);
759+ break;
760+ }
761+ }
762+ if (shadow_ren_idx < 0) {
763+ shadow_ren_idx = g_nren; shadow_ren_new = 1;
764+ snprintf(g_ren_from[g_nren], sizeof g_ren_from[0], "%s", p0_orig);
765+ snprintf(g_ren_to[g_nren], sizeof g_ren_to[0], "_bp%d", shadow_tmp);
766+ g_nren++;
767+ }
768+ }
769+ else {
770+ if (keys) buf_printf(g_pre, "lv_%s = _t%d;\n", p0, tk);
771+ else buf_printf(g_pre, "lv_%s = sp_%sHash_get(_t%d, _t%d);\n", p0, shn, ts, tk);
772+ }
773+ }
774+ /* Patch ntype for block param reads so emit_call sees the correct type */
775+ int patched_ids[64]; TyKind patched_old[64]; int npatched = 0;
776+ if (need_shadow) {
777+ for (int j = 0; j < bn && npatched < 64; j++)
778+ npatched += patch_lv_reads(c, bb[j], p0_orig, param_ty,
779+ patched_ids + npatched, patched_old + npatched, 64 - npatched);
691780 }
692781 for (int j = 0; j < bn - 1; j++) emit_stmt(c, bb[j], g_pre, g_indent + 1);
693782 int save = g_indent; g_indent++;
694783 Buf vb; memset(&vb, 0, sizeof vb); emit_expr(c, bb[bn - 1], &vb); g_indent = save;
784+ /* Restore patched ntypes and rename */
785+ for (int j = 0; j < npatched; j++) c->ntype[patched_ids[j]] = patched_old[j];
786+ if (shadow_ren_idx >= 0) {
787+ if (shadow_ren_new) g_nren = shadow_ren_idx;
788+ else strncpy(g_ren_to[shadow_ren_idx], shadow_ren_old, sizeof g_ren_to[0] - 1);
789+ }
695790 TyKind bret = comp_ntype(c, bb[bn - 1]);
791+ if (need_shadow && bret == TY_UNKNOWN) bret = param_ty;
696792 emit_indent(g_pre, g_indent + 1); buf_printf(g_pre, "sp_%sHash_set(_t%d, ", dhn, td);
697793 if (keys) {
698794 /* new key = block result; value carried over (box if dst value is poly) */