scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Scala 2.12 incorrectly assumes invariance #12821

Closed j-mie6 closed 11 months ago

j-mie6 commented 11 months ago

Reproduction steps

Scala version: 2.12.18

trait InvK[F[_]]
trait InvKCo[F[+_]]

// object, class, trait all fine
// just need to be able to define `type`s
object Foo {
  // X must occur in a covariant position in A
  type A[+X] = X

  // typechecker incorrectly assumes that A's X is in an invariant position due to F not being F[+_]
  def i: InvK[A] = ???
  def j: InvKCo[A] = ???
}

Problem

In Scala 2.13.11 and Scala 3.3.0, this code compiles entirely fine. In Scala 2.12 however, it reports the following errors:

covariant type A appears in an invariant position in type => InvK[Foo.A] of method i covariant type A appears in an invariant position in type => InvKCo[Foo.A] of method j

Both of these are incorrectly assuming the variance is mismatched, but this is not the case, and adding a covariance annotation as in InvKCo does not help either.

There is a workaround, which is to use @uncheckedVariance.

(thanks @s5bug for the minimisation :slightly_smiling_face:)

SethTisue commented 11 months ago

When something is fixed in 2.13, we don't keep the ticket open. So I'm closing, but note regardless that:

Despite our best intentions, closing a ticket is sometimes taken as a slap in the face, as we saw on e.g. #11259. I assure you that's not our intention.

SethTisue commented 11 months ago

Not sure what 2.13.2 PR fixed this, but the "Compiler fixes" section of https://github.com/scala/scala/releases/tag/v2.13.2 has a couple of obvious candidates

j-mie6 commented 11 months ago

Despite our best intentions, closing a ticket is sometimes taken as a slap in the face, as we saw on e.g. #11259. I assure you that's not our intention.

My face is positively unslapped 🙂

j-mie6 commented 11 months ago

Do I understand correctly that you verified that it fails on 2.13.1 and not 2.13.2?

In which case, I might try and backport it myself some time in the distant future

SethTisue commented 11 months ago

Do I understand correctly that you verified that it fails on 2.13.1 and not 2.13.2?

yeah. scala-cli is great for this sort of testing (scala-cli -S 2.13.1)

j-mie6 commented 11 months ago

Awesome. I assume I can publishLocal a snapshot of the scala compiler or something if I wanted to test each PR in turn at some point?

SethTisue commented 11 months ago

Every merged PR has a corresponding nightly build, not on Maven Central, but on https://scala-ci.typesafe.com/artifactory/scala-integration/ — where scala-cli will look by default if you ask for a version number such as scala-cli -S 2.13.2-bin-1bde691 (which is the nightly corresponding to scala/scala#8651). So it's relatively easy to git bisect in the scala/scala repo — you just git bisect skip whenever it suggests a commit other than a PR merge commit.

j-mie6 commented 11 months ago

Ok amazing, thanks ❤️

Hopefully when I'm free of my PhD restraints I can try and hunt this down!

som-snytt commented 11 months ago

PhD restraints

are you getting a PhD or a BD?

j-mie6 commented 11 months ago

Definitely PhD @som-snytt 😛