From 24dcad0d9c7f2b451fc097b7d7d37170244a63d2 Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Wed, 31 Aug 2022 19:58:51 +0300 Subject: [PATCH] [MPP] Use module's type checker for upper bound checks The default type checker doesn't have a correct type refinement setup. This can cause false positives in subtyping of upper bounds in edge cases with expect type arguments. See the tests in the intellij repo. KTIJ-22295 --- .../resolve/jvm/checkers/WarningAwareUpperBoundChecker.kt | 5 ++++- .../src/org/jetbrains/kotlin/resolve/UpperBoundChecker.kt | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/WarningAwareUpperBoundChecker.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/WarningAwareUpperBoundChecker.kt index 9eeccd4f89a..70f7ebd8fc7 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/WarningAwareUpperBoundChecker.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/checkers/WarningAwareUpperBoundChecker.kt @@ -19,10 +19,13 @@ import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.UPPER_BOUND_VIOLAT import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION_BASED_ON_JAVA_ANNOTATIONS import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeSubstitutor +import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.getEnhancementDeeply // TODO: remove this checker after removing support LV < 1.6 -class WarningAwareUpperBoundChecker : UpperBoundChecker() { +class WarningAwareUpperBoundChecker( + typeChecker: KotlinTypeChecker, +) : UpperBoundChecker(typeChecker) { override fun checkBoundsOfExpandedTypeAlias(type: KotlinType, expression: KtExpression, trace: BindingTrace) { val typeParameters = type.constructor.parameters diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/UpperBoundChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/UpperBoundChecker.kt index 5687c996ead..eee22f429c9 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/UpperBoundChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/UpperBoundChecker.kt @@ -23,7 +23,9 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters @DefaultImplementation(impl = UpperBoundChecker::class) -open class UpperBoundChecker { +open class UpperBoundChecker( + private val typeChecker: KotlinTypeChecker, +) { open fun checkBoundsOfExpandedTypeAlias(type: KotlinType, expression: KtExpression, trace: BindingTrace) { // do nothing in the strict mode as the errors are already reported in the type inference if necessary } @@ -141,7 +143,7 @@ open class UpperBoundChecker { ): Boolean { val substitutedBound = substitutor.safeSubstitute(bound, Variance.INVARIANT) - if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(argumentType, substitutedBound)) { + if (!typeChecker.isSubtypeOf(argumentType, substitutedBound)) { if (argumentReference != null) { upperBoundViolatedReporter.report(argumentReference, substitutedBound) } else if (typeAliasUsageElement != null && !substitutedBound.containsTypeAliasParameters() && !argumentType.containsTypeAliasParameters()) {