Commit a64a0cb5

a64a0cb5c09dMerged

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

Description

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

obj.is_a?(Mod::Klass) previously fell through to compile_expr(arg) and emitted a bare C identifier (Mod_Klass) with no declaration, producing "use of undeclared identifier" at C compile time. The dynamic-arg guard in compile_introspection_expr only excluded the ConstantReadNode shape, so any qualified constant (ConstantPathNode) fell off the static-resolution path entirely.

Compounding the issue, compile_introspection_expr only matched is_a? — neither kind_of? (an MRI alias) nor instance_of? (exact-class match) were routed through it, so both silently fell through to warn_unresolved_call regardless of arg shape.

Fix:

  • Extract is_static_const_ref (1 if ConstantRead/Path, else 0) and resolve_introspection_arg_name (walks ConstantPath via the existing resolve_const_ref_name helper; falls back to @nd_name).
  • Widen the is_a? branch to also match kind_of?; both share the ancestor-walk semantics in MRI.
  • Add an instance_of? branch that does exact-class comparison via the same arg-name resolution helper.

Test (test/introspection_constant_path.rb) exercises all three method names against:

  • Same-class match (is_a?/kind_of?/instance_of? all true)
  • Unrelated-sibling mismatch (all false)
  • Ancestor case via Mod::Klass < Mod::Base (is_a?/kind_of? true, instance_of? false — exact-match semantics distinguished here)
  • Deeper constant path (Mod::Sub::Deep walks a three-level path)

Out-of-scope cases documented in the fixture header:

  • Class-as-local (k = Mod::Klass; obj.is_a?(k)) — separate ConstantPathNode-to-local assignment bug
  • Built-in ancestor (obj.is_a?(Object)) — spinel's class registry doesn't materialize Object as a user-class ancestor

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

Details

Commit
a64a0cb5c09d
Parent
d5b20a0db9db

spinel_codegen.rbmodifiedView file

@@ -17126,9 +17126,29 @@
1712617126 ""
1712717127 end
1712817128
17129+ def is_static_const_ref(aid)
17130+ if @nd_type[aid] == "ConstantReadNode" || @nd_type[aid] == "ConstantPathNode"
17131+ return 1
17132+ end
17133+ 0
17134+ end
17135+
17136+ # Resolve an is_a?/kind_of?/instance_of? argument to its registered
17137+ # class name. ConstantPathNode (`Foo::Bar`) walks the path via
17138+ # resolve_const_ref_name; ConstantReadNode (`Foo`) carries the leaf
17139+ # in @nd_name. Other shapes fall back to @nd_name so static lookup
17140+ # still has a name to try.
17141+ def resolve_introspection_arg_name(aid)
17142+ if @nd_type[aid] == "ConstantPathNode"
17143+ return resolve_const_ref_name(aid)
17144+ end
17145+ @nd_name[aid]
17146+ end
17147+
1712917148 def compile_introspection_expr(nid, mname, rc, recv_type)
17130- # is_a? - check class hierarchy
17131- if mname == "is_a?"
17149+ # is_a? / kind_of? — kind_of? is an exact alias of is_a? in MRI.
17150+ # Both walk the class hierarchy: cname is or inherits from arg0 → TRUE.
17151+ if mname == "is_a?" || mname == "kind_of?"
1713217152 if is_obj_type(recv_type) == 1
1713317153 cname = recv_type[4, recv_type.length - 4]
1713417154 arg0 = ""
@@ -17136,12 +17156,12 @@
1713617156 if args_id >= 0
1713717157 a = get_args(args_id)
1713817158 if a.length > 0
17139- # dynamic Class arg. The
17140- # statically-typed recv knows its own cls_id; the arg
17141- # carries the target cls_id at runtime. sp_class_le walks
17142- # the precomputed ancestors table to decide membership.
17159+ # Dynamic Class arg (a non-const expression evaluating to a class).
17160+ # The statically-typed recv knows its own cls_id; the arg carries
17161+ # the target cls_id at runtime. sp_class_le walks the precomputed
17162+ # ancestors table to decide membership.
1714317163 arg_t = infer_type(a[0])
17144- if arg_t == "class" && @nd_type[a[0]] != "ConstantReadNode"
17164+ if arg_t == "class" && is_static_const_ref(a[0]) == 0
1714517165 ci = find_class_idx(cname)
1714617166 if ci >= 0
1714717167 @needs_class_table = 1
@@ -17149,10 +17169,9 @@
1714917169 return "sp_class_le((sp_Class){" + cls_id_for_user_internal(ci).to_s + "LL}, " + compile_expr(a[0]) + ")"
1715017170 end
1715117171 end
17152- arg0 = @nd_name[a[0]]
17172+ arg0 = resolve_introspection_arg_name(a[0])
1715317173 end
1715417174 end
17155- # Check if cname is or inherits from arg0
1715617175 if is_class_or_ancestor(cname, arg0) == 1
1715717176 return "TRUE"
1715817177 else
@@ -17162,6 +17181,29 @@
1716217181 return "FALSE"
1716317182 end
1716417183
17184+ # instance_of? — exact class identity, no ancestor walk. A parent
17185+ # instance is never an exact-subclass match, so descendant resolution
17186+ # is not relevant here; static comparison suffices.
17187+ if mname == "instance_of?"
17188+ if is_obj_type(recv_type) == 1
17189+ cname = recv_type[4, recv_type.length - 4]
17190+ arg0 = ""
17191+ args_id = @nd_arguments[nid]
17192+ if args_id >= 0
17193+ a = get_args(args_id)
17194+ if a.length > 0
17195+ arg0 = resolve_introspection_arg_name(a[0])
17196+ end
17197+ end
17198+ if cname == arg0
17199+ return "TRUE"
17200+ else
17201+ return "FALSE"
17202+ end
17203+ end
17204+ return "FALSE"
17205+ end
17206+
1716517207 # respond_to? - check if method exists
1716617208 if mname == "respond_to?"
1716717209 if is_obj_type(recv_type) == 1