Commit dec6767c

dec6767c6185Merged

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

Description

codegen(promote): complete poly method dispatch for to_s/inspect and a poly index

Two gaps in the poly-receiver method dispatch surfaced once promote widened concrete ints to poly:

  • to_s / inspect: the no-arg dispatch switch enumerated only user-class and a few builtin-collection arms. A poly value holding a builtin scalar (an int ivar widened to poly, @x.to_s) matched no case and fell through to the empty-string default, printing blank. Add a default: arm routing through sp_poly_to_s / sp_poly_inspect, which every value answers.

  • Integer#[] bit-ref / array []: the builtin-index arm required the index arg to be TY_INT, but a promote-widened index variable is poly, so n[i] (dynamic index) took no arm and returned the 0/blank default while n[0] (literal index) worked. Accept a poly index and unbox it with sp_poly_to_i where it is consumed.

Fixes promote-mode empty/blank output: bundle_classd_14, bundle_classd_19, bundle_classd_33, bundle_misc_c_31.

Default test 992/0/0 and optcarrot checksum 59662 unchanged.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
dec6767c6185
Parent
f2820e72b5b5

src/codegen_call.cmodifiedView file

@@ -8352,6 +8352,18 @@
83528352 buf_printf(b, " case SP_BUILTIN_SYM_POLY_HASH: _t%d = %s((sp_SymPolyHash *)_t%d.v.p)->len == 0%s; break;", tr, ebopen, tv, ebclose);
83538353 buf_printf(b, " case SP_BUILTIN_STR_POLY_HASH: _t%d = %s((sp_StrPolyHash *)_t%d.v.p)->len == 0%s; break;", tr, ebopen, tv, ebclose);
83548354 }
8355+ /* to_s / inspect are universal: a poly value that is a builtin scalar
8356+ (int, float, string, ...) rather than one of the enumerated user
8357+ classes still answers them. Without a default arm the result stayed
8358+ the empty-string default, so `@x.to_s` on a poly-widened int printed
8359+ blank. Route the fallthrough through the runtime poly converter. */
8360+ if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) {
8361+ const char *pfn = !strcmp(name, "to_s") ? "sp_poly_to_s" : "sp_poly_inspect";
8362+ buf_printf(b, " default: _t%d = ", tr);
8363+ if (ret == TY_POLY) buf_printf(b, "sp_box_str(%s(_t%d))", pfn, tv);
8364+ else buf_printf(b, "%s(_t%d)", pfn, tv);
8365+ buf_puts(b, "; break;");
8366+ }
83558367 buf_printf(b, " } _t%d; })", tr);
83568368 return;
83578369 }
@@ -8361,8 +8373,11 @@
83618373 and call the matching user method (or a builtin array `[]`), passing the
83628374 arguments evaluated once into temps. */
83638375 if (recv >= 0 && rt == TY_POLY && argc > 0) {
8364- /* the builtin-array `[]` arm only applies to an integer index */
8365- int is_index = !strcmp(name, "[]") && argc == 1 && comp_ntype(c, argv[0]) == TY_INT;
8376+ /* the builtin-array `[]` / Integer#[] bit-ref arm applies to an integer
8377+ index; in promote mode that index variable may have widened to poly, so
8378+ accept poly too (the index is unboxed where it is used below). */
8379+ int is_index = !strcmp(name, "[]") && argc == 1 &&
8380+ (comp_ntype(c, argv[0]) == TY_INT || comp_ntype(c, argv[0]) == TY_POLY);
83668381 int is_include = (!strcmp(name, "include?") || !strcmp(name, "member?") ||
83678382 !strcmp(name, "has_key?") || !strcmp(name, "key?")) && argc == 1;
83688383 int ncand = 0;
@@ -8398,12 +8413,17 @@
83988413 /* include? on a TAG_STR receiver: check tag before entering cls_id switch */
83998414 if (is_include && infer_type(c, argv[0]) == TY_STRING)
84008415 buf_printf(b, "if (_t%d.tag == SP_TAG_STR) { _t%d = sp_str_include(_t%d.v.s, _t%d); } else ", tv, tr, tv, atmp[0]);
8416+ /* The builtin index/bit-ref arms use the index as a raw mrb_int; unbox it
8417+ when the index temp widened to poly (promote mode). */
8418+ char idxref[64];
8419+ if (is_index && atmp_ty[0] == TY_POLY) snprintf(idxref, sizeof idxref, "sp_poly_to_i(_t%d)", atmp[0]);
8420+ else snprintf(idxref, sizeof idxref, "_t%d", atmp[0]);
84018421 /* Integer#[N] bit-extraction: poly recv may hold a tagged int */
84028422 if (is_index) {
84038423 if (ret == TY_POLY)
8404- buf_printf(b, "if (_t%d.tag == SP_TAG_INT) { _t%d = sp_box_int((_t%d.v.i >> _t%d) & 1); } else ", tv, tr, tv, atmp[0]);
8424+ buf_printf(b, "if (_t%d.tag == SP_TAG_INT) { _t%d = sp_box_int((_t%d.v.i >> %s) & 1); } else ", tv, tr, tv, idxref);
84058425 else
8406- buf_printf(b, "if (_t%d.tag == SP_TAG_INT) { _t%d = (_t%d.v.i >> _t%d) & 1; } else ", tv, tr, tv, atmp[0]);
8426+ buf_printf(b, "if (_t%d.tag == SP_TAG_INT) { _t%d = (_t%d.v.i >> %s) & 1; } else ", tv, tr, tv, idxref);
84078427 }
84088428 buf_printf(b, "switch (_t%d.cls_id) {", tv);
84098429 for (int k = 0; k < c->nclasses; k++) {
@@ -8452,13 +8472,13 @@
84528472 }
84538473 if (is_index) {
84548474 if (ret == TY_POLY) {
8455- buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_box_int(sp_IntArray_get((sp_IntArray *)_t%d.v.p, _t%d)); break;", tr, tv, atmp[0]);
8456- buf_printf(b, " case SP_BUILTIN_STR_ARRAY: _t%d = sp_box_str(sp_StrArray_get((sp_StrArray *)_t%d.v.p, _t%d)); break;", tr, tv, atmp[0]);
8457- buf_printf(b, " case SP_BUILTIN_FLT_ARRAY: _t%d = sp_box_float(sp_FloatArray_get((sp_FloatArray *)_t%d.v.p, _t%d)); break;", tr, tv, atmp[0]);
8458- buf_printf(b, " case SP_BUILTIN_POLY_ARRAY: _t%d = sp_PolyArray_get((sp_PolyArray *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
8475+ buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_box_int(sp_IntArray_get((sp_IntArray *)_t%d.v.p, %s)); break;", tr, tv, idxref);
8476+ buf_printf(b, " case SP_BUILTIN_STR_ARRAY: _t%d = sp_box_str(sp_StrArray_get((sp_StrArray *)_t%d.v.p, %s)); break;", tr, tv, idxref);
8477+ buf_printf(b, " case SP_BUILTIN_FLT_ARRAY: _t%d = sp_box_float(sp_FloatArray_get((sp_FloatArray *)_t%d.v.p, %s)); break;", tr, tv, idxref);
8478+ buf_printf(b, " case SP_BUILTIN_POLY_ARRAY: _t%d = sp_PolyArray_get((sp_PolyArray *)_t%d.v.p, %s); break;", tr, tv, idxref);
84598479 }
84608480 else {
8461- buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_IntArray_get((sp_IntArray *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
8481+ buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_IntArray_get((sp_IntArray *)_t%d.v.p, %s); break;", tr, tv, idxref);
84628482 }
84638483 }
84648484 if (is_include) {