[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
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 9eeccd4..70f7ebd 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_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 5687c99..eee22f4 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.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 @@
): 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()) {