Skip to content

Commit

Permalink
Fix use of alias_method with inheritance (#3329)
Browse files Browse the repository at this point in the history
  • Loading branch information
djudd-stripe committed Jul 29, 2020
1 parent 8f3904b commit d05dd7f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
16 changes: 11 additions & 5 deletions gems/sorbet-runtime/lib/types/private/methods/_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ def self._on_method_added(hook_mod, method_name, is_singleton_method: false)
new_method = nil
T::Private::ClassUtils.replace_method(mod, method_name) do |*args, &blk|
method_sig = T::Private::Methods.maybe_run_sig_block_for_method(new_method)
method_sig ||= T::Private::Methods._handle_missing_method_signature(mod, original_method, __callee__)
method_sig ||= T::Private::Methods._handle_missing_method_signature(
is_singleton_method ? self.singleton_class : self.class,
original_method,
__callee__,
)

# Should be the same logic as CallValidation.wrap_method_if_needed but we
# don't want that extra layer of indirection in the callstack
Expand Down Expand Up @@ -238,9 +242,11 @@ def self._on_method_added(hook_mod, method_name, is_singleton_method: false)
end
end

def self._handle_missing_method_signature(mod, original_method, callee)
def self._handle_missing_method_signature(klass, original_method, callee)
method_sig = T::Private::Methods.signature_for_method(original_method)
aliasing_method = mod.instance_method(callee)

aliasing_method = klass.instance_method(callee)
aliasing_mod = aliasing_method.owner

if method_sig && aliasing_method != original_method && aliasing_method.original_name == original_method.name
# We're handling a case where `alias` or `alias_method` was called for a
Expand All @@ -249,10 +255,10 @@ def self._handle_missing_method_signature(mod, original_method, callee)
# Note, this logic is duplicated above, make sure to keep changes in sync.
if method_sig.check_level == :always || (method_sig.check_level == :tests && T::Private::RuntimeLevels.check_tests?)
# Checked, so copy the original signature to the aliased copy.
T::Private::Methods.unwrap_method(mod, method_sig, aliasing_method)
T::Private::Methods.unwrap_method(aliasing_mod, method_sig, aliasing_method)
else
# Unchecked, so just make `alias_method` behave as if it had been called pre-sig.
mod.send(:alias_method, callee, original_method.name)
aliasing_mod.send(:alias_method, callee, original_method.name)
end
else
raise "`sig` not present for method `#{aliasing_method.name}` but you're trying to run it anyways. " \
Expand Down
76 changes: 76 additions & 0 deletions gems/sorbet-runtime/test/types/edge_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,42 @@ def foo(x=:foo)
allocs = counting_allocations {obj.bar}
assert_equal(0, allocs)
end

it 'handles alias to superclass method with runtime checking' do
superclass = Class.new do
extend T::Sig

sig {params(x: Symbol).returns(Symbol)}
def foo(x=:foo)
x
end
end

subclass = Class.new(superclass) do
alias_method :bar, :foo
end

assert_equal(:foo, subclass.new.foo)
assert_equal(:foo, subclass.new.bar)
end

it 'handles alias to superclass method without runtime checking' do
superclass = Class.new do
extend T::Sig

sig {params(x: Symbol).returns(Symbol).checked(:never)}
def foo(x=:foo)
x
end
end

subclass = Class.new(superclass) do
alias_method :bar, :foo
end

assert_equal(:foo, subclass.new.foo)
assert_equal(:foo, subclass.new.bar)
end
end

describe 'singleton method' do
Expand Down Expand Up @@ -183,6 +219,46 @@ class << self
assert_equal(:foo, klass.bar)
assert_equal(:foo, klass.foo)
end

it 'handles alias_method to superclass method with runtime checking' do
superclass = Class.new do
extend T::Sig

sig {params(x: Symbol).returns(Symbol)}
def self.foo(x=:foo)
x
end
end

subclass = Class.new(superclass) do
class << self
alias_method :bar, :foo
end
end

assert_equal(:foo, subclass.foo)
assert_equal(:foo, subclass.bar)
end

it 'handles alias_method to superclass method without runtime checking' do
superclass = Class.new do
extend T::Sig

sig {params(x: Symbol).returns(Symbol).checked(:never)}
def self.foo(x=:foo)
x
end
end

subclass = Class.new(superclass) do
class << self
alias_method :bar, :foo
end
end

assert_equal(:foo, subclass.foo)
assert_equal(:foo, subclass.bar)
end
end
end

Expand Down

0 comments on commit d05dd7f

Please sign in to comment.