Skip to content

Fix #7648: Heal variances when comparing eta-expansions #7707

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

Merged
merged 2 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,15 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
val saved = comparedTypeLambdas
comparedTypeLambdas += tp1
comparedTypeLambdas += tp2
try
variancesConform(tp1.typeParams, tp2.typeParams) &&
boundsOK &&
isSubType(tp1.resType, tp2.resType.subst(tp2, tp1))
val variancesOK =
variancesConform(tp1.typeParams, tp2.typeParams)
|| { // if tp1 is of the form [X] =>> C[X] where `C` is co- or contra-variant
// assume the variance of `C` for `tp1` instead. Fixes #7648.
tp1 match
case EtaExpansion(tycon1) => variancesConform(tycon1.typeParams, tp2.typeParams)
case _ => false
}
try variancesOK && boundsOK && isSubType(tp1.resType, tp2.resType.subst(tp2, tp1))
finally comparedTypeLambdas = saved
case _ =>
val tparams1 = tp1.typeParams
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/tcpoly_typealias.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait BOk4 extends A3 {
// does type alias signature (not considering RHS) correspond to abstract type member in super class
// does RHS correspond to the type alias sig
trait BInv extends A{
type m[x] = FooCov[x] // error: invariant x in alias def
type m[x] = FooCov[x] // was an error: invariant x in alias def, now ok, since `FooCov` is covariant
}

trait BCon extends A{
Expand Down
18 changes: 18 additions & 0 deletions tests/pos/i7648.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package i7648

class IO[+A]

trait Functor[F[_]]
trait Monad[F[_]] extends Functor[F]

class Stream[+F[_], +A] {
def take[F1[x] >: F[x]](n: Int)(implicit f: Functor[F1]): Stream[F1, A] = {
this
}
}

object Test with

implicit val ioMonad: Monad[IO] = null

val x = new Stream[IO, Int].take[IO](10)