Skip to content

Commit a8bd625

Browse files
committed
Clean up logic in isFullyDefinedAccumulator
1 parent 7c1f7f0 commit a8bd625

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,29 @@ object Inferencing {
8989
* to their upper bound.
9090
*/
9191
private class IsFullyDefinedAccumulator(force: ForceDegree.Value)(implicit ctx: Context) extends TypeAccumulator[Boolean] {
92+
9293
private def instantiate(tvar: TypeVar, fromBelow: Boolean): Type = {
9394
val inst = tvar.instantiate(fromBelow)
9495
typr.println(i"forced instantiation of ${tvar.origin} = $inst")
9596
inst
9697
}
98+
9799
private[this] var toMaximize: Boolean = false
100+
98101
def apply(x: Boolean, tp: Type): Boolean = tp.dealias match {
99102
case _: WildcardType | _: ProtoType =>
100103
false
101104
case tvar: TypeVar
102105
if !tvar.isInstantiated && ctx.typerState.constraint.contains(tvar) =>
103106
force.appliesTo(tvar) && {
104-
val direction = instDirection(tvar.origin)
105-
if (direction != 0) {
106-
//if (direction > 0) println(s"inst $tvar dir = up")
107-
instantiate(tvar, direction < 0)
108-
}
109-
else {
110-
val minimize =
111-
force.minimizeAll ||
112-
variance >= 0 && !(
113-
!force.allowBottom &&
114-
defn.isBottomType(ctx.typeComparer.approximation(tvar.origin, fromBelow = true)))
115-
if (minimize) instantiate(tvar, fromBelow = true)
116-
else toMaximize = true
117-
}
107+
val pref = tvar.origin
108+
def avoidBottom =
109+
!force.allowBottom &&
110+
defn.isBottomType(ctx.typeComparer.approximation(pref, fromBelow = true))
111+
def preferMax =
112+
instDirection(pref) > 0 || variance >= 0 && avoidBottom
113+
if (force.minimizeAll || !preferMax) instantiate(tvar, fromBelow = true)
114+
else toMaximize = true
118115
foldOver(x, tvar)
119116
}
120117
case tp =>

tests/pos/i6127.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1+
import reflect.ClassTag
2+
class Co[+S]
3+
object Co {
4+
def empty[X: ClassTag]: Co[X] = ???
5+
}
6+
class Contra[-S]
7+
object Contra {
8+
def empty[X: ClassTag]: Contra[X] = ???
9+
}
110
class Foo[+FT](x: FT) {
211
def fooArray: Foo[Array[String]] = new Foo(Array.empty)
3-
val y: Array[String] = Array.empty
4-
}
12+
val y1: Array[String] = Array.empty
13+
def fooCo: Foo[Co[String]] = new Foo(Co.empty)
14+
val y2: Co[String] = Co.empty
15+
def fooContra: Foo[Contra[String]] = new Foo(Contra.empty)
16+
val y3: Contra[String] = Contra.empty
17+
}
18+

0 commit comments

Comments
 (0)