Skip to content

Commit 2133efd

Browse files
committed
Fix #6385: Don't instantiate hk type constructors too early
The issue #6385 contains more explanations.
1 parent d78b8c0 commit 2133efd

File tree

8 files changed

+33
-13
lines changed

8 files changed

+33
-13
lines changed

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,6 @@ trait ConstraintHandling[AbstractContext] {
501501
}
502502
}
503503

504-
/** Instantiate `param` to `tp` if the constraint stays satisfiable */
505-
protected def tryInstantiate(param: TypeParamRef, tp: Type)(implicit actx: AbstractContext): Boolean = {
506-
val saved = constraint
507-
constraint =
508-
if (addConstraint(param, tp, fromBelow = true) &&
509-
addConstraint(param, tp, fromBelow = false)) constraint.replace(param, tp)
510-
else saved
511-
constraint ne saved
512-
}
513-
514504
/** Check that constraint is fully propagated. See comment in Config.checkConstraintsPropagated */
515505
def checkPropagated(msg: => String)(result: Boolean)(implicit actx: AbstractContext): Boolean = {
516506
if (Config.checkConstraintsPropagated && result && addConstraintInvocations == 0) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
808808
tycon1.dealiasKeepRefiningAnnots match {
809809
case tycon1: TypeParamRef =>
810810
(tycon1 == tycon2 ||
811-
canConstrain(tycon1) && tryInstantiate(tycon1, tycon2)) &&
811+
canConstrain(tycon1) && isSubType(tycon1, tycon2)) &&
812812
isSubArgs(args1, args2, tp1, tparams)
813813
case tycon1: TypeRef =>
814814
tycon2.dealiasKeepRefiningAnnots match {
@@ -882,7 +882,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
882882
tl => tparams1.map(tparam => tl.integrate(tparams, tparam.paramInfo).bounds),
883883
tl => tp1base.tycon.appliedTo(args1.take(lengthDiff) ++
884884
tparams1.indices.toList.map(tl.paramRefs(_))))
885-
(assumedTrue(tycon2) || tryInstantiate(tycon2, tycon1.ensureLambdaSub)) &&
885+
(assumedTrue(tycon2) || isSubType(tycon1.ensureLambdaSub, tycon2)) &&
886886
recur(tp1, tycon1.appliedTo(args2))
887887
}
888888
}
@@ -967,7 +967,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
967967
case param1: TypeParamRef =>
968968
def canInstantiate = tp2 match {
969969
case AppliedType(tycon2, args2) =>
970-
tryInstantiate(param1, tycon2.ensureLambdaSub) && isSubArgs(args1, args2, tp1, tycon2.typeParams)
970+
isSubType(param1, tycon2.ensureLambdaSub) && isSubArgs(args1, args2, tp1, tycon2.typeParams)
971971
case _ =>
972972
false
973973
}

compiler/test/dotc/pos-test-pickling.blacklist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
i94-nada.scala
12
i1812.scala
23
i1867.scala
34
i3067.scala

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ class CompilationTests extends ParallelTesting {
165165
compileFile("tests/neg-custom-args/nopredef.scala", defaultOptions.and("-Yno-predef")),
166166
compileFile("tests/neg-custom-args/noimports.scala", defaultOptions.and("-Yno-imports")),
167167
compileFile("tests/neg-custom-args/noimports2.scala", defaultOptions.and("-Yno-imports")),
168+
compileFile("tests/neg-custom-args/i1650.scala", allowDeepSubtypes),
168169
compileFile("tests/neg-custom-args/i3882.scala", allowDeepSubtypes),
169170
compileFile("tests/neg-custom-args/i4372.scala", allowDeepSubtypes),
170171
compileFile("tests/neg-custom-args/i1754.scala", allowDeepSubtypes),
File renamed without changes.

tests/neg/i6385a.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Box[F[_]]
2+
3+
class C[X]
4+
class D[X] extends C[String]
5+
6+
object Test {
7+
def f[F[_]](x: Box[F]) = ???
8+
def db: Box[D] = ???
9+
def cb: Box[C] = db // error
10+
f[[X] => C[X]](db) // error
11+
}
File renamed without changes.

tests/pos/i6385.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Tc1[A]
2+
trait Tc2[A] extends Tc1[A]
3+
4+
class PinTypeTo[K[_]]
5+
object PinTypeTo {
6+
implicit val pinType: PinTypeTo[Tc2] = new PinTypeTo[Tc2]
7+
}
8+
9+
class X
10+
object X {
11+
implicit def Tc2Instance[F[x] >: Tc2[x]: PinTypeTo]: F[X] = new Tc2[X] {}
12+
}
13+
14+
object app extends App {
15+
implicitly[Tc2[X]] // ok
16+
implicitly[Tc1[X]]//(X.Tc2Instance[Tc2]) // fails
17+
}

0 commit comments

Comments
 (0)