Commit d5d9d0f2

d5d9d0f252f6Merged

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

Description

fix(codegen): Module#method_defined? compile-time

Foo.method_defined?(:hi) was unsupported and emitted 0. The codegen knows the full method table for every user class at compile time (class_has_method is the same helper used elsewhere to decide static dispatch), so the answer can be baked in.

Two pieces:

  • infer_call_type adds method_defined? to the bool-returning list alongside respond_to?. Without this, results print as 1 / 0 from p instead of true / false.
  • compile_constant_recv_expr's if rcname != "" block gains an early arm: when the method name is method_defined? AND the arg is a SymbolNode or StringNode literal, look up the class index via find_class_idx and return TRUE/FALSE based on class_has_method (which already walks the parent chain and includes attr readers/writers).

Falls through unchanged when the arg isn't a literal symbol / string (e.g. a runtime variable), since we'd need a real method lookup at runtime — out of scope here.

Test: method_defined.rb covers Animal/Dog inheritance, an attr reader + writer pair, the negative case, and the string-arg form.

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

Details

Commit
d5d9d0f252f6
Parent
8ff9aa915a30

spinel_codegen.rbmodifiedView file

@@ -3093,7 +3093,7 @@
30933093 if mname == "is_a?"
30943094 return "bool"
30953095 end
3096- if mname == "respond_to?"
3096+ if mname == "respond_to?" || mname == "method_defined?"
30973097 return "bool"
30983098 end
30993099 if mname == "chr"
@@ -10630,7 +10630,9 @@
1063010630
1063110631 # Does class `ci` provide `mname` as a reader, writer, or method?
1063210632 # Walks parent classes for inherited members.
10633- def class_has_method(ci, mname)
10633+ # Does class `ci` define `mname` directly — instance method, attr
10634+ # reader, or attr writer — without walking the parent chain?
10635+ def class_has_method_local(ci, mname)
1063410636 readers = @cls_attr_readers[ci].split(";")
1063510637 if not_in(mname, readers) == 0
1063610638 return 1
@@ -10646,6 +10648,13 @@
1064610648 if not_in(mname, mnames) == 0
1064710649 return 1
1064810650 end
10651+ return 0
10652+ end
10653+
10654+ def class_has_method(ci, mname)
10655+ if class_has_method_local(ci, mname) == 1
10656+ return 1
10657+ end
1064910658 if @cls_parents[ci] != ""
1065010659 pi = find_class_idx(@cls_parents[ci])
1065110660 if pi >= 0
@@ -24098,6 +24107,31 @@
2409824107 def compile_constant_recv_expr(nid, mname, recv, rc)
2409924108 rcname = constructor_class_name(recv)
2410024109 if rcname != ""
24110+ # Foo.method_defined?(:sym[, inherit=true]) — compile-time decide.
24111+ # Default arm walks the parent chain via class_has_method (covers
24112+ # instance methods, attr readers/writers, ancestors). When the
24113+ # second arg is the literal `false`, restrict the lookup to the
24114+ # receiver's own methods (no parent walk) via class_has_method_local.
24115+ if mname == "method_defined?"
24116+ ci_md = find_class_idx(rcname)
24117+ if ci_md >= 0
24118+ args_id_md = @nd_arguments[nid]
24119+ if args_id_md >= 0
24120+ a_md = get_args(args_id_md)
24121+ if a_md.length > 0
24122+ if @nd_type[a_md[0]] == "SymbolNode" || @nd_type[a_md[0]] == "StringNode"
24123+ lit_name = @nd_content[a_md[0]]
24124+ inherit_md = 1
24125+ if a_md.length >= 2 && @nd_type[a_md[1]] == "FalseNode"
24126+ inherit_md = 0
24127+ end
24128+ hit = inherit_md == 1 ? class_has_method(ci_md, lit_name) : class_has_method_local(ci_md, lit_name)
24129+ return hit == 1 ? "TRUE" : "FALSE"
24130+ end
24131+ end
24132+ end
24133+ end
24134+ end
2410124135 # ARGV methods
2410224136 if rcname == "ARGV"
2410324137 if mname == "length"