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

Initialize signature showing Object.new signature but not the specific class signature #2585

Closed
dgmora opened this issue Sep 20, 2024 · 5 comments · Fixed by #2950
Closed
Labels
bug Something isn't working help-wanted Extra attention is needed non-vscode

Comments

@dgmora
Copy link

dgmora commented Sep 20, 2024

Description

Reproduction steps

  1. Create a class with an initialize method, give it no documentation.
  2. In another place, instantiate this class
  3. The signature help displays class.rbs help. It doesn't matter if the signature is different or no.
  4. I would expect at least to see the signature.

This may be an issue with the neovim/lsp configuration, I'm wondering if other people see this too. I have seen this with my classes, but also with gems. The classes for the gems are loaded correctly, however when I try to instantiate it, I don't see the signature. Could be related to #2553 ?

I've created a Dockerfile where you can see this happening. There are some annoying warnings, I need to update the Dockerfile.

curl -o Dockerfile https://gist.githubusercontent.com/dgmora/f869b0692db5fafdf6d43090ec9cb46f/raw/Dockerfile && \
  docker build -t lazyvim-lsp . && \
  docker run -it lazyvim-lsp
# `nvim` to start neovim
# inside:
#   `<space>`  to see available keymaps (and wait) 
#   `<space>e` to toggle filetree
#   `a`        to create a file in the file tree

Image

@dgmora dgmora added bug Something isn't working help-wanted Extra attention is needed non-vscode labels Sep 20, 2024
@dgmora dgmora changed the title Initialize signature showing Object signature but not the class' signature Initialize signature showing Object.new signature but not the specific class signature Sep 20, 2024
@vinistock
Copy link
Member

Thank you for the bug report! This is not related to the PR you mentioned, it was always like this. Essentially, we need to map the fact that when invoking new, the signature you should see is the one for initialize (i.e.: the names don't match).

Sorbet solves this with what it calls intrinsics, defining custom type checking rules for certain method calls. We would need something similar to that so that we can create mappings between what is invoked and what the user wants to see.

This is the same case for something like perform_now or perform_later in Rails, where you don't define those methods, but instead define perform - and that's the one you're interested in seeing documentation for and jumping to.

Note: important to remember for whoever ends up tackling this issue that Ruby does allow you to override self.new and do something completely different that doesn't even involve initialize. We will need to make a decision here: do we want to pay the performance price to check if the user has overridden self.new and favour correctness or always consider new -> initialize and prefer better performance?

@dgmora
Copy link
Author

dgmora commented Oct 3, 2024

Thanks for the clarification!

I've also noticed that if the first class has a method like

class MyExample
  def test
    puts 'hi'
  end
  ...
end

Doing MyExample.new.te doesn't show test. Is that related?

@vinistock
Copy link
Member

Not exactly. That would require some special handling for the new method in our type inferrer.

However, we explored type flow analysis in #2421 and concluded that the effort vs benefit was not worth it at the moment (you essentially need to build an entire type checker to get that working). I say that because of this:

# We can make this work properly because all we need to look into is the immediate receiver
MyExample.new.test

# This requires more sophisticated flow analysis, which we will not invest effort at this time
# Although this specific example might look simple, the mechanisms to ensure proper type flow analysis
# are not. We need to properly track scopes, create an internal type system so that we can represent
# unions if variables are set conditionally and many other fundamental pieces of a full fledged type
# checker
a = MyExample.new
a.test

Copy link
Contributor

github-actions bot commented Dec 3, 2024

This issue is being marked as stale because there was no activity in the last 2 months

@github-actions github-actions bot added the Stale label Dec 3, 2024
@andyw8
Copy link
Contributor

andyw8 commented Dec 3, 2024

Let's add this to the roadmap: #2950

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help-wanted Extra attention is needed non-vscode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants