Skip to content

Commit 7b4d3f0

Browse files
authored
Backport "A slightly more conservative version of #14218" (#18371)
Backports #18352
2 parents 952a811 + 110c91f commit 7b4d3f0

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ object Types {
246246
case _ => false
247247
}
248248

249+
/** Is this type exactly `Any`, or a type lambda ending in `Any`? */
250+
def isTopOfSomeKind(using Context): Boolean = dealias match
251+
case tp: TypeLambda => tp.resType.isTopOfSomeKind
252+
case _ => isExactlyAny
253+
249254
def isBottomType(using Context): Boolean =
250255
if ctx.mode.is(Mode.SafeNulls) && !ctx.phase.erasedTypes then hasClassSymbol(defn.NothingClass)
251256
else isBottomTypeAfterErasure
@@ -4813,7 +4818,7 @@ object Types {
48134818
def hasLowerBound(using Context): Boolean = !currentEntry.loBound.isExactlyNothing
48144819

48154820
/** For uninstantiated type variables: Is the upper bound different from Any? */
4816-
def hasUpperBound(using Context): Boolean = !currentEntry.hiBound.finalResultType.isExactlyAny
4821+
def hasUpperBound(using Context): Boolean = !currentEntry.hiBound.isTopOfSomeKind
48174822

48184823
/** Unwrap to instance (if instantiated) or origin (if not), until result
48194824
* is no longer a TypeVar

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ object Inferencing {
187187
// else hold off instantiating unbounded unconstrained variable
188188
else if direction != 0 then
189189
instantiate(tvar, fromBelow = direction < 0)
190-
else if variance >= 0 && (force.ifBottom == IfBottom.ok && !tvar.hasUpperBound || tvar.hasLowerBound) then
190+
else if variance >= 0 && tvar.hasLowerBound then
191+
instantiate(tvar, fromBelow = true)
192+
else if (variance > 0 || variance == 0 && !tvar.hasUpperBound)
193+
&& force.ifBottom == IfBottom.ok
194+
then // if variance == 0, prefer upper bound if one is given
191195
instantiate(tvar, fromBelow = true)
192196
else if variance >= 0 && force.ifBottom == IfBottom.fail then
193197
fail = true

0 commit comments

Comments
 (0)