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

Show the T.attached_class specificity note when the attached class is inside something else #7746

Open
neilparikh opened this issue Mar 5, 2024 · 0 comments
Labels
enhancement New feature or surprising current feature

Comments

@neilparikh
Copy link
Collaborator

Input

→ View on sorbet.run

# typed: true
extend T::Sig

class A
  extend T::Sig

  sig {returns(T.attached_class)}
  def self.make
    A.new
  end
end

class B
  extend T::Sig

  sig {returns(T::Array[T.attached_class])}
  def self.make
    B.new
  end
end

Observed output

editor.rb:9: Expected T.attached_class (of A) but found A for method result type https://srb.help/7005
     9 |    A.new
            ^^^^^
  Expected T.attached_class (of A) for result type of method make:
    editor.rb:8:
     8 |  def self.make
          ^^^^^^^^^^^^^
  Got A originating from:
    editor.rb:9:
     9 |    A.new
            ^^^^^
  Note:
    A is incompatible with T.attached_class (of A) because when this method is called on a subclass T.attached_class (of A) will represent a more specific subclass, meaning A will not be specific enough. See https://sorbet.org/docs/attached-class for more.

editor.rb:18: Expected T::Array[T.attached_class (of B)] but found B for method result type https://srb.help/7005
    18 |    B.new
            ^^^^^
  Expected T::Array[T.attached_class (of B)] for result type of method make:
    editor.rb:17:
    17 |  def self.make
          ^^^^^^^^^^^^^
  Got B originating from:
    editor.rb:18:
    18 |    B.new
            ^^^^^
Errors: 2

Expected behavior

The note about attached class for A should also appear for B.

It would also be nice to recommend to the user that they can replace A.new with self.new if we can detect that.


@neilparikh neilparikh added the enhancement New feature or surprising current feature label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or surprising current feature
Projects
None yet
Development

No branches or pull requests

1 participant