From b4dbf0f9a4a8bcc10b3c701510db58b99fb06bdd Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 26 Apr 2019 15:07:01 +0300 Subject: [PATCH] [NI] Fix inference for constraints with captured in projection --- ...ractTypeCheckerContextForConstraintSystem.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt index 70620d2304e..0b1d564258b 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/AbstractTypeCheckerContextForConstraintSystem.kt @@ -74,7 +74,7 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck if (subType.anyBound(this::isMyTypeVariable)) { return simplifyUpperConstraint(subType, superType) && (answer ?: true) } else { - extractTypeVariableForSubtype(subType)?.let { + extractTypeVariableForSubtype(subType, superType)?.let { return simplifyUpperConstraint(it, superType) && (answer ?: true) } @@ -83,12 +83,23 @@ abstract class AbstractTypeCheckerContextForConstraintSystem : AbstractTypeCheck } // extract type variable only from type like Captured(out T) - private fun extractTypeVariableForSubtype(type: KotlinTypeMarker): KotlinTypeMarker? { + private fun extractTypeVariableForSubtype(subType: KotlinTypeMarker, superType: KotlinTypeMarker): KotlinTypeMarker? { - val typeMarker = type.asSimpleType()?.asCapturedType() ?: return null + val typeMarker = subType.asSimpleType()?.asCapturedType() ?: return null val projection = typeMarker.typeConstructorProjection() if (projection.isStarProjection()) return null + if (projection.getVariance() == TypeVariance.IN) { + val type = projection.getType().asSimpleType() ?: return null + if (isMyTypeVariable(type)) { + simplifyLowerConstraint(type, superType) + if (isMyTypeVariable(superType.asSimpleType() ?: return null)) { + addLowerConstraint(superType.typeConstructor(), nullableAnyType()) + } + } + return null + } + return if (projection.getVariance() == TypeVariance.OUT) projection.getType().takeIf { it is SimpleTypeMarker && isMyTypeVariable(it) }?.asSimpleType() else