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 :xmodule-singleton accessor (matching both the reader:xand writer:x=), - a
def self.m/module_functionsingleton 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
-155595559
end55605560
5561+
# 1 if `cn` has a singleton method `mname` — either a synthetic5562+
# `<cn>_cls_<mname>` (module_function / top-level form) or an5563+
# 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) >= 05566+
return 15567+
end5568+
ci = find_class_idx(cn)5569+
if ci >= 05570+
cmnames = @cls_cmeth_names[ci].split(";", -1)5571+
ki = 05572+
while ki < cmnames.length5573+
if cmnames[ki] == mname5574+
return 15575+
end5576+
ki = ki + 15577+
end5578+
end5579+
05580+
end5581+
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 15592+
end5593+
if mname == "new" || mname == "allocate"5594+
if find_class_idx(rcname) >= 0 && module_name_exists(rcname) == 05595+
return 15596+
end5597+
end5598+
05599+
end5600+
55615601
# Walk the AST for `Module.accessor = RHS` writes where55625602
# (Module, accessor) was registered in `collect_module` as a55635603
# singleton accessor. Accumulates the set of distinct@@ -25506,6 +25546,37 @@
2550625546
end2550725547
end2550825548
end25549+
# Foo.respond_to?(:sym) on a class/module constant. CRuby checks25550+
# the receiver's singleton method set (def self.m / module_function25551+
# / class << self accessors), not its instance methods. The literal25552+
# symbol/string case is decided at compile time; a non-literal arg25553+
# falls through to the generic path.25554+
if mname == "respond_to?"25555+
args_id_rt = @nd_arguments[nid]25556+
if args_id_rt >= 025557+
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 reader25561+
# key only; the writer `x=` shares the same slot, so strip a25562+
# trailing `=` before the accessor lookup.25563+
qbase = qname25564+
if qbase.length > 0 && qbase[qbase.length - 1, 1] == "="25565+
qbase = qbase[0, qbase.length - 1]25566+
end25567+
if find_module_acc_idx(rcname + "." + qbase) >= 025568+
return "TRUE"25569+
end25570+
if cls_singleton_method_exists(rcname, qname) == 125571+
return "TRUE"25572+
end25573+
if const_universal_method?(rcname, qname) == 125574+
return "TRUE"25575+
end25576+
return "FALSE"25577+
end25578+
end25579+
end2550925580
# ARGV methods2551025581
if rcname == "ARGV"2551125582
if mname == "length"test/respond_to_def_self_method.rbaddedView file
Loading diff…
test/respond_to_def_self_method.rb.expectedaddedView file
Loading diff…
test/respond_to_module_singleton_accessor.rbaddedView file
Loading diff…
test/respond_to_module_singleton_accessor.rb.expectedaddedView file
Loading diff…