Skip to content

Commit 01fc181

Browse files
committed
Fix problems with TypeVar instantiation
1) Simplify skipped one level over arguments of AndType/OrType. 2) variances needs to follow instantiated typevars
1 parent 4fab474 commit 01fc181

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,14 @@ class TypeComparer(initctx: Context) extends DotClass {
631631
def comparePolyParam = {
632632
tp1 == tp2 ||
633633
isSubTypeWhenFrozen(bounds(tp1).hi, tp2) || {
634-
if (!frozenConstraint &&
635-
(tp2 isRef defn.NothingClass) &&
636-
ctx.typerState.isGlobalCommittable)
637-
ctx.log(s"!!! instantiating to Nothing: $tp1")
638-
if (isConstrained(tp1)) addConstraint(tp1, tp2, fromBelow = false)
634+
if (isConstrained(tp1))
635+
addConstraint(tp1, tp2, fromBelow = false) && {
636+
if ((!frozenConstraint) &&
637+
(tp2 isRef defn.NothingClass) &&
638+
ctx.typerState.isGlobalCommittable)
639+
ctx.log(s"!!! instantiated to Nothing: $tp1, constraint = ${constraint.show}")
640+
true
641+
}
639642
else (ctx.mode is Mode.TypevarsMissContext) || thirdTry(tp1, tp2)
640643
}
641644
}

src/dotty/tools/dotc/core/Types.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ object Types {
828828
def variances(include: TypeVar => Boolean)(implicit ctx: Context): VarianceMap = track("variances") {
829829
val accu = new TypeAccumulator[VarianceMap] {
830830
def apply(vmap: VarianceMap, t: Type): VarianceMap = t match {
831-
case t: TypeVar if include(t) =>
831+
case t: TypeVar if !t.isInstantiated && include(t) =>
832832
val v = vmap(t)
833833
if (v == null) vmap.updated(t, variance)
834834
else if (v == variance) vmap
@@ -852,9 +852,9 @@ object Types {
852852
class Simplify extends TypeMap {
853853
def apply(tp: Type): Type = tp match {
854854
case AndType(l, r) =>
855-
mapOver(l) & mapOver(r)
855+
this(l) & this(r)
856856
case OrType(l, r) =>
857-
mapOver(l) | mapOver(r)
857+
this(l) | this(r)
858858
case tp: PolyParam =>
859859
ctx.typerState.constraint.typeVarOfParam(tp) orElse tp
860860
case _ =>

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,8 @@ class Typer extends Namer with Applications with Implicits {
12251225
if (tree.tpe <:< pt) tree
12261226
else if (ctx.mode is Mode.Pattern) tree // no subtype check for pattern
12271227
else {
1228-
typr.println(s"adapt to subtype ${tree.tpe} !<:< $pt")
1229-
typr.println(TypeComparer.explained(implicit ctx => tree.tpe <:< pt))
1228+
typr.println(s"adapt to subtype ${tree.tpe.show} !<:< ${pt.show}")
1229+
//typr.println(TypeComparer.explained(implicit ctx => tree.tpe <:< pt))
12301230
adaptToSubType(wtp)
12311231
}
12321232
}

tests/pos/collections.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import scala.collection.generic.CanBuildFrom
22

33
object collections {
44

5+
val arr = Array("a", "b")
6+
val aa = arr ++ arr
7+
58
List(1, 2, 3) map (x => 2)
69

710
val s = Set(1, 2, 3)

0 commit comments

Comments
 (0)