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/0frompinstead oftrue/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
if mname == "is_a?" return "bool" end if mname == "respond_to?" if mname == "respond_to?" || mname == "method_defined?" return "bool" end if mname == "chr" # Does class `ci` provide `mname` as a reader, writer, or method? # Walks parent classes for inherited members. def class_has_method(ci, mname) # Does class `ci` define `mname` directly — instance method, attr # reader, or attr writer — without walking the parent chain? def class_has_method_local(ci, mname) readers = @cls_attr_readers[ci].split(";") if not_in(mname, readers) == 0 return 1 if not_in(mname, mnames) == 0 return 1 end return 0 end def class_has_method(ci, mname) if class_has_method_local(ci, mname) == 1 return 1 end if @cls_parents[ci] != "" pi = find_class_idx(@cls_parents[ci]) if pi >= 0 def compile_constant_recv_expr(nid, mname, recv, rc) rcname = constructor_class_name(recv) if rcname != "" # Foo.method_defined?(:sym[, inherit=true]) — compile-time decide. # Default arm walks the parent chain via class_has_method (covers # instance methods, attr readers/writers, ancestors). When the # second arg is the literal `false`, restrict the lookup to the # receiver's own methods (no parent walk) via class_has_method_local. if mname == "method_defined?" ci_md = find_class_idx(rcname) if ci_md >= 0 args_id_md = @nd_arguments[nid] if args_id_md >= 0 a_md = get_args(args_id_md) if a_md.length > 0 if @nd_type[a_md[0]] == "SymbolNode" || @nd_type[a_md[0]] == "StringNode" lit_name = @nd_content[a_md[0]] inherit_md = 1 if a_md.length >= 2 && @nd_type[a_md[1]] == "FalseNode" inherit_md = 0 end hit = inherit_md == 1 ? class_has_method(ci_md, lit_name) : class_has_method_local(ci_md, lit_name) return hit == 1 ? "TRUE" : "FALSE" end end end end end # ARGV methods if rcname == "ARGV" if mname == "length"test/method_defined.rbaddedView file
Loading diff…
test/method_defined.rb.expectedaddedView file
Loading diff…