Skip to content

Commit 18ce5d2

Browse files
oderskysmarter
authored andcommitted
Don't instantiate hk type constructors too early
The issue scala#6385 contains more explanations.
1 parent c8eb14f commit 18ce5d2

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
@@ -520,16 +520,6 @@ trait ConstraintHandling[AbstractContext] {
520520
}
521521
}
522522

523-
/** Instantiate `param` to `tp` if the constraint stays satisfiable */
524-
protected def tryInstantiate(param: TypeParamRef, tp: Type)(implicit actx: AbstractContext): Boolean = {
525-
val saved = constraint
526-
constraint =
527-
if (addConstraint(param, tp, fromBelow = true) &&
528-
addConstraint(param, tp, fromBelow = false)) constraint.replace(param, tp)
529-
else saved
530-
constraint ne saved
531-
}
532-
533523
/** Check that constraint is fully propagated. See comment in Config.checkConstraintsPropagated */
534524
def checkPropagated(msg: => String)(result: Boolean)(implicit actx: AbstractContext): Boolean = {
535525
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
@@ -818,7 +818,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
818818
tycon1.dealiasKeepRefiningAnnots match {
819819
case tycon1: TypeParamRef =>
820820
(tycon1 == tycon2 ||
821-
canConstrain(tycon1) && tryInstantiate(tycon1, tycon2)) &&
821+
canConstrain(tycon1) && isSubType(tycon1, tycon2)) &&
822822
isSubArgs(args1, args2, tp1, tparams)
823823
case tycon1: TypeRef =>
824824
tycon2.dealiasKeepRefiningAnnots match {
@@ -892,7 +892,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
892892
tl => tparams1.map(tparam => tl.integrate(tparams, tparam.paramInfo).bounds),
893893
tl => tp1base.tycon.appliedTo(args1.take(lengthDiff) ++
894894
tparams1.indices.toList.map(tl.paramRefs(_))))
895-
(assumedTrue(tycon2) || tryInstantiate(tycon2, tycon1.ensureLambdaSub)) &&
895+
(assumedTrue(tycon2) || isSubType(tycon1.ensureLambdaSub, tycon2)) &&
896896
recur(tp1, tycon1.appliedTo(args2))
897897
}
898898
}
@@ -977,7 +977,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
977977
case param1: TypeParamRef =>
978978
def canInstantiate = tp2 match {
979979
case AppliedType(tycon2, args2) =>
980-
tryInstantiate(param1, tycon2.ensureLambdaSub) && isSubArgs(args1, args2, tp1, tycon2.typeParams)
980+
isSubType(param1, tycon2.ensureLambdaSub) && isSubArgs(args1, args2, tp1, tycon2.typeParams)
981981
case _ =>
982982
false
983983
}

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
@@ -164,6 +164,7 @@ class CompilationTests extends ParallelTesting {
164164
compileFile("tests/neg-custom-args/nopredef.scala", defaultOptions.and("-Yno-predef")),
165165
compileFile("tests/neg-custom-args/noimports.scala", defaultOptions.and("-Yno-imports")),
166166
compileFile("tests/neg-custom-args/noimports2.scala", defaultOptions.and("-Yno-imports")),
167+
compileFile("tests/neg-custom-args/i1650.scala", allowDeepSubtypes),
167168
compileFile("tests/neg-custom-args/i3882.scala", allowDeepSubtypes),
168169
compileFile("tests/neg-custom-args/i4372.scala", allowDeepSubtypes),
169170
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)