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

compile error scalaz with Scala 2.13.2 ( "covariant type A occurs in invariant position") #11953

Closed
xuwei-k opened this issue Apr 23, 2020 · 6 comments
Labels

Comments

@xuwei-k
Copy link

xuwei-k commented Apr 23, 2020

reproduction steps

git clone git://github.com/scalaz/scalaz.git
cd scalaz
git checkout scala-2.13.2
sbt ++2.13.2 coreJVM/compile

problem

[error] /home/travis/build/scalaz/scalaz/core/src/main/scala/scalaz/IList.scala:510:29: covariant type A occurs in invariant position in type [+A]scalaz.INil[A] {
[error]   def <init>(): scalaz.INil._INil[A]
[error] } of class _INil
[error]   private[this] final class _INil[+A] extends INil[A]
[error]                             ^

expectation

not compile error.

@xuwei-k xuwei-k added the typer label Apr 23, 2020
@SethTisue
Copy link
Member

@joroKr21 might this have to do with scala/scala#8545 ?

@xuwei-k
Copy link
Author

xuwei-k commented Apr 23, 2020

more simpler example

package example

sealed trait IList[A]

sealed abstract case class INil[A] () extends IList[A]
object INil {
  private[this] final class _INil[+A] extends INil[A]
}

@smarter
Copy link
Member

smarter commented Apr 23, 2020

This is unsound in general so scalac is right to reject this code:

sealed trait IList[A]

sealed abstract case class INil[A]() extends IList[A] { var elem: A = _ }
object INil {
  private[this] final class _INil[+A] extends INil[A]

  def main(args: Array[String]): Unit = {
    val n = new _INil[Int]
    (n: _INil[Any]).elem = "foo"
    val i: Int = n.elem // ClassCastException
  }
}

@smarter smarter closed this as completed Apr 23, 2020
@smarter
Copy link
Member

smarter commented Apr 23, 2020

(you should be able to use @uncheckedVariance to bypass this)

@xuwei-k
Copy link
Author

xuwei-k commented Apr 23, 2020

Thank you for quick response!

@joroKr21
Copy link
Member

What @smarter said 😄

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

No branches or pull requests

4 participants