From e7bd464a3c186ef9e05fa45813d3a9203951e749 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Fri, 11 Feb 2022 11:41:24 +0300 Subject: [PATCH] [FE 1.0] Check if type variable is bounded in upper by incompatible types (resolution stage) ^KT-51221 Fixed --- .../calls/components/ResolutionParts.kt | 19 +++++++++++++++++++ .../resolve/calls/model/KotlinCallKind.kt | 10 +++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt index 0b24afeacad..5a4ecbc47c3 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt @@ -899,4 +899,23 @@ internal object CheckContextReceiversResolutionPart : ResolutionPart() { addDiagnostic(NoContextReceiver(candidateContextReceiverParameter)) return null } +} + +internal object CheckIncompatibleTypeVariableUpperBounds : ResolutionPart() { + override fun ResolutionCandidate.process(workIndex: Int) = with(getSystem().asConstraintSystemCompleterContext()) { + val typeVariables = getSystem().getBuilder().currentStorage().notFixedTypeVariables.values + + for (variableWithConstraints in typeVariables) { + val upperTypes = variableWithConstraints.constraints.extractUpperTypes() + + if (upperTypes.isEmptyIntersection()) { + val isInferredEmptyIntersectionForbidden = + callComponents.languageVersionSettings.supportsFeature(LanguageFeature.ForbidInferringTypeVariablesIntoEmptyIntersection) + + val errorFactory = + if (isInferredEmptyIntersectionForbidden) ::InferredEmptyIntersectionError else ::InferredEmptyIntersectionWarning + addError(errorFactory(upperTypes, variableWithConstraints.typeVariable)) + } + } + } } \ No newline at end of file diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallKind.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallKind.kt index 51565360b75..14d121fa35a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallKind.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinCallKind.kt @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.resolve.calls.components.CheckCallableReference import org.jetbrains.kotlin.resolve.calls.components.CheckContextReceiversResolutionPart import org.jetbrains.kotlin.resolve.calls.components.CheckExplicitReceiverKindConsistency import org.jetbrains.kotlin.resolve.calls.components.CheckExternalArgument +import org.jetbrains.kotlin.resolve.calls.components.CheckIncompatibleTypeVariableUpperBounds import org.jetbrains.kotlin.resolve.calls.components.CheckInfixResolutionPart import org.jetbrains.kotlin.resolve.calls.components.CheckOperatorResolutionPart import org.jetbrains.kotlin.resolve.calls.components.CheckReceivers @@ -38,7 +39,8 @@ enum class KotlinCallKind(vararg resolutionPart: ResolutionPart) { CheckExplicitReceiverKindConsistency, CheckReceivers, PostponedVariablesInitializerResolutionPart, - CheckContextReceiversResolutionPart + CheckContextReceiversResolutionPart, + CheckIncompatibleTypeVariableUpperBounds ), FUNCTION( CheckVisibility, @@ -58,7 +60,8 @@ enum class KotlinCallKind(vararg resolutionPart: ResolutionPart) { CompatibilityOfTypeVariableAsIntersectionTypePart, CompatibilityOfPartiallyApplicableSamConversion, PostponedVariablesInitializerResolutionPart, - CheckContextReceiversResolutionPart + CheckContextReceiversResolutionPart, + CheckIncompatibleTypeVariableUpperBounds ), INVOKE(*FUNCTION.resolutionSequence.toTypedArray()), CALLABLE_REFERENCE( @@ -69,7 +72,8 @@ enum class KotlinCallKind(vararg resolutionPart: ResolutionPart) { CollectionTypeVariableUsagesInfo, CheckReceivers, CheckCallableReference, - CompatibilityOfTypeVariableAsIntersectionTypePart + CompatibilityOfTypeVariableAsIntersectionTypePart, + CheckIncompatibleTypeVariableUpperBounds ), UNSUPPORTED();