From a9583e4f9ac9f9c0a55a7de53a1ed83dabe93271 Mon Sep 17 00:00:00 2001 From: Nikita Bobko Date: Wed, 25 Oct 2023 20:43:23 +0200 Subject: [PATCH] [FIR, IR] 1/2 Align FirExpectActualMatchingContextImpl.areCompatibleExpectActualTypes with ExpectActualUtilsKt.areCompatibleExpectActualTypes Those two functions are copy-paste of each other. They diverged since the time they were copy-pasted. This commit makes FirExpectActualMatchingContextImpl.areCompatibleExpectActualTypes up-to-date. I didn't update ExpectActualUtilsKt.areCompatibleExpectActualTypes because I will drop it in the next commit. Change in `dynamicTypesEqualToAnything` doesn't change any logic (yet. This change in logic will take effect in the next commit), because: 1. `dynamicTypesEqualToAnything` is only changed in AbstractExpectActualChecker.getCallablesCheckingIncompatibility. But AbstractExpectActualChecker.getCallablesCheckingIncompatibility doesn't check return types on frontend (it only check return types on backend). 2. `dynamicTypesEqualToAnything` is ignored on IR backend I have no idea what is the difference between `createTypeCheckerState()` and `actualSession.typeContext`, but it aligns these copy-pasted versions and makes the tests behave like in K1 (at least 'typeUsageWithUnresolvedReference' and 'kt57320' are affected) This commit is mainly a preparation for the next commit. Review: https://jetbrains.team/p/kt/reviews/12750/timeline --- .../jetbrains/kotlin/fir/types/ExpectActualUtils.kt | 1 + .../mpp/FirExpectActualMatchingContextImpl.kt | 13 +++++++++++-- .../actualizer/IrExpectActualMatchingContext.kt | 1 + .../calls/mpp/AbstractExpectActualChecker.kt | 3 ++- .../calls/mpp/ExpectActualMatchingContext.kt | 1 + .../typeUsageWithUnresolvedReference.fir.kt | 2 +- 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ExpectActualUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ExpectActualUtils.kt index 983813126b6..39c724cc2e3 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ExpectActualUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ExpectActualUtils.kt @@ -30,6 +30,7 @@ fun createExpectActualTypeParameterSubstitutor( return substitutor.chain(parentSubstitutor) } +// Copy-pasted to org.jetbrains.kotlin.fir.resolve.transformers.mpp.FirExpectActualMatchingContextImpl.areCompatibleExpectActualTypes fun areCompatibleExpectActualTypes( expectedType: ConeKotlinType?, actualType: ConeKotlinType?, diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/mpp/FirExpectActualMatchingContextImpl.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/mpp/FirExpectActualMatchingContextImpl.kt index 663fe774a35..155505a99b9 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/mpp/FirExpectActualMatchingContextImpl.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/mpp/FirExpectActualMatchingContextImpl.kt @@ -28,7 +28,6 @@ import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualCollectionArgumentsCom import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualMatchingContext import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualMatchingContext.AnnotationCallInfo import org.jetbrains.kotlin.resolve.checkers.OptInNames -import org.jetbrains.kotlin.resolve.multiplatform.ExpectActualCompatibility import org.jetbrains.kotlin.resolve.multiplatform.ExpectActualMatchingCompatibility import org.jetbrains.kotlin.types.AbstractTypeChecker import org.jetbrains.kotlin.types.TypeCheckerState @@ -304,14 +303,24 @@ class FirExpectActualMatchingContextImpl private constructor( override val TypeParameterSymbolMarker.isReified: Boolean get() = asSymbol().isReified + // Copy-pasted to org.jetbrains.kotlin.fir.types.ExpectActualUtilsKt.areCompatibleExpectActualTypes override fun areCompatibleExpectActualTypes( expectType: KotlinTypeMarker?, actualType: KotlinTypeMarker?, parameterOfAnnotationComparisonMode: Boolean, + dynamicTypesEqualToAnything: Boolean ): Boolean { if (expectType == null) return actualType == null if (actualType == null) return false + if (!dynamicTypesEqualToAnything) { + val isExpectedDynamic = expectType is ConeDynamicType + val isActualDynamic = actualType is ConeDynamicType + if (isExpectedDynamic && !isActualDynamic || !isExpectedDynamic && isActualDynamic) { + return false + } + } + if (parameterOfAnnotationComparisonMode && expectType is ConeClassLikeType && expectType.isArrayType && actualType is ConeClassLikeType && actualType.isArrayType ) { @@ -323,7 +332,7 @@ class FirExpectActualMatchingContextImpl private constructor( } return AbstractTypeChecker.equalTypes( - createTypeCheckerState(), + actualSession.typeContext, expectType, actualType ) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/actualizer/IrExpectActualMatchingContext.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/actualizer/IrExpectActualMatchingContext.kt index c2d685bfc80..b24661c1766 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/actualizer/IrExpectActualMatchingContext.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/actualizer/IrExpectActualMatchingContext.kt @@ -338,6 +338,7 @@ internal abstract class IrExpectActualMatchingContext( expectType: KotlinTypeMarker?, actualType: KotlinTypeMarker?, parameterOfAnnotationComparisonMode: Boolean, + dynamicTypesEqualToAnything: Boolean ): Boolean { if (expectType == null) return actualType == null if (actualType == null) return false diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/AbstractExpectActualChecker.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/AbstractExpectActualChecker.kt index e420b30ca4f..a4dfe0d2b6c 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/AbstractExpectActualChecker.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/AbstractExpectActualChecker.kt @@ -372,7 +372,8 @@ object AbstractExpectActualChecker { if (!areCompatibleExpectActualTypes( substitutor.safeSubstitute(expectDeclaration.returnType), actualDeclaration.returnType, - parameterOfAnnotationComparisonMode = insideAnnotationClass + parameterOfAnnotationComparisonMode = insideAnnotationClass, + dynamicTypesEqualToAnything = false ) ) { return ExpectActualCheckingCompatibility.ReturnType diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/ExpectActualMatchingContext.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/ExpectActualMatchingContext.kt index dca5f5d5f73..7e542266071 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/ExpectActualMatchingContext.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/ExpectActualMatchingContext.kt @@ -147,6 +147,7 @@ interface ExpectActualMatchingContext : TypeSystemC expectType: KotlinTypeMarker?, actualType: KotlinTypeMarker?, parameterOfAnnotationComparisonMode: Boolean = false, + dynamicTypesEqualToAnything: Boolean = true ): Boolean fun actualTypeIsSubtypeOfExpectType( diff --git a/compiler/testData/diagnostics/tests/multiplatform/annotationMatching/typeUsageWithUnresolvedReference.fir.kt b/compiler/testData/diagnostics/tests/multiplatform/annotationMatching/typeUsageWithUnresolvedReference.fir.kt index 99ec2d04be2..323478de54e 100644 --- a/compiler/testData/diagnostics/tests/multiplatform/annotationMatching/typeUsageWithUnresolvedReference.fir.kt +++ b/compiler/testData/diagnostics/tests/multiplatform/annotationMatching/typeUsageWithUnresolvedReference.fir.kt @@ -7,4 +7,4 @@ annotation class Ann // MODULE: m1-jvm()()(m1-common) // FILE: jvm.kt -actual fun Unresolved> foo() {} +actual fun Unresolved> foo() {}