Commit b6a36c3c

b6a36c3c7876Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

codegen: keep builtin empty? arms in poly dispatch with a user-defined empty?

Sibling of #1437. Once any class defines an instance empty?, a poly empty? send leaves the generic sp_poly_length(x) == 0 lowering and builds a per-class cls_id switch -- populated only from user empty? methods. There was no String tag arm (nor Array/Hash cls_id arms), so a String receiver matched nothing, the result temp stayed 0, and empty? returned false. One def empty? anywhere poisoned every poly String#empty?.

Add the builtin empty? arms to the argc==0 poly dispatch, mirroring the length/size/count handling: string/symbol-tagged receivers answer length == 0 before the switch, and the array / poly-hash cls_id cases answer len == 0, boxing to a bool when the result feeds a poly slot.

Fixes #1438

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
b6a36c3c7876
Parent
2ddce70a3d19

src/codegen_call.cmodifiedView file

@@ -5220,6 +5220,7 @@
52205220 unboxing the pointer. */
52215221 if (recv >= 0 && rt == TY_POLY && argc == 0) {
52225222 int is_lengthlike = !strcmp(name, "length") || !strcmp(name, "size") || !strcmp(name, "count");
5223+ int is_empty = !strcmp(name, "empty?");
52235224 int ncand = 0;
52245225 for (int k = 0; k < c->nclasses; k++)
52255226 if (comp_method_in_chain(c, k, name, NULL) >= 0 || comp_reader_in_chain(c, k, name, NULL)) ncand++;
@@ -5233,11 +5234,19 @@
52335234 length-like int branches must box their integer result. */
52345235 const char *bopen = (ret == TY_POLY) ? "sp_box_int(" : "";
52355236 const char *bclose = (ret == TY_POLY) ? ")" : "";
5237+ /* empty? answers a bool; box it (not an int) when the result feeds poly */
5238+ const char *ebopen = (ret == TY_POLY) ? "sp_box_bool(" : "";
5239+ const char *ebclose = (ret == TY_POLY) ? ")" : "";
52365240 /* string/symbol-tagged poly values answer length/size directly */
52375241 if (is_lengthlike) {
52385242 buf_printf(b, "if (_t%d.tag == SP_TAG_SYM) _t%d = %s(mrb_int)strlen(sp_sym_to_s((sp_sym)_t%d.v.i))%s; else ", tv, tr, bopen, tv, bclose);
52395243 buf_printf(b, "if (_t%d.tag == SP_TAG_STR) _t%d = %s(mrb_int)sp_str_length(_t%d.v.s)%s; else ", tv, tr, bopen, tv, bclose);
52405244 }
5245+ /* a string/symbol-tagged poly value answers empty? directly (#1438) */
5246+ if (is_empty) {
5247+ buf_printf(b, "if (_t%d.tag == SP_TAG_STR) _t%d = %ssp_str_length(_t%d.v.s) == 0%s; else ", tv, tr, ebopen, tv, ebclose);
5248+ buf_printf(b, "if (_t%d.tag == SP_TAG_SYM) _t%d = %sstrlen(sp_sym_to_s((sp_sym)_t%d.v.i)) == 0%s; else ", tv, tr, ebopen, tv, ebclose);
5249+ }
52415250 buf_printf(b, "switch (_t%d.cls_id) {", tv);
52425251 for (int k = 0; k < c->nclasses; k++) {
52435252 int defcls = -1;
@@ -5295,6 +5304,16 @@
52955304 buf_printf(b, " case SP_BUILTIN_SYM_POLY_HASH: _t%d = %s((sp_SymPolyHash *)_t%d.v.p)->len%s; break;", tr, bopen, tv, bclose);
52965305 buf_printf(b, " case SP_BUILTIN_STR_POLY_HASH: _t%d = %s((sp_StrPolyHash *)_t%d.v.p)->len%s; break;", tr, bopen, tv, bclose);
52975306 }
5307+ /* built-in array / hash receivers reaching a poly empty? dispatch (#1438) */
5308+ if (is_empty) {
5309+ buf_printf(b, " case SP_BUILTIN_INT_ARRAY: case SP_BUILTIN_SYM_ARRAY: _t%d = %ssp_IntArray_length((sp_IntArray *)_t%d.v.p) == 0%s; break;", tr, ebopen, tv, ebclose);
5310+ buf_printf(b, " case SP_BUILTIN_STR_ARRAY: _t%d = %ssp_StrArray_length((sp_StrArray *)_t%d.v.p) == 0%s; break;", tr, ebopen, tv, ebclose);
5311+ buf_printf(b, " case SP_BUILTIN_FLT_ARRAY: _t%d = %ssp_FloatArray_length((sp_FloatArray *)_t%d.v.p) == 0%s; break;", tr, ebopen, tv, ebclose);
5312+ buf_printf(b, " case SP_BUILTIN_POLY_ARRAY: _t%d = %ssp_PolyArray_length((sp_PolyArray *)_t%d.v.p) == 0%s; break;", tr, ebopen, tv, ebclose);
5313+ buf_printf(b, " case SP_BUILTIN_POLY_POLY_HASH: _t%d = %s((sp_PolyPolyHash *)_t%d.v.p)->len == 0%s; break;", tr, ebopen, tv, ebclose);
5314+ 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);
5315+ 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);
5316+ }
52985317 buf_printf(b, " } _t%d; })", tr);
52995318 return;
53005319 }