Commit 04052c60

04052c60b425Merged

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

Description

Resolve alias against inherited methods

alias new old failed to compile when old was inherited from an ancestor rather than defined directly on the class:

Spinel: alias bar -> foo: source method not found in class Child

collect_class_method_alias only consulted cls_find_method_direct (the class's own method table). Fall back to find_method_owner to walk the parent chain, and copy the slot (params / ptypes / return / body_id / defaults) from the owning class's tables.

class Parent; def foo; "parent"; end; end class Child < Parent; alias bar foo; end Child.new.bar # => "parent" (was: compile error)

Two-level inheritance and aliasing a method with arguments are covered by the regression test.

Fixes #1159

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
04052c60b425
Parent
55f7d4ba3081

spinel_analyze.rbmodifiedView file

@@ -9519,16 +9519,33 @@
95199519 # would assign a new body_id to the old slot but the alias slot
95209520 # keeps the original.
95219521 def collect_class_method_alias(ci, new_name, old_name)
9522+ # The source may be defined directly on this class or inherited from
9523+ # an ancestor (`class C < P; alias bar foo` where P defines foo). Walk
9524+ # the parent chain to the owning class and copy that class's slot.
9525+ src_ci = ci
95229526 src = cls_find_method_direct(ci, old_name)
95239527 if src < 0
9528+ owner = find_method_owner(ci, old_name)
9529+ if owner != ""
9530+ oci = find_class_idx(owner)
9531+ if oci >= 0
9532+ osrc = cls_find_method_direct(oci, old_name)
9533+ if osrc >= 0
9534+ src = osrc
9535+ src_ci = oci
9536+ end
9537+ end
9538+ end
9539+ end
9540+ if src < 0
95249541 $stderr.puts "Spinel: alias `" + new_name + "` -> `" + old_name + "`: source method not found in class " + @cls_names[ci]
95259542 exit(1)
95269543 end
9527- pnames_all = @cls_meth_params[ci].split("|", -1)
9528- ptypes_all = @cls_meth_ptypes[ci].split("|", -1)
9529- rets_all = @cls_meth_returns[ci].split(";", -1)
9530- bodies_all = @cls_meth_bodies[ci].split(";", -1)
9531- defs_all = @cls_meth_defaults[ci].split("|", -1)
9544+ pnames_all = @cls_meth_params[src_ci].split("|", -1)
9545+ ptypes_all = @cls_meth_ptypes[src_ci].split("|", -1)
9546+ rets_all = @cls_meth_returns[src_ci].split(";", -1)
9547+ bodies_all = @cls_meth_bodies[src_ci].split(";", -1)
9548+ defs_all = @cls_meth_defaults[src_ci].split("|", -1)
95329549 params = pnames_all[src] || ""
95339550 ptypes = ptypes_all[src] || ""
95349551 ret = rets_all[src] || "int"