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

Linearize singleton ancestors #2214

Merged
merged 2 commits into from
Jul 4, 2024
Merged

Conversation

vinistock
Copy link
Member

Motivation

Closes #1939, closes #1938, closes #1333

This PR adds lazy singleton ancestor linearization, which allows us to properly handle constants, methods and instance variables that are inherited by a singleton class.

Implementation

  1. We already had lazy creation of singleton classes spreading across three different places. Moved that to the Index, which is the entity that's actually concerned with those. Started using it in RBSIndexer and the regular declaration listener
  2. Removed the concept of extend. An extend is nothing but a convenience to include something in the singleton class. If we simply treat all extends as singleton class inclusions, the entire algorithm works as is and we don't have to handle another type of operation
  3. Finally, the most important part is how to lazily determine the ancestors based on how many levels of singleton we are checking. Here's how the algorithm works:

For each level of singleton, the ancestors are: the linearization of the same level of singleton + the linearization of the level - 1 of the Class class. For example:

class Foo; end
class Bar < Foo; end

# First singleton
Bar.singleton_class.ancestors
# => [Bar::<Class:Bar>,
#     Foo::<Class:Foo>,
#     Object::<Class:Object>,
#     BasicObject::<Class:BasicObject>, # <<<< Until here, it's the ancestors of <Class:Bar>
#     Class, Module, Object, Kernel, BasicObject] # <<< Ancestors of Class

# Second singleton
Bar.singleton_class.singleton_class.ancestors
# => [Bar::<Class:Bar>::<Class:<Class:Bar>>,
#     Foo::<Class:Foo>::<Class:<Class:Foo>>,
#     Object::<Class:Object>::<Class:<Class:Object>>,
#     BasicObject::<Class:BasicObject>::<Class:<Class:BasicObject>>,
#     ^^^^^^^ until here, it's the ancestors of <Class:<Class:Bar>>
#     Class::<Class:Class>, Module::<Class:Module>, Object::<Class:Object>,
#     BasicObject::<Class:BasicObject>,
#     ^^^^^^^ then the ancestors of <Class:Class> (the singleton level - 1)
#     Class, Module, Object, Kernel, BasicObject]
#     ^^^^^^^ And finally the ancestors of Class

Automated Tests

Added tests.

Manual Tests

  1. Launch LSP on this branch
  2. Type File. in any file
  3. Verify that you see methods inherited from the IO class, such as the method pipe

@vinistock vinistock added enhancement New feature or request server This pull request should be included in the server gem's release notes labels Jun 20, 2024
@vinistock vinistock self-assigned this Jun 20, 2024
@vinistock vinistock requested a review from a team as a code owner June 20, 2024 20:01
@vinistock vinistock requested review from andyw8 and st0012 June 20, 2024 20:01
@vinistock vinistock force-pushed the vs/linearizing_singleton_ancestors branch from bfd1084 to 28d1aca Compare June 21, 2024 18:19
@vinistock vinistock force-pushed the vs/linearizing_singleton_ancestors branch from 28d1aca to 92eb2a0 Compare July 2, 2024 19:17
lib/ruby_indexer/lib/ruby_indexer/index.rb Show resolved Hide resolved
lib/ruby_indexer/test/index_test.rb Outdated Show resolved Hide resolved
@vinistock vinistock force-pushed the vs/linearizing_singleton_ancestors branch from 92eb2a0 to 1bb3c8c Compare July 4, 2024 17:53
Co-authored-by: Alexandre Terrasa <[email protected]>
@vinistock vinistock force-pushed the vs/linearizing_singleton_ancestors branch from 1bb3c8c to 5be1528 Compare July 4, 2024 19:24
@vinistock vinistock force-pushed the vs/linearizing_singleton_ancestors branch from 5be1528 to fe7c0ea Compare July 4, 2024 19:25
@vinistock vinistock enabled auto-merge (squash) July 4, 2024 19:25
@vinistock vinistock merged commit a4391c6 into main Jul 4, 2024
34 checks passed
@vinistock vinistock deleted the vs/linearizing_singleton_ancestors branch July 4, 2024 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request server This pull request should be included in the server gem's release notes
Projects
None yet
2 participants