Commit 46bf6410

46bf64108329Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +4 added · ~1 modified

Description

Check singleton methods/accessors in respond_to? on a constant

respond_to? on a class or module constant always returned false: the only respond_to? handler covered primitive and instance (obj_*) receivers, so a constant receiver fell through to the generic "0" fallback. CRuby checks the receiver's singleton method set.

Add a respond_to? arm to compile_constant_recv_expr that, for a literal symbol/string argument, returns TRUE when the name is:

  • a class << self; attr_accessor :x module-singleton accessor (matching both the reader :x and writer :x=),
  • a def self.m / module_function singleton method, or
  • a method every class/module object responds to (name, to_s, class, new for classes, ...). Otherwise FALSE. A non-literal argument falls through unchanged.

Fixes #1045 Fixes #1046

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

Details

Commit
46bf64108329
Parent
8145379a9d65

spinel_codegen.rbmodifiedView file

@@ -5558,6 +5558,46 @@
55585558 -1
55595559 end
55605560
5561+ # 1 if `cn` has a singleton method `mname` — either a synthetic
5562+ # `<cn>_cls_<mname>` (module_function / top-level form) or an
5563+ # in-class `def self.<mname>` recorded in @cls_cmeth_names.
5564+ def cls_singleton_method_exists(cn, mname)
5565+ if find_method_idx(cn + "_cls_" + mname) >= 0
5566+ return 1
5567+ end
5568+ ci = find_class_idx(cn)
5569+ if ci >= 0
5570+ cmnames = @cls_cmeth_names[ci].split(";", -1)
5571+ ki = 0
5572+ while ki < cmnames.length
5573+ if cmnames[ki] == mname
5574+ return 1
5575+ end
5576+ ki = ki + 1
5577+ end
5578+ end
5579+ 0
5580+ end
5581+
5582+ # 1 if `mname` is a method every class/module object responds to.
5583+ # `new`/`allocate` are class-only (a module has no allocator).
5584+ def const_universal_method?(rcname, mname)
5585+ if mname == "name" || mname == "to_s" || mname == "inspect" ||
5586+ mname == "class" || mname == "==" || mname == "!=" ||
5587+ mname == "===" || mname == "respond_to?" || mname == "is_a?" ||
5588+ mname == "kind_of?" || mname == "instance_of?" || mname == "hash" ||
5589+ mname == "freeze" || mname == "frozen?" || mname == "instance_methods" ||
5590+ mname == "method_defined?" || mname == "ancestors"
5591+ return 1
5592+ end
5593+ if mname == "new" || mname == "allocate"
5594+ if find_class_idx(rcname) >= 0 && module_name_exists(rcname) == 0
5595+ return 1
5596+ end
5597+ end
5598+ 0
5599+ end
5600+
55615601 # Walk the AST for `Module.accessor = RHS` writes where
55625602 # (Module, accessor) was registered in `collect_module` as a
55635603 # singleton accessor. Accumulates the set of distinct
@@ -25506,6 +25546,37 @@
2550625546 end
2550725547 end
2550825548 end
25549+ # Foo.respond_to?(:sym) on a class/module constant. CRuby checks
25550+ # the receiver's singleton method set (def self.m / module_function
25551+ # / class << self accessors), not its instance methods. The literal
25552+ # symbol/string case is decided at compile time; a non-literal arg
25553+ # falls through to the generic path.
25554+ if mname == "respond_to?"
25555+ args_id_rt = @nd_arguments[nid]
25556+ if args_id_rt >= 0
25557+ a_rt = get_args(args_id_rt)
25558+ if a_rt.length > 0 && (@nd_type[a_rt[0]] == "SymbolNode" || @nd_type[a_rt[0]] == "StringNode")
25559+ qname = @nd_content[a_rt[0]]
25560+ # `attr_accessor :x` in `class << self` registers the reader
25561+ # key only; the writer `x=` shares the same slot, so strip a
25562+ # trailing `=` before the accessor lookup.
25563+ qbase = qname
25564+ if qbase.length > 0 && qbase[qbase.length - 1, 1] == "="
25565+ qbase = qbase[0, qbase.length - 1]
25566+ end
25567+ if find_module_acc_idx(rcname + "." + qbase) >= 0
25568+ return "TRUE"
25569+ end
25570+ if cls_singleton_method_exists(rcname, qname) == 1
25571+ return "TRUE"
25572+ end
25573+ if const_universal_method?(rcname, qname) == 1
25574+ return "TRUE"
25575+ end
25576+ return "FALSE"
25577+ end
25578+ end
25579+ end
2550925580 # ARGV methods
2551025581 if rcname == "ARGV"
2551125582 if mname == "length"