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

Scala 2.12 incorrectly assumes invariance #12821

Closed
j-mie6 opened this issue Jul 10, 2023 · 10 comments
Closed

Scala 2.12 incorrectly assumes invariance #12821

j-mie6 opened this issue Jul 10, 2023 · 10 comments
Milestone

Comments

@j-mie6
Copy link

j-mie6 commented Jul 10, 2023

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 🙂)

@SethTisue SethTisue added this to the 2.13.0 milestone Jul 10, 2023
@SethTisue
Copy link
Member

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

  • a 2.12-only ticket may have value as documentation, even closed
  • a ticket might prompt someone to attempt to backport the fix to 2.12 (it doesn't usually, since not much non-sponsored 2.12 work is happening anymore, but you never know)

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 SethTisue modified the milestones: 2.13.0, 2.13.2 Jul 10, 2023
@SethTisue
Copy link
Member

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
Copy link
Author

j-mie6 commented Jul 10, 2023

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
Copy link
Author

j-mie6 commented Jul 10, 2023

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
Copy link
Member

SethTisue commented Jul 10, 2023

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
Copy link
Author

j-mie6 commented Jul 10, 2023

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
Copy link
Member

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
Copy link
Author

j-mie6 commented Jul 10, 2023

Ok amazing, thanks ❤️

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

@som-snytt
Copy link

PhD restraints

are you getting a PhD or a BD?

@j-mie6
Copy link
Author

j-mie6 commented Jul 10, 2023

Definitely PhD @som-snytt 😛

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

3 participants