Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite recursion bug in .inherited sigs #1636

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions spec/tapioca/runtime/generic_type_registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ class GenericTypeRegistrySpec < Minitest::Spec
refute_same(result, RaisesInInheritedCallback)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ There is a typo in the commit message

assert_operator(result, :<, RaisesInInheritedCallback)
end

it "works for classes that specify a 'loose' sig on .inherited" do
# By "loose", we mean `T::Class[T.anything]` instead of `T::Class[SampleGenericClass[T.anything]]`
_ = HasNonRecursiveInheritedSig[Object]
end

it "FIXME: breaks from infinite recursion if the sig on .inherited uses the generic type" do
# Our swizzled implementation of the `.inherited` method needs to be carefully implemented to not fall into
# infinite recursion when the sig for the method references the class that it's defined on.
assert_raises(SystemStackError) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to not commit code that is being changed by a commit in the same PR. You can introduce a test that breaks, and fix it in the next commit, TDD like.

HasRecursiveInheritedSig[Object]
end
end
end
end

Expand All @@ -98,6 +111,39 @@ def inherited(subclass)
end
end
end

class HasNonRecursiveInheritedSig
extend T::Generic

Element = type_member

class << self
extend T::Sig

# The correct type would be `T::Class[SampleGenericClass[T.anything]]`, but that would crash Tapioca.
# That's not honey Pooh, that's recursion!
sig { params(subclass: T::Class[T.anything]).void }
def inherited(subclass)
super
end
end
end

class HasRecursiveInheritedSig
extend T::Generic

Element = type_member

class << self
extend T::Sig

# This sig references an instantiation of this class itself.
sig { params(subclass: T::Class[HasRecursiveInheritedSig[T.anything]]).void }
def inherited(subclass)
super
end
end
end
end
end
end