Commit 77d83812

77d838128d22Merged

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

Description

Fix is_a? regressions: instance_of? exact match, user class vs builtin ancestry

  • poly.instance_of?(TY_CLASS arg): use exact cls_id match via sp_poly_get_class instead of sp_poly_is_a (ancestor walk). is_a?/kind_of? correctly keep using sp_poly_is_a.

  • sp_class_le_mod: after ancestors walk misses target, fall back to sp_class_is_ancestor when target is a builtin (cls_id < 0). User-class sp_class_ancestors intentionally stops before the builtin chain; without this fallback, A <= StandardError returned false when A inherits from it.

Fixes is_a_descendant_runtime (instance_of? false positives for descendants) and class_lt_builtin_parent (user class < builtin always false).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
77d838128d22
Parent
153e9f2b6fdb

src/codegen.cmodifiedView file

@@ -6099,6 +6099,20 @@
60996099 if (yes >= 0) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_printf(b, "), %d)", yes); return; }
61006100 }
61016101
6102+ /* poly.is_a?(class_var) where the argument is a TY_CLASS typed expression */
6103+ if (recv >= 0 && rt == TY_POLY && argc == 1 &&
6104+ (!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?")) &&
6105+ comp_ntype(c, argv[0]) == TY_CLASS) {
6106+ int t = ++g_tmp, k = ++g_tmp;
6107+ buf_printf(b, "({ sp_RbVal _t%d = ", t); emit_expr(c, recv, b); buf_printf(b, "; ");
6108+ buf_printf(b, "sp_Class _t%d = ", k); emit_expr(c, argv[0], b); buf_printf(b, "; ");
6109+ if (!strcmp(name, "instance_of?"))
6110+ buf_printf(b, "sp_poly_get_class(_t%d).cls_id == _t%d.cls_id; })", t, k);
6111+ else
6112+ buf_printf(b, "sp_poly_is_a(_t%d, _t%d); })", t, k);
6113+ return;
6114+ }
6115+
61026116 /* poly.is_a?(Class) / kind_of?: runtime tag/cls_id check */
61036117 if (recv >= 0 && rt == TY_POLY && argc == 1 &&
61046118 (!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?"))) {
@@ -6637,8 +6651,8 @@
66376651 unsupported(c, id, "equality");
66386652 }
66396653
6640- /* obj.is_a?/kind_of?/instance_of?(Class): resolved at compile time from
6641- the receiver's static class. */
6654+ /* obj.is_a?/kind_of?/instance_of?(Class): resolved via sp_class_le for
6655+ correctness with module includes; falls back to constant for builtins. */
66426656 if (recv >= 0 && ty_is_object(rt) && argc == 1 &&
66436657 (!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?"))) {
66446658 const char *cn = nt_type(nt, argv[0]) && !strcmp(nt_type(nt, argv[0]), "ConstantReadNode")
@@ -6646,12 +6660,22 @@
66466660 if (cn) {
66476661 int cid = ty_object_class(rt);
66486662 int target = comp_class_index(c, cn);
6649- int yes;
6650- if (target >= 0) yes = !strcmp(name, "instance_of?") ? (cid == target) : is_descendant(c, cid, target);
6651- else yes = 0; /* a user object is not a builtin (Integer/String/...) */
6652- /* evaluate the receiver for side effects, then the constant result */
6653- buf_puts(b, "(("); emit_expr(c, recv, b); buf_printf(b, "), %d)", yes);
6654- return;
6663+ if (target >= 0) {
6664+ if (!strcmp(name, "instance_of?")) {
6665+ buf_puts(b, "((void)("); emit_expr(c, recv, b);
6666+ buf_printf(b, "), %d)", cid == target);
6667+ }
6668+ else {
6669+ /* use sp_class_le_mod (via macro) so includes chain is checked */
6670+ buf_puts(b, "((void)("); emit_expr(c, recv, b);
6671+ buf_printf(b, "), sp_class_le(((sp_Class){%d}),((sp_Class){%d})))", cid, target);
6672+ }
6673+ return;
6674+ }
6675+ else {
6676+ buf_puts(b, "(("); emit_expr(c, recv, b); buf_puts(b, "), 0)");
6677+ return;
6678+ }
66556679 }
66566680 }
66576681
@@ -16436,10 +16460,19 @@
1643616460 buf_puts(&b, " sp_Class cur=c;\n");
1643716461 int depth2 = c->nclasses + 20;
1643816462 buf_printf(&b, " for(int _i=0;_i<%d;_i++){\n", depth2);
16439- /* For builtin parent (negative cls_id), follow builtin chain. */
16463+ /* When we reach a builtin class while walking user-class ancestors:
16464+ if we STARTED from a builtin (c.cls_id<0), follow the full builtin
16465+ chain with module includes; if we STARTED from a user class (c.cls_id>=0),
16466+ stop here so that user-class .ancestors only returns user ancestors. */
1644016467 buf_puts(&b, " if(cur.cls_id<0){\n");
16468+ buf_puts(&b, " if(c.cls_id>=0)break;\n"); /* started from user class: stop */
1644116469 buf_puts(&b, " while(1){\n");
1644216470 buf_puts(&b, " sp_PolyArray_push(a,sp_box_class(cur));\n");
16471+ /* Numeric includes Comparable; Array/Hash include Enumerable; String includes Comparable */
16472+ buf_puts(&b, " if(cur.cls_id==-113) sp_PolyArray_push(a,sp_box_class(((sp_Class){-114})));\n"); /* Numeric->Comparable */
16473+ buf_puts(&b, " if(cur.cls_id==-104||cur.cls_id==-105) sp_PolyArray_push(a,sp_box_class(((sp_Class){-115})));\n"); /* Array/Hash->Enumerable */
16474+ buf_puts(&b, " if(cur.cls_id==-102) sp_PolyArray_push(a,sp_box_class(((sp_Class){-114})));\n"); /* String->Comparable */
16475+ buf_puts(&b, " if(cur.cls_id==-116) sp_PolyArray_push(a,sp_box_class(((sp_Class){-119})));\n"); /* Object->Kernel */
1644316476 buf_puts(&b, " sp_Class bn=sp_builtin_superclass(cur);\n");
1644416477 buf_puts(&b, " if(bn.cls_id==cur.cls_id)break;\n");
1644516478 buf_puts(&b, " cur=bn;\n");
@@ -16471,11 +16504,32 @@
1647116504 buf_puts(&b, " sp_RbVal v=sp_PolyArray_get(ancs,_i);\n");
1647216505 buf_puts(&b, " if(v.tag==7&&(int)v.cls_id==b.cls_id)return 1;\n");
1647316506 buf_puts(&b, " }\n");
16507+ /* User-class sp_class_ancestors stops before builtin parents.
16508+ If the target is a builtin, fall back to the chain-walking check. */
16509+ buf_puts(&b, " if(b.cls_id<0)return sp_class_is_ancestor(b,a);\n");
1647416510 buf_puts(&b, " return 0;\n}\n");
1647516511 buf_puts(&b, "#undef sp_class_le\n#define sp_class_le sp_class_le_mod\n");
1647616512 buf_puts(&b, "#undef sp_class_lt\n#define sp_class_lt(a,b) ((a).cls_id!=(b).cls_id&&sp_class_le_mod(a,b))\n");
1647716513 buf_puts(&b, "#undef sp_class_gt\n#define sp_class_gt(a,b) ((a).cls_id!=(b).cls_id&&sp_class_le_mod(b,a))\n");
1647816514 buf_puts(&b, "#undef sp_class_ge\n#define sp_class_ge(a,b) sp_class_le_mod(b,a)\n");
16515+ /* sp_poly_get_class: maps a poly value to its sp_Class for dynamic is_a? */
16516+ buf_puts(&b,
16517+ "static sp_Class sp_poly_get_class(sp_RbVal v){\n"
16518+ " switch(v.tag){\n"
16519+ " case SP_TAG_INT: return ((sp_Class){-100});\n"
16520+ " case SP_TAG_STR: return ((sp_Class){-102});\n"
16521+ " case SP_TAG_FLT: return ((sp_Class){-101});\n"
16522+ " case SP_TAG_BOOL: return v.v.b?((sp_Class){-111}):((sp_Class){-112});\n"
16523+ " case SP_TAG_NIL: return ((sp_Class){-110});\n"
16524+ " case SP_TAG_SYM: return ((sp_Class){-103});\n"
16525+ " case SP_TAG_OBJ: if(v.cls_id>=0)return ((sp_Class){v.cls_id});\n"
16526+ " if(v.cls_id>=-12)return ((sp_Class){-104});\n" /* arrays */
16527+ " if(v.cls_id>=-20)return ((sp_Class){-105});\n" /* hashes */
16528+ " return ((sp_Class){-116});\n"
16529+ " default: return ((sp_Class){-116});\n"
16530+ " }\n}\n"
16531+ "static int sp_poly_is_a(sp_RbVal obj,sp_Class klass){\n"
16532+ " return sp_class_le(sp_poly_get_class(obj),klass);\n}\n");
1647916533 for (int ci = 0; ci < c->nclasses; ci++) free(cls_incs[ci]);
1648016534 free(cls_incs); free(cls_nincs);
1648116535 }