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 adefault: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, son[i](dynamic index) took no arm and returned the 0/blank default whilen[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
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); 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); } /* to_s / inspect are universal: a poly value that is a builtin scalar (int, float, string, ...) rather than one of the enumerated user classes still answers them. Without a default arm the result stayed the empty-string default, so `@x.to_s` on a poly-widened int printed blank. Route the fallthrough through the runtime poly converter. */ if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) { const char *pfn = !strcmp(name, "to_s") ? "sp_poly_to_s" : "sp_poly_inspect"; buf_printf(b, " default: _t%d = ", tr); if (ret == TY_POLY) buf_printf(b, "sp_box_str(%s(_t%d))", pfn, tv); else buf_printf(b, "%s(_t%d)", pfn, tv); buf_puts(b, "; break;"); } buf_printf(b, " } _t%d; })", tr); return; } and call the matching user method (or a builtin array `[]`), passing the arguments evaluated once into temps. */ if (recv >= 0 && rt == TY_POLY && argc > 0) { /* the builtin-array `[]` arm only applies to an integer index */ int is_index = !strcmp(name, "[]") && argc == 1 && comp_ntype(c, argv[0]) == TY_INT; /* the builtin-array `[]` / Integer#[] bit-ref arm applies to an integer index; in promote mode that index variable may have widened to poly, so accept poly too (the index is unboxed where it is used below). */ int is_index = !strcmp(name, "[]") && argc == 1 && (comp_ntype(c, argv[0]) == TY_INT || comp_ntype(c, argv[0]) == TY_POLY); int is_include = (!strcmp(name, "include?") || !strcmp(name, "member?") || !strcmp(name, "has_key?") || !strcmp(name, "key?")) && argc == 1; int ncand = 0; /* include? on a TAG_STR receiver: check tag before entering cls_id switch */ if (is_include && infer_type(c, argv[0]) == TY_STRING) 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]); /* The builtin index/bit-ref arms use the index as a raw mrb_int; unbox it when the index temp widened to poly (promote mode). */ char idxref[64]; if (is_index && atmp_ty[0] == TY_POLY) snprintf(idxref, sizeof idxref, "sp_poly_to_i(_t%d)", atmp[0]); else snprintf(idxref, sizeof idxref, "_t%d", atmp[0]); /* Integer#[N] bit-extraction: poly recv may hold a tagged int */ if (is_index) { if (ret == TY_POLY) 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]); 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); else 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]); buf_printf(b, "if (_t%d.tag == SP_TAG_INT) { _t%d = (_t%d.v.i >> %s) & 1; } else ", tv, tr, tv, idxref); } buf_printf(b, "switch (_t%d.cls_id) {", tv); for (int k = 0; k < c->nclasses; k++) { } if (is_index) { if (ret == TY_POLY) { 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]); 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]); 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]); 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]); 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); 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); 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); buf_printf(b, " case SP_BUILTIN_POLY_ARRAY: _t%d = sp_PolyArray_get((sp_PolyArray *)_t%d.v.p, %s); break;", tr, tv, idxref); } else { 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]); buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_IntArray_get((sp_IntArray *)_t%d.v.p, %s); break;", tr, tv, idxref); } } if (is_include) {