Skip to content

Subtyping error involving higher-kinded types and covariance #3264

Closed
@julienrf

Description

@julienrf

Consider the following code:

trait Foo[+F[_]] {
  def bar: Bar[F]
}

trait Bar[+F[_]]

trait Base[+A]
trait Sub[A] extends Base[A]

object Main {
  def main(args: Array[String]): Unit = {
    def makeFoo(barSub: Bar[Sub]): Foo[Base] =
      new Foo[Base] {
        def bar/*: Bar[Base]*/ = barSub
      }
  }
}

We get the following type error (with 0.3.0-RC2):

-- [E007] Type Mismatch Error: /tmp/scastie1099504913399535263/src/main/scala/main.scala:14:33 
14 |        def bar/*: Bar[Base]*/ = barSub
   |                                 ^^^^^^
   |                                 found:    Bar[Sub](barSub)
   |                                 required: Bar[Base]
   |                                 

Try it in Scastie: https://scastie.scala-lang.org/julienrf/ijDLJLEPRRqt4FpHCpPRwg/1

If we uncomment the explicit type annotation, it compiles fine. Also, if make the type parameter A non-variant in Base it compiles fine even without the explicit type annotation.

(Discovered here: scala/collection-strawman#262)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions