Skip to content

Commit 590f720

Browse files
committed
Fix #7648: Heal variances when comparing eta-expansions
1 parent 77ca516 commit 590f720

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,15 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
579579
val saved = comparedTypeLambdas
580580
comparedTypeLambdas += tp1
581581
comparedTypeLambdas += tp2
582-
try
583-
variancesConform(tp1.typeParams, tp2.typeParams) &&
584-
boundsOK &&
585-
isSubType(tp1.resType, tp2.resType.subst(tp2, tp1))
582+
val variancesOK =
583+
variancesConform(tp1.typeParams, tp2.typeParams)
584+
|| { // if tp1 is of the form [X] =>> C[X] where `C` is co- or contra-variant
585+
// assume the variance of `C` for `tp1` instead. Fixes #7648.
586+
tp1 match
587+
case EtaExpansion(tycon1) => variancesConform(tycon1.typeParams, tp2.typeParams)
588+
case _ => false
589+
}
590+
try variancesOK && boundsOK && isSubType(tp1.resType, tp2.resType.subst(tp2, tp1))
586591
finally comparedTypeLambdas = saved
587592
case _ =>
588593
val tparams1 = tp1.typeParams

tests/pos/i7648.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package i7648
2+
3+
class IO[+A]
4+
5+
trait Functor[F[_]]
6+
trait Monad[F[_]] extends Functor[F]
7+
8+
class Stream[+F[_], +A] {
9+
def take[F1[x] >: F[x]](n: Int)(implicit f: Functor[F1]): Stream[F1, A] = {
10+
this
11+
}
12+
}
13+
14+
object Test with
15+
16+
implicit val ioMonad: Monad[IO] = null
17+
18+
val x = new Stream[IO, Int].take[IO](10)

0 commit comments

Comments
 (0)