Commit 32d326c6

32d326c660ebMerged

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

Description

Merge pull request #459 from ryanseys/rs-introspection-const-path

fix(codegen): resolve qualified-constant arg in is_a?/kind_of?/instance_of?

Details

Commit
32d326c660eb
Parent
b90c30c3b0d9
Merge parent
a64a0cb5c09d

spinel_codegen.rbmodifiedView file

@@ -17147,9 +17147,29 @@
1714717147 ""
1714817148 end
1714917149
17150+ def is_static_const_ref(aid)
17151+ if @nd_type[aid] == "ConstantReadNode" || @nd_type[aid] == "ConstantPathNode"
17152+ return 1
17153+ end
17154+ 0
17155+ end
17156+
17157+ # Resolve an is_a?/kind_of?/instance_of? argument to its registered
17158+ # class name. ConstantPathNode (`Foo::Bar`) walks the path via
17159+ # resolve_const_ref_name; ConstantReadNode (`Foo`) carries the leaf
17160+ # in @nd_name. Other shapes fall back to @nd_name so static lookup
17161+ # still has a name to try.
17162+ def resolve_introspection_arg_name(aid)
17163+ if @nd_type[aid] == "ConstantPathNode"
17164+ return resolve_const_ref_name(aid)
17165+ end
17166+ @nd_name[aid]
17167+ end
17168+
1715017169 def compile_introspection_expr(nid, mname, rc, recv_type)
17151- # is_a? - check class hierarchy
17152- if mname == "is_a?"
17170+ # is_a? / kind_of? — kind_of? is an exact alias of is_a? in MRI.
17171+ # Both walk the class hierarchy: cname is or inherits from arg0 → TRUE.
17172+ if mname == "is_a?" || mname == "kind_of?"
1715317173 if is_obj_type(recv_type) == 1
1715417174 cname = recv_type[4, recv_type.length - 4]
1715517175 arg0 = ""
@@ -17157,12 +17177,12 @@
1715717177 if args_id >= 0
1715817178 a = get_args(args_id)
1715917179 if a.length > 0
17160- # dynamic Class arg. The
17161- # statically-typed recv knows its own cls_id; the arg
17162- # carries the target cls_id at runtime. sp_class_le walks
17163- # the precomputed ancestors table to decide membership.
17180+ # Dynamic Class arg (a non-const expression evaluating to a class).
17181+ # The statically-typed recv knows its own cls_id; the arg carries
17182+ # the target cls_id at runtime. sp_class_le walks the precomputed
17183+ # ancestors table to decide membership.
1716417184 arg_t = infer_type(a[0])
17165- if arg_t == "class" && @nd_type[a[0]] != "ConstantReadNode"
17185+ if arg_t == "class" && is_static_const_ref(a[0]) == 0
1716617186 ci = find_class_idx(cname)
1716717187 if ci >= 0
1716817188 @needs_class_table = 1
@@ -17170,10 +17190,9 @@
1717017190 return "sp_class_le((sp_Class){" + cls_id_for_user_internal(ci).to_s + "LL}, " + compile_expr(a[0]) + ")"
1717117191 end
1717217192 end
17173- arg0 = @nd_name[a[0]]
17193+ arg0 = resolve_introspection_arg_name(a[0])
1717417194 end
1717517195 end
17176- # Check if cname is or inherits from arg0
1717717196 if is_class_or_ancestor(cname, arg0) == 1
1717817197 return "TRUE"
1717917198 else
@@ -17183,6 +17202,29 @@
1718317202 return "FALSE"
1718417203 end
1718517204
17205+ # instance_of? — exact class identity, no ancestor walk. A parent
17206+ # instance is never an exact-subclass match, so descendant resolution
17207+ # is not relevant here; static comparison suffices.
17208+ if mname == "instance_of?"
17209+ if is_obj_type(recv_type) == 1
17210+ cname = recv_type[4, recv_type.length - 4]
17211+ arg0 = ""
17212+ args_id = @nd_arguments[nid]
17213+ if args_id >= 0
17214+ a = get_args(args_id)
17215+ if a.length > 0
17216+ arg0 = resolve_introspection_arg_name(a[0])
17217+ end
17218+ end
17219+ if cname == arg0
17220+ return "TRUE"
17221+ else
17222+ return "FALSE"
17223+ end
17224+ end
17225+ return "FALSE"
17226+ end
17227+
1718617228 # respond_to? - check if method exists
1718717229 if mname == "respond_to?"
1718817230 if is_obj_type(recv_type) == 1