Commit e6f13226

e6f13226c1e0Merged

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

Description

codegen(promote): dispatch a poly scalar to its reopened primitive class

The previous SP_TAG_OBJ guard sent every boxed scalar to the no-match sentinel, which broke dispatch to a reopened primitive method: a poly int calling a user-defined Integer#to_pair matched no case and returned the null/zero default (a class Integer; def to_pair; end reopen produced blank output).

Replace the inline guard with emit_poly_dispatch_key: an SP_TAG_OBJ value still uses its cls_id, but a boxed scalar maps to its reopened primitive's class index (Integer/Float/String/Symbol/NilClass) when one exists, else to the sentinel. So a reopened-primitive method dispatches, while a plain scalar still cannot alias a regular user class at index 0.

Fixes a regression from "guard poly method dispatch on SP_TAG_OBJ" (class_method_open_class_call under promote).

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

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

Details

Commit
e6f13226c1e0
Parent
e495a314e72f

src/codegen_call.cmodifiedView file

@@ -231,6 +231,25 @@
231231 return 1;
232232 }
233233
234+/* Emit the switch key for a poly method dispatch. An SP_TAG_OBJ value uses its
235+ real cls_id; a boxed scalar maps to its reopened primitive class index (so a
236+ reopened Integer/Float/String/Symbol/nil method still dispatches), else to a
237+ sentinel matching no case -- this keeps a plain scalar (cls_id 0) from
238+ aliasing a regular user class that happens to occupy index 0. */
239+static void emit_poly_dispatch_key(Compiler *c, int tv, Buf *b) {
240+ static const struct { const char *tag, *cls; } P[] = {
241+ {"SP_TAG_INT", "Integer"}, {"SP_TAG_FLT", "Float"},
242+ {"SP_TAG_STR", "String"}, {"SP_TAG_SYM", "Symbol"},
243+ {"SP_TAG_NIL", "NilClass"},
244+ };
245+ buf_printf(b, "(_t%d.tag == SP_TAG_OBJ ? _t%d.cls_id", tv, tv);
246+ for (unsigned i = 0; i < sizeof P / sizeof P[0]; i++) {
247+ int idx = comp_class_index(c, P[i].cls);
248+ if (idx >= 0) buf_printf(b, " : _t%d.tag == %s ? %d", tv, P[i].tag, idx);
249+ }
250+ buf_puts(b, " : 0x7fffffff)");
251+}
252+
234253 static int emit_array_call(Compiler *c, int id, Buf *b) {
235254 const NodeTable *nt = c->nt;
236255 const char *name = nt_str(nt, id, "name");
@@ -3951,7 +3970,6 @@
39513970 int oi = bop[0] == 'g' ? 0 : bop[0] == 's' ? 1 : 2;
39523971 if (!bam_done[ki][oi]) {
39533972 bam_done[ki][oi] = 1;
3954- const char *et = (ki == 0) ? "mrb_int" : "const char *";
39553973 const char *cast = (ki == 0) ? "" : "(mrb_int)(uintptr_t)";
39563974 const char *uncast = (ki == 0) ? "" : "(const char *)(uintptr_t)";
39573975 if (oi == 0) {
@@ -3969,7 +3987,6 @@
39693987 buf_printf(&g_procs, "static mrb_int _bam_%sArray_push(void *a, mrb_int v) {\n"
39703988 " sp_%sArray_push((sp_%sArray *)a, %sv);\n return (mrb_int)(uintptr_t)a;\n}\n", bk, bk, bk, uncast);
39713989 }
3972- (void)et;
39733990 }
39743991 buf_printf(b, "(mrb_int)(uintptr_t)&_bam_%sArray_%s", bk, bop);
39753992 }
@@ -8293,12 +8310,12 @@
82938310 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);
82948311 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);
82958312 }
8296- /* Only an SP_TAG_OBJ value carries a real cls_id; a boxed scalar's cls_id
8297- is 0, which would otherwise alias the first user class (class id 0) and
8298- mis-dispatch (e.g. a poly int's `to_s` calling that class's to_s --
8299- infinite recursion). Map non-object tags to a value matching no case so
8300- they fall to the default (sp_poly_to_s / 0). */
8301- buf_printf(b, "switch (_t%d.tag == SP_TAG_OBJ ? _t%d.cls_id : 0x7fffffff) {", tv, tv);
8313+ /* A boxed scalar's cls_id is 0, which would otherwise alias the first user
8314+ class (class id 0) and mis-dispatch (e.g. a poly int's `to_s` recursing
8315+ into that class's to_s). Key the switch on the tag-aware dispatch id. */
8316+ buf_puts(b, "switch (");
8317+ emit_poly_dispatch_key(c, tv, b);
8318+ buf_puts(b, ") {");
83028319 for (int k = 0; k < c->nclasses; k++) {
83038320 int defcls = -1;
83048321 int mi = comp_method_in_chain(c, k, name, &defcls);
@@ -8451,12 +8468,12 @@
84518468 else
84528469 buf_printf(b, "if (_t%d.tag == SP_TAG_INT) { _t%d = (_t%d.v.i >> %s) & 1; } else ", tv, tr, tv, idxref);
84538470 }
8454- /* Only an SP_TAG_OBJ value carries a real cls_id; a boxed scalar's cls_id
8455- is 0, which would otherwise alias the first user class (class id 0) and
8456- mis-dispatch (e.g. a poly int's `to_s` calling that class's to_s --
8457- infinite recursion). Map non-object tags to a value matching no case so
8458- they fall to the default (sp_poly_to_s / 0). */
8459- buf_printf(b, "switch (_t%d.tag == SP_TAG_OBJ ? _t%d.cls_id : 0x7fffffff) {", tv, tv);
8471+ /* A boxed scalar's cls_id is 0, which would otherwise alias the first user
8472+ class (class id 0) and mis-dispatch (e.g. a poly int's `to_s` recursing
8473+ into that class's to_s). Key the switch on the tag-aware dispatch id. */
8474+ buf_puts(b, "switch (");
8475+ emit_poly_dispatch_key(c, tv, b);
8476+ buf_puts(b, ") {");
84608477 for (int k = 0; k < c->nclasses; k++) {
84618478 int defcls = -1;
84628479 int mi = comp_method_in_chain(c, k, name, &defcls);