Commit fb072cff

fb072cfffc86Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): is_a? / kind_of? / instance_of? on poly receiver

Without this, <poly>.is_a?(Integer) (or any tag-class check) falls through to the user-class arm of compile_poly_method_call which has no is_a? method on any class, the primitive (Int / Str / Flt / Nil / Bool / Sym / Array / Hash / Range / Proc) arms are missing entirely, and the result is always falsy.

Add a runtime tag-check that inspects SP_TAG_* and cls_id directly: SP_TAG_INT/STR/FLT/SYM/NIL for primitives, SP_TAG_BOOL with .v.b for True/False, the SP_BUILTIN_*_ARRAY / *_HASH / RANGE / PROC cls_ids for built-in containers, plus the mixin-style names (Object, Kernel, BasicObject, Comparable, Numeric) that match every (or several) values. For user-defined classes, OR together every cls_id whose parent chain reaches the queried class — recv.is_a?(C) is true when recv's class IS C or a subclass of C, so the dispatch enumerates descendants, not ancestors. instance_of? falls back to the exact-class check.

Common idiom unblocked: (addr.is_a?(Integer) ? [addr] : addr).each where addr is a heterogeneous Range / Integer / Array param.

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

Details

Commit
fb072cfffc86
Parent
2e1c2c409741

spinel_codegen.rbmodifiedView file

@@ -1784,6 +1784,30 @@
17841784 -1
17851785 end
17861786
1787+ # Walk @cls_parents starting from `child_idx` and return 1 if we
1788+ # ever land on `ancestor_idx`. Used by `is_a?(<Klass>)` on poly
1789+ # receivers to enumerate descendant cls_ids — `recv.is_a?(C)` is
1790+ # true when recv's class is C or any subclass of C, so we OR
1791+ # together every cls_id whose parent chain reaches C.
1792+ def cls_is_descendant(child_idx, ancestor_idx)
1793+ ck = child_idx
1794+ while ck >= 0
1795+ pname = @cls_parents[ck]
1796+ if pname == ""
1797+ return 0
1798+ end
1799+ pi = find_class_idx(pname)
1800+ if pi < 0
1801+ return 0
1802+ end
1803+ if pi == ancestor_idx
1804+ return 1
1805+ end
1806+ ck = pi
1807+ end
1808+ 0
1809+ end
1810+
17871811 def find_method_idx(name)
17881812 i = 0
17891813 while i < @meth_names.length
@@ -26433,6 +26457,66 @@
2643326457 common == "" ? "int" : common
2643426458 end
2643526459
26460+ # Runtime tag-check for `<poly>.is_a?(<klass>)` / `kind_of?` /
26461+ # `instance_of?`. Returns a C boolean expression for the named
26462+ # built-in class (Integer, String, Float, etc.), including
26463+ # mixin-style names that match every value (Object, Kernel,
26464+ # BasicObject, Comparable). Returns "" when the name has no
26465+ # SP_TAG_* mapping (caller falls back to user-class dispatch).
26466+ def poly_is_a_tag_check(klass, recv_tmp, is_instance_of)
26467+ if klass == "Integer"
26468+ return "(" + recv_tmp + ".tag == SP_TAG_INT)"
26469+ end
26470+ if klass == "String"
26471+ return "(" + recv_tmp + ".tag == SP_TAG_STR)"
26472+ end
26473+ if klass == "Float"
26474+ return "(" + recv_tmp + ".tag == SP_TAG_FLT)"
26475+ end
26476+ if klass == "Symbol"
26477+ return "(" + recv_tmp + ".tag == SP_TAG_SYM)"
26478+ end
26479+ if klass == "NilClass"
26480+ return "(" + recv_tmp + ".tag == SP_TAG_NIL)"
26481+ end
26482+ if klass == "TrueClass"
26483+ return "(" + recv_tmp + ".tag == SP_TAG_BOOL && " + recv_tmp + ".v.b)"
26484+ end
26485+ if klass == "FalseClass"
26486+ return "(" + recv_tmp + ".tag == SP_TAG_BOOL && !" + recv_tmp + ".v.b)"
26487+ end
26488+ if klass == "Numeric"
26489+ return "(" + recv_tmp + ".tag == SP_TAG_INT || " + recv_tmp + ".tag == SP_TAG_FLT)"
26490+ end
26491+ if klass == "Comparable"
26492+ if is_instance_of == 1
26493+ return "0"
26494+ end
26495+ return "(" + recv_tmp + ".tag == SP_TAG_INT || " + recv_tmp + ".tag == SP_TAG_FLT || " + recv_tmp + ".tag == SP_TAG_STR || " + recv_tmp + ".tag == SP_TAG_SYM)"
26496+ end
26497+ if klass == "Object" || klass == "Kernel" || klass == "BasicObject"
26498+ # Every value is_a? Object in Ruby. instance_of?(Object) is FALSE
26499+ # for primitives (their class is Integer/String/etc., not Object).
26500+ if is_instance_of == 1
26501+ return "0"
26502+ end
26503+ return "1"
26504+ end
26505+ if klass == "Array"
26506+ return "(" + recv_tmp + ".tag == SP_TAG_OBJ && (" + recv_tmp + ".cls_id == SP_BUILTIN_INT_ARRAY || " + recv_tmp + ".cls_id == SP_BUILTIN_STR_ARRAY || " + recv_tmp + ".cls_id == SP_BUILTIN_FLT_ARRAY || " + recv_tmp + ".cls_id == SP_BUILTIN_SYM_ARRAY || " + recv_tmp + ".cls_id == SP_BUILTIN_PTR_ARRAY || " + recv_tmp + ".cls_id == SP_BUILTIN_POLY_ARRAY))"
26507+ end
26508+ if klass == "Hash"
26509+ return "(" + recv_tmp + ".tag == SP_TAG_OBJ && (" + recv_tmp + ".cls_id == SP_BUILTIN_STR_INT_HASH || " + recv_tmp + ".cls_id == SP_BUILTIN_STR_STR_HASH || " + recv_tmp + ".cls_id == SP_BUILTIN_INT_STR_HASH || " + recv_tmp + ".cls_id == SP_BUILTIN_SYM_INT_HASH || " + recv_tmp + ".cls_id == SP_BUILTIN_SYM_STR_HASH || " + recv_tmp + ".cls_id == SP_BUILTIN_STR_POLY_HASH || " + recv_tmp + ".cls_id == SP_BUILTIN_SYM_POLY_HASH))"
26510+ end
26511+ if klass == "Range"
26512+ return "(" + recv_tmp + ".tag == SP_TAG_OBJ && " + recv_tmp + ".cls_id == SP_BUILTIN_RANGE)"
26513+ end
26514+ if klass == "Proc"
26515+ return "(" + recv_tmp + ".tag == SP_TAG_OBJ && " + recv_tmp + ".cls_id == SP_BUILTIN_PROC)"
26516+ end
26517+ ""
26518+ end
26519+
2643626520 def compile_poly_method_call(nid, rc, mname)
2643726521 @needs_rb_value = 1
2643826522 if mname == "nil?"
@@ -26456,6 +26540,51 @@
2645626540 emit(" mrb_int " + tmp + " = (" + recv_tmp + ".tag == SP_TAG_INT) ? " + recv_tmp + ".v.i : 0;")
2645726541 return tmp
2645826542 end
26543+ # `<poly>.is_a?(<Class>)` / `kind_of?` / `instance_of?` — decide
26544+ # at runtime by inspecting the SP_TAG_* tag (and cls_id for
26545+ # SP_TAG_OBJ). Without this branch the call falls through to the
26546+ # SP_TAG_OBJ dispatch below, which has no `is_a?` method on any
26547+ # user class — every primitive arm returns the default and
26548+ # `is_a?(Integer)` on a poly slot is always falsy. Common in
26549+ # dispatch helpers like `(addr.is_a?(Integer) ? [addr] : addr).each`.
26550+ if mname == "is_a?" || mname == "kind_of?" || mname == "instance_of?"
26551+ args_id = @nd_arguments[nid]
26552+ if args_id >= 0
26553+ a = get_args(args_id)
26554+ if a.length > 0
26555+ arg_name = @nd_name[a[0]]
26556+ recv_tmp = new_temp
26557+ emit(" sp_RbVal " + recv_tmp + " = " + rc + ";")
26558+ io = mname == "instance_of?" ? 1 : 0
26559+ tag_check = poly_is_a_tag_check(arg_name, recv_tmp, io)
26560+ if tag_check != ""
26561+ return tag_check
26562+ end
26563+ # User-defined class: match by cls_id. instance_of? requires
26564+ # exact class; is_a?/kind_of? include subclasses, so
26565+ # enumerate user classes whose ancestor chain reaches
26566+ # arg_name (cls_is_descendant) and OR them in.
26567+ ci = find_class_idx(arg_name)
26568+ if ci >= 0
26569+ if mname == "instance_of?"
26570+ return "(" + recv_tmp + ".tag == SP_TAG_OBJ && " + recv_tmp + ".cls_id == " + ci.to_s + ")"
26571+ end
26572+ check = "(" + recv_tmp + ".tag == SP_TAG_OBJ && (" + recv_tmp + ".cls_id == " + ci.to_s
26573+ cn = 0
26574+ while cn < @cls_names.length
26575+ if cn != ci && cls_is_descendant(cn, ci) == 1
26576+ check = check + " || " + recv_tmp + ".cls_id == " + cn.to_s
26577+ end
26578+ cn = cn + 1
26579+ end
26580+ check = check + "))"
26581+ return check
26582+ end
26583+ return "0"
26584+ end
26585+ end
26586+ return "0"
26587+ end
2645926588 # For object method calls, dispatch based on cls_id. Two namespaces
2646026589 # of cls_id share SP_TAG_OBJ:
2646126590 # - non-negative: index into @cls_names (user-defined classes)