@@ -79,8 +79,10 @@ object Inferencing {
79
79
* constrained upper bound != given upper bound and
80
80
* constrained lower bound == given lower bound).
81
81
* If (1) and (2) do not apply:
82
- * 3. T is maximized if it appears only contravariantly in the given type.
83
- * 4. T is minimized in all other cases.
82
+ * 3. T is minimized if forceDegree is minimizeAll.
83
+ * 4. Otherwise, T is maximized if it appears only contravariantly in the given type,
84
+ * or if forceDegree is `noBottom` and T's minimized value is a bottom type.
85
+ * 5. Otherwise, T is minimized.
84
86
*
85
87
* The instantiation is done in two phases:
86
88
* 1st Phase: Try to instantiate minimizable type variables to
@@ -89,32 +91,30 @@ object Inferencing {
89
91
* to their upper bound.
90
92
*/
91
93
private class IsFullyDefinedAccumulator (force : ForceDegree .Value )(implicit ctx : Context ) extends TypeAccumulator [Boolean ] {
94
+
92
95
private def instantiate (tvar : TypeVar , fromBelow : Boolean ): Type = {
93
96
val inst = tvar.instantiate(fromBelow)
94
97
typr.println(i " forced instantiation of ${tvar.origin} = $inst" )
95
98
inst
96
99
}
100
+
97
101
private [this ] var toMaximize : Boolean = false
102
+
98
103
def apply (x : Boolean , tp : Type ): Boolean = tp.dealias match {
99
104
case _ : WildcardType | _ : ProtoType =>
100
105
false
101
106
case tvar : TypeVar
102
107
if ! tvar.isInstantiated && ctx.typerState.constraint.contains(tvar) =>
103
108
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
- }
109
+ val pref = tvar.origin
110
+ val direction = instDirection(pref)
111
+ def avoidBottom =
112
+ ! force.allowBottom &&
113
+ defn.isBottomType(ctx.typeComparer.approximation(pref, fromBelow = true ))
114
+ def preferMin = force.minimizeAll || variance >= 0 && ! avoidBottom
115
+ if (direction != 0 ) instantiate(tvar, fromBelow = direction < 0 )
116
+ else if (preferMin) instantiate(tvar, fromBelow = true )
117
+ else toMaximize = true
118
118
foldOver(x, tvar)
119
119
}
120
120
case tp =>
0 commit comments