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) andresolve_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
"" end def is_static_const_ref(aid) if @nd_type[aid] == "ConstantReadNode" || @nd_type[aid] == "ConstantPathNode" return 1 end 0 end # Resolve an is_a?/kind_of?/instance_of? argument to its registered # class name. ConstantPathNode (`Foo::Bar`) walks the path via # resolve_const_ref_name; ConstantReadNode (`Foo`) carries the leaf # in @nd_name. Other shapes fall back to @nd_name so static lookup # still has a name to try. def resolve_introspection_arg_name(aid) if @nd_type[aid] == "ConstantPathNode" return resolve_const_ref_name(aid) end @nd_name[aid] end def compile_introspection_expr(nid, mname, rc, recv_type) # is_a? - check class hierarchy if mname == "is_a?" # is_a? / kind_of? — kind_of? is an exact alias of is_a? in MRI. # Both walk the class hierarchy: cname is or inherits from arg0 → TRUE. if mname == "is_a?" || mname == "kind_of?" if is_obj_type(recv_type) == 1 cname = recv_type[4, recv_type.length - 4] arg0 = "" if args_id >= 0 a = get_args(args_id) if a.length > 0 # dynamic Class arg. The # statically-typed recv knows its own cls_id; the arg # carries the target cls_id at runtime. sp_class_le walks # the precomputed ancestors table to decide membership. # Dynamic Class arg (a non-const expression evaluating to a class). # The statically-typed recv knows its own cls_id; the arg carries # the target cls_id at runtime. sp_class_le walks the precomputed # ancestors table to decide membership. arg_t = infer_type(a[0]) if arg_t == "class" && @nd_type[a[0]] != "ConstantReadNode" if arg_t == "class" && is_static_const_ref(a[0]) == 0 ci = find_class_idx(cname) if ci >= 0 @needs_class_table = 1 return "sp_class_le((sp_Class){" + cls_id_for_user_internal(ci).to_s + "LL}, " + compile_expr(a[0]) + ")" end end arg0 = @nd_name[a[0]] arg0 = resolve_introspection_arg_name(a[0]) end end # Check if cname is or inherits from arg0 if is_class_or_ancestor(cname, arg0) == 1 return "TRUE" else return "FALSE" end # instance_of? — exact class identity, no ancestor walk. A parent # instance is never an exact-subclass match, so descendant resolution # is not relevant here; static comparison suffices. if mname == "instance_of?" if is_obj_type(recv_type) == 1 cname = recv_type[4, recv_type.length - 4] arg0 = "" args_id = @nd_arguments[nid] if args_id >= 0 a = get_args(args_id) if a.length > 0 arg0 = resolve_introspection_arg_name(a[0]) end end if cname == arg0 return "TRUE" else return "FALSE" end end return "FALSE" end # respond_to? - check if method exists if mname == "respond_to?" if is_obj_type(recv_type) == 1test/introspection_constant_path.rbaddedView file
Loading diff…
test/introspection_constant_path.rb.expectedaddedView file
Loading diff…