Skip to content

Commit c802c4e

Browse files
committed
Don't interpolate downwards to a bottom type
If the result of a covariant type variable interpolation would be a bottom type, wait instead. This could make the variable be inferred to its upper bound after all, if we do not need a fully instantiated type right away. This makes a difference for indentation whereever we have a situation like this: ``` def foo: T = bla.asInstanceOf ``` With indentation on, this is now equivalent to ``` def foo: T = { bla.asInstanceof } ``` This means there is now an added opportunity for an interpolation step, of the asInstanceOf, which would go downwards to Nothing. So indentation changes the inferred type, which is very bad. With the commit the problem is avoided. The commit is broken out separately into PR #7130.
1 parent d0ffce5 commit c802c4e

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,14 @@ trait Inferencing { this: Typer =>
434434
else if (!hasUnreportedErrors)
435435
if (v.intValue != 0) {
436436
typr.println(i"interpolate $tvar in $state in $tree: $tp, fromBelow = ${v.intValue == 1}, $constraint")
437-
tvar.instantiate(fromBelow = v.intValue == 1)
437+
if (true) {
438+
val fromBelow = v.intValue == 1
439+
val instType = ctx.typeComparer.instanceType(tvar.origin, fromBelow)
440+
if (!(fromBelow && instType.isRef(defn.NothingClass)))
441+
tvar.instantiateWith(instType)
442+
}
443+
else
444+
tvar.instantiate(fromBelow = v.intValue == 1)
438445
}
439446
else typr.println(i"no interpolation for nonvariant $tvar in $state")
440447
}
File renamed without changes.
File renamed without changes.

tests/run/type-propagation.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test extends App {
2+
def foo: String = {
3+
"abc".asInstanceOf
4+
}
5+
6+
assert(foo == "abc")
7+
}

0 commit comments

Comments
 (0)