scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.84k stars 1.05k forks source link

Invalid variance inferred / variance check failed #21625

Open sir-wabbit opened 3 weeks ago

sir-wabbit commented 3 weeks ago

Compiler version

3.5.0

Minimized code

trait Foo[-L] {
  def test[LF, F[_ >: L] >: LF]: Unit
}

Output

contravariant type L occurs in covariant position in type [_$1 >: L] >: LF of type F

Expectation

Should compile. Does compile in Scala 2.

This works for example and I am not sure that the outer lower bound should change anything.

trait Foo[-L] {
  def test[F[_ >: L]]: Unit
}

This also doesn't work

trait Foo[-L] {
  def test[F[_ >: L] >: Unit]: Unit
}

But this does:

trait Foo[-L] {
  def test[F[_ >: L] >: Nothing]: Unit
}
sir-wabbit commented 3 weeks ago

Even funnier:

trait Foo[+L] {
  def test[F[_ >: L] >: Unit]: Unit
}

this complains that a covariant type occurs in ... contravariant position.

dwijnand commented 3 weeks ago

Firstly F[_ >: L] >: LF is our syntax for F >: [_ >: L] =>> LF. When we check the lower bound we flip to contravariance. But when we try to figure out what the variance of that L is, we accidentally bias to being in a covariant position, when we're actually in contravariant position. So seems like it should be an easy fix (waiting for CI).