Pattern matching: work-around for smart cast target type instability #KT-14705 Fixed

This commit is contained in:
mglukhikh
2017-01-23 14:18:49 +03:00
committed by Mikhail Glukhikh
parent bc0550d7b7
commit b53ebd115b
3 changed files with 29 additions and 9 deletions
@@ -245,15 +245,6 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
possibleTypesForSubject: Set<KotlinType>
) {
val subjectExpression = expression.subjectExpression ?: return
val isNullableType = TypeUtils.isNullableType(subjectType)
val bindingContext = contextBeforeSubject.trace.bindingContext
if (isNullableType && !WhenChecker.containsNullCase(expression, bindingContext)) {
val notNullableType = TypeUtils.makeNotNullable(subjectType)
if (checkSmartCastToExpectedTypeInSubject(contextBeforeSubject, subjectExpression, subjectType,
notNullableType)) {
return
}
}
for (possibleCastType in possibleTypesForSubject) {
val possibleCastClass = possibleCastType.constructor.declarationDescriptor as? ClassDescriptor ?: continue
if (possibleCastClass.kind == ClassKind.ENUM_CLASS || possibleCastClass.modality == Modality.SEALED) {
@@ -263,6 +254,15 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
}
}
}
val isNullableType = TypeUtils.isNullableType(subjectType)
val bindingContext = contextBeforeSubject.trace.bindingContext
if (isNullableType && !WhenChecker.containsNullCase(expression, bindingContext)) {
val notNullableType = TypeUtils.makeNotNullable(subjectType)
if (checkSmartCastToExpectedTypeInSubject(contextBeforeSubject, subjectExpression, subjectType,
notNullableType)) {
return
}
}
}
private fun checkSmartCastToExpectedTypeInSubject(