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

Remember compared polytypes #13663

Merged
merged 2 commits into from
Oct 4, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CommunityBuildTestB:
@Test def fs2 = projects.fs2.run()
@Test def munit = projects.munit.run()
@Test def munitCatsEffect = projects.munitCatsEffect.run()
// @Test def perspective = projects.perspective.run()
@Test def perspective = projects.perspective.run()
@Test def scalacheckEffect = projects.scalacheckEffect.run()
@Test def scodec = projects.scodec.run()
@Test def scodecBits = projects.scodecBits.run()
Expand Down
24 changes: 15 additions & 9 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
val bounds = gadtBounds(sym)
bounds != null && op(bounds)

private inline def comparingTypeLambdas(tl1: TypeLambda, tl2: TypeLambda)(op: => Boolean): Boolean =
val saved = comparedTypeLambdas
comparedTypeLambdas += tl1
comparedTypeLambdas += tl2
try op finally comparedTypeLambdas = saved

protected def isSubType(tp1: Type, tp2: Type, a: ApproxState): Boolean = {
val savedApprox = approx
val savedLeftRoot = leftRoot
Expand Down Expand Up @@ -629,12 +635,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
migrateTo3 ||
tp1.typeParams.corresponds(tp2.typeParams)((tparam1, tparam2) =>
isSubType(tparam2.paramInfo.subst(tp2, tp1), tparam1.paramInfo))
val saved = comparedTypeLambdas
comparedTypeLambdas += tp1
comparedTypeLambdas += tp2
val variancesOK = variancesConform(tp1.typeParams, tp2.typeParams)
try variancesOK && boundsOK && isSubType(tp1.resType, tp2.resType.subst(tp2, tp1))
finally comparedTypeLambdas = saved
comparingTypeLambdas(tp1, tp2) {
val variancesOK = variancesConform(tp1.typeParams, tp2.typeParams)
variancesOK && boundsOK && isSubType(tp1.resType, tp2.resType.subst(tp2, tp1))
}
case _ =>
val tparams1 = tp1.typeParams
if (tparams1.nonEmpty)
Expand Down Expand Up @@ -704,9 +708,11 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
case tp2: PolyType =>
def comparePoly = tp1 match {
case tp1: PolyType =>
(tp1.signature consistentParams tp2.signature) &&
matchingPolyParams(tp1, tp2) &&
isSubType(tp1.resultType, tp2.resultType.subst(tp2, tp1))
comparingTypeLambdas(tp1, tp2) {
(tp1.signature consistentParams tp2.signature)
&& matchingPolyParams(tp1, tp2)
&& isSubType(tp1.resultType, tp2.resultType.subst(tp2, tp1))
}
case _ => false
}
comparePoly
Expand Down
15 changes: 15 additions & 0 deletions tests/pos/i13660.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Const[A] = [_] =>> A
type FunctionK[A[_], B[_]] = [Z] => A[Z] => B[Z]

type #~>#:[T, R] = FunctionK[Const[T], Const[R]]

object FunctionK:
def liftConst[A, B](f: A => B): /*FunctionK[Const[A], Const[B]]*/ A #~>#: B =
[Z1] => (a: A) => f(a)

trait FoldableK[F[_[_], _]]:

def foldMapK1[A[_], C, B](fa: F[A, C])(f: FunctionK[A, Const[B]]): B

def toListK[A, C](fa: F[Const[A], C]): List[A] =
foldMapK1(fa)(FunctionK.liftConst(List(_: A)))
8 changes: 8 additions & 0 deletions tests/pos/i7888.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def usingSeq[B](f: [A] => Seq[A] => B): B = {
f(Nil)
}
def crash() = {
usingSeq { [A] => (a: Seq[A]) =>
a
}
}