NI: Fix smart-cast related regression

See the test and the issue for the clarification
After the change 7898922066
the expected type for "a" in expression "foo(a)" is A<E & B<*>>
But we have the original type A<E> and smart-casted enriched type A<B<*>>
(non of them is a subtype of A<E & B<*>>)
and fail in checkTypeInternal when checking types in during completion

^KT-35844 Fixed
This commit is contained in:
Denis Zharkov
2020-01-10 18:20:44 +03:00
parent 02d9f258d1
commit 7255ee0a5b
6 changed files with 62 additions and 1 deletions
@@ -34,7 +34,11 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.checker.TypeIntersector
import org.jetbrains.kotlin.types.checker.intersectTypes
import org.jetbrains.kotlin.types.checker.intersectWrappedTypes
import org.jetbrains.kotlin.types.typeUtil.expandIntersectionTypeIfNecessary
import org.jetbrains.kotlin.utils.addIfNotNull
import java.util.*
class SmartCastManager(private val argumentTypeResolver: ArgumentTypeResolver) {
@@ -134,7 +138,15 @@ class SmartCastManager(private val argumentTypeResolver: ArgumentTypeResolver) {
else
listOf(expectedType)
for (possibleType in c.dataFlowInfo.getCollectedTypes(dataFlowValue, c.languageVersionSettings)) {
val collectedTypes = c.dataFlowInfo.getCollectedTypes(dataFlowValue, c.languageVersionSettings).toMutableList()
if (collectedTypes.isNotEmpty() && c.languageVersionSettings.supportsFeature(LanguageFeature.NewInference)) {
// Sometime expected type may be inferred to be an intersection of all of the smart-cast types
val typeToIntersect = collectedTypes + dataFlowValue.type
collectedTypes.addIfNotNull(intersectWrappedTypes(typeToIntersect))
}
for (possibleType in collectedTypes) {
if (expectedTypes.any { argumentTypeResolver.isSubtypeOfForArgumentType(possibleType, it) } &&
(additionalPredicate == null || additionalPredicate(possibleType))
) {