8000 Cannot type_parameter a method that returns a Covariant generic · Issue #6745 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Cannot type_parameter a method that returns a Covariant generic #6745

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

Closed
Kache opened this issue Feb 16, 2023 · 2 comments
Closed

Cannot type_parameter a method that returns a Covariant generic #6745

Kache opened this issue Feb 16, 2023 · 2 comments

Comments

@Kache
Copy link
Kache commented Feb 16, 2023

Input

→ View on sorbet.run

# typed: true

extend T::Sig

class CoBox
  extend T::Sig
  extend T::Generic

  Elem = type_member(:out)

  sig { params(value: Elem).void }
  def initialize(value)
    @value = value
  end
end

sig { type_parameters(:V).params(value: T.type_parameter(:V)).returns(CoBox[T.type_parameter(:V)]) }
def co_box(value)
  CoBox.new(value) # errors, but should not
end

# covariant usage
T.let(co_box(Bar.new), CoBox[Bar])
T.let(co_box(Baz.new), CoBox[Bar])


# invariant example follows, just for comparison
class Box
  extend T::Sig
  extend T::Generic

  Elem = type_member

  sig { params(value: Elem).void }
  def initialize(value)
    @value = value
  end
end

sig { type_parameters(:V).params(value: T.type_parameter(:V)).returns(Box[T.type_parameter(:V)]) }
def box(value)
  Box.new(value)
end

class Bar; end
class Baz < Bar; end

T.let(box(Bar.new), Box[Bar])

Observed output

editor.rb:19: Expected CoBox[T.type_parameter(:V) (of Object#co_box)] but found CoBox[<top>] for method result type https://srb.help/7005
    19 |  CoBox.new(value) # errors, but should not
          ^^^^^^^^^^^^^^^^
  Expected CoBox[T.type_parameter(:V) (of Object#co_box)] for result type of method co_box:
    editor.rb:18:
    18 |def co_box(value)
        ^^^^^^^^^^^^^^^^^
  Got CoBox[<top>] originating from:
    editor.rb:19:
    19 |  CoBox.new(value) # errors, but should not
          ^^^^^^^^^^^^^^^^
Errors: 1

Expected behavior

Perhaps I've misunderstood something, but I think methods like co_box shouldn't error.

My current workaround is to have them return CoBox[T.untyped]


@jez
Copy link
Collaborator
jez commented Feb 16, 2023

You are missing a type annotation on CoBox.new:

→ corrected example

This annotation is secretly required. When it is not provided, Sorbet assumes:

  • T.untyped if the generic is invariant
  • <top> if the generic is covariant
  • T.noreturn if the generic is contravariant

The fix for this will be one of #3768 or #4450 (either: require a type argument for all generics, or infer a type argument from the arguments, and require the ones that can't be inferred).

(I've tried both in the past and neither is a simple fix so unfortunately they'll remain open for the time being.)

Closing this as a dupe of those other ones

@jez jez closed this as not planned Won't fix, can't repro, duplicate, stale Feb 16, 2023
@Kache
Copy link
Author
Kache commented Feb 16, 2023

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0