[NI] Preserve nullability of resulting type from CST if it's possible

Consider common supertype of `S` and `Nothing`, where `S` has nullable
 upper bound or it's flexible. Before the fix, result was `S?`, which
 is correct but too conservative. Now, we'll preserve nullability of
 resulting type if it's already nullable.

 This happened because we were failing to find path of not-nullable
 types from `Nothing` to `S`, which should obviously exists by
 semantics of Nothing
This commit is contained in:
Mikhail Zarechenskiy
2019-03-04 01:18:07 +03:00
parent 9b3e17f0d7
commit 93e79afab4
5 changed files with 59 additions and 1 deletions
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.types.checker.TypeCheckerContext.SeveralSupertypesWi
import org.jetbrains.kotlin.types.checker.TypeCheckerContext.SupertypesPolicy
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny
import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.makeNullable
import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -452,7 +453,7 @@ object NullabilityChecker {
}
private fun TypeCheckerContext.hasPathByNotMarkedNullableNodes(start: SimpleType, end: TypeConstructor) =
anySupertype(start, { !it.isMarkedNullable && it.constructor == end }) {
anySupertype(start, { it.isNothing() || (!it.isMarkedNullable && it.constructor == end) }) {
if (it.isMarkedNullable) SupertypesPolicy.None else SupertypesPolicy.LowerIfFlexible
}