Skip to content

Commit 8c80c31

Browse files
committed
Fix #8067: Don't constrain with types of wrong kinds
The example got into a pickle since it tried to constrain a type variable A[_] with a type (G[_] => G[String]). It should have rejected this outright. Instead the constraint addition succeeded and the compiler got into a dead-end with it afterwards.
1 parent eb04b8f commit 8c80c31

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,18 @@ trait ConstraintHandling[AbstractContext] {
524524
pruneLambdaParams(bound)
525525
}
526526

527+
def sameKind(tp1: Type, tp2: Type): Boolean =
528+
tp1.typeParams.corresponds(tp2.typeParams)((p1, p2) =>
529+
sameKind(p1.paramInfo, p2.paramInfo))
530+
527531
try bound match {
528532
case bound: TypeParamRef if constraint contains bound =>
529533
addParamBound(bound)
530534
case _ =>
531535
val pbound = prune(bound)
532-
pbound.exists && (
533-
if (fromBelow) addLowerBound(param, pbound) else addUpperBound(param, pbound))
536+
pbound.exists
537+
&& sameKind(param, pbound)
538+
&& (if fromBelow then addLowerBound(param, pbound) else addUpperBound(param, pbound))
534539
}
535540
finally addConstraintInvocations -= 1
536541
}

tests/pos/i8067.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Test {
2+
3+
trait Tc[F[_]]
4+
type OfString[G[_]] = G[String]
5+
6+
def foo[A[_]](fs: A[String])(implicit f: Tc[A]): A[String] = ???
7+
def bar[B[_]](fs: OfString[B]): B[String] = foo(fs)(new Tc[B] {})
8+
9+
}

0 commit comments

Comments
 (0)