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

Infer types for method calls on immediately instantiated objects #3007

Conversation

vinistock
Copy link
Member

@vinistock vinistock commented Jan 3, 2025

Motivation

Closes #2994

This PR adds inference for methods invoked directly on an object instantiation. It's fairly easy to add, so I think it's worth getting it done.

Implementation

When the receiver of a method call is another method call using new, we infer the type of the constant receiver and then return the attached version of it. For example:

# In this case, `Foo::<Class:Foo>` is the receiver of `new`, but new will return an
# object. Thus the right type is the attached class, which is `Foo`
Foo.new.something

The only exception is if the method new was overridden, which is valid in Ruby. We cannot guarantee that the override actually returns a new object of the class, so it's better to not infer anything in those cases.

Automated Tests

Added tests.

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@vinistock vinistock added server This pull request should be included in the server gem's release notes enhancement New feature or request labels Jan 3, 2025 — with Graphite App
@vinistock vinistock requested review from andyw8 and st0012 January 3, 2025 16:02
@vinistock vinistock marked this pull request as ready for review January 3, 2025 16:06
@vinistock vinistock requested a review from a team as a code owner January 3, 2025 16:06
Copy link
Contributor

@andyw8 andyw8 left a comment

Choose a reason for hiding this comment

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

Nice enhancement. Can we also make a note of this in the docs to help users understand why completions may or may not be available.

@vinistock
Copy link
Member Author

Can we also make a note of this in the docs to help users understand why completions may or may not be available.

@andyw8 a note of what exactly? How we infer the type for new?

@andyw8
Copy link
Contributor

andyw8 commented Jan 6, 2025

I'm thinking something like:

Due to Ruby being a dynamically-typed language, completions are only available when we know the type of the receiver, for example:

  • methods invoked directly on a singleton class
  • methods invoked on a literal
  • methods invoked immediately after a instantiating an object with new

@vinistock vinistock force-pushed the 01-03-infer_types_for_method_calls_on_immediately_instantiated_objects branch from 797cab1 to da24be0 Compare January 6, 2025 22:07
@andyw8
Copy link
Contributor

andyw8 commented Jan 6, 2025

Note is good, LGTM.

@vinistock vinistock force-pushed the 01-03-infer_types_for_method_calls_on_immediately_instantiated_objects branch from da24be0 to 2241ced Compare January 6, 2025 22:49
@vinistock vinistock merged commit f82fccc into main Jan 6, 2025
42 checks passed
Copy link
Member Author

Merge activity

  • Jan 6, 6:12 PM EST: A user merged this pull request with Graphite.

@vinistock vinistock deleted the 01-03-infer_types_for_method_calls_on_immediately_instantiated_objects branch January 6, 2025 23:12
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
Development

Successfully merging this pull request may close these issues.

"Go to definition" showing not relevant list
3 participants