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
unboxing the pointer. */ if (recv >= 0 && rt == TY_POLY && argc == 0) { int is_lengthlike = !strcmp(name, "length") || !strcmp(name, "size") || !strcmp(name, "count"); int is_empty = !strcmp(name, "empty?"); int ncand = 0; for (int k = 0; k < c->nclasses; k++) if (comp_method_in_chain(c, k, name, NULL) >= 0 || comp_reader_in_chain(c, k, name, NULL)) ncand++; length-like int branches must box their integer result. */ const char *bopen = (ret == TY_POLY) ? "sp_box_int(" : ""; const char *bclose = (ret == TY_POLY) ? ")" : ""; /* empty? answers a bool; box it (not an int) when the result feeds poly */ const char *ebopen = (ret == TY_POLY) ? "sp_box_bool(" : ""; const char *ebclose = (ret == TY_POLY) ? ")" : ""; /* string/symbol-tagged poly values answer length/size directly */ if (is_lengthlike) { 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); 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); } /* a string/symbol-tagged poly value answers empty? directly (#1438) */ if (is_empty) { 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); 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); } buf_printf(b, "switch (_t%d.cls_id) {", tv); for (int k = 0; k < c->nclasses; k++) { int defcls = -1; 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); 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); } /* built-in array / hash receivers reaching a poly empty? dispatch (#1438) */ if (is_empty) { 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); 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); 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); 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); 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); 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); } buf_printf(b, " } _t%d; })", tr); return; }test/poly_empty_with_user_method.rbaddedView file
Loading diff…
test/poly_empty_with_user_method.rb.expectedaddedView file
Loading diff…