From 6b217369a886d6117d3f3725d3ba1369efda650e Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Wed, 23 Aug 2023 10:49:22 +0200 Subject: [PATCH] [FE1.0] Fix false reporting of non-matching annotations in IDE... ...when typealiased expect class used in class literal. ^KTIJ-26700 Fixed --- ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++++ ...endMPPDiagnosticsWithPsiTestGenerated.java | 6 +++++ .../ClassicExpectActualMatchingContext.kt | 18 +++++++++------ .../constExpressionValuesEqualityChecker.kt | 14 +++++++++-- .../typealiasedAsKClassArg.kt | 23 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 +++++ 6 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAsKClassArg.kt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated.java index cc0e2022666..772946d2518 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated.java @@ -1419,6 +1419,12 @@ public class FirOldFrontendMPPDiagnosticsWithLightTreeTestGenerated extends Abst public void testTypealiasedAnnotationAsArgument() throws Exception { runTest("compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAnnotationAsArgument.kt"); } + + @Test + @TestMetadata("typealiasedAsKClassArg.kt") + public void testTypealiasedAsKClassArg() throws Exception { + runTest("compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAsKClassArg.kt"); + } } } } diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithPsiTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithPsiTestGenerated.java index 37a0a3c9d85..f415707dee1 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithPsiTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendMPPDiagnosticsWithPsiTestGenerated.java @@ -1419,6 +1419,12 @@ public class FirOldFrontendMPPDiagnosticsWithPsiTestGenerated extends AbstractFi public void testTypealiasedAnnotationAsArgument() throws Exception { runTest("compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAnnotationAsArgument.kt"); } + + @Test + @TestMetadata("typealiasedAsKClassArg.kt") + public void testTypealiasedAsKClassArg() throws Exception { + runTest("compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAsKClassArg.kt"); + } } } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/multiplatform/ClassicExpectActualMatchingContext.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/multiplatform/ClassicExpectActualMatchingContext.kt index 695e7aff7b8..0aad92a7c9a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/multiplatform/ClassicExpectActualMatchingContext.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/multiplatform/ClassicExpectActualMatchingContext.kt @@ -389,13 +389,17 @@ class ClassicExpectActualMatchingContext( return classDescriptor } val classId = classDescriptor.classId - // For IDE composite module analysis, when actual annotation may differ - val platformDescriptor = platformModule.findClassifierAcrossModuleDependencies(classId) - return when (platformDescriptor) { - is ClassDescriptor -> platformDescriptor - is TypeAliasDescriptor -> platformDescriptor.classDescriptor ?: classDescriptor - else -> classDescriptor - } + return findExpandedExpectClassInPlatformModule(classId) ?: classDescriptor + } + } + + // For IDE composite module analysis, when actual class may differ + internal fun findExpandedExpectClassInPlatformModule(originalClassId: ClassId): ClassDescriptor? { + val classifier = platformModule.findClassifierAcrossModuleDependencies(originalClassId) + return when (classifier) { + is TypeAliasDescriptor -> classifier.classDescriptor + is ClassDescriptor -> classifier + else -> null } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/multiplatform/constExpressionValuesEqualityChecker.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/multiplatform/constExpressionValuesEqualityChecker.kt index 60c5ef1e1da..5084e93a57a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/multiplatform/constExpressionValuesEqualityChecker.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/multiplatform/constExpressionValuesEqualityChecker.kt @@ -7,10 +7,10 @@ package org.jetbrains.kotlin.resolve.multiplatform import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualCollectionArgumentsCompatibilityCheckStrategy -import org.jetbrains.kotlin.resolve.calls.mpp.ExpectActualMatchingContext import org.jetbrains.kotlin.resolve.constants.ConstantValue +import org.jetbrains.kotlin.resolve.constants.KClassValue -internal fun ExpectActualMatchingContext<*>.areExpressionConstValuesEqual( +internal fun ClassicExpectActualMatchingContext.areExpressionConstValuesEqual( expectValue: Any?, actualValue: Any?, collectionArgumentsCompatibilityCheckStrategy: ExpectActualCollectionArgumentsCompatibilityCheckStrategy, @@ -36,6 +36,16 @@ internal fun ExpectActualMatchingContext<*>.areExpressionConstValuesEqual( areExpressionConstValuesEqual(f, s, collectionArgumentsCompatibilityCheckStrategy) } } + expectValue is KClassValue.Value.NormalClass && actualValue is KClassValue.Value.NormalClass -> { + val expectClassIdOriginal = expectValue.classId + val expectClassIdPlatform = findExpandedExpectClassInPlatformModule(expectClassIdOriginal)?.classId + val expectValueCopy = expectValue.copy( + value = expectValue.value.copy( + classId = expectClassIdPlatform ?: expectClassIdOriginal + ) + ) + expectValueCopy == actualValue + } else -> expectValue == actualValue } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAsKClassArg.kt b/compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAsKClassArg.kt new file mode 100644 index 00000000000..45592ad981d --- /dev/null +++ b/compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAsKClassArg.kt @@ -0,0 +1,23 @@ +// FIR_IDENTICAL +// WITH_STDLIB +// MODULE: common +expect class Typealiased + +annotation class Ann(val p: kotlin.reflect.KClass<*>) + +@Ann(Typealiased::class) +expect fun test() + +@Ann(Array::class) +expect fun testInArray() + +// MODULE: main()()(common) +class TypealiasedImpl + +actual typealias Typealiased = TypealiasedImpl + +@Ann(Typealiased::class) +actual fun test() {} + +@Ann(Array::class) +actual fun testInArray() {} \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 1cd752d4e8b..d211b75efb3 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -24206,6 +24206,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { public void testTypealiasedAnnotationAsArgument() throws Exception { runTest("compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAnnotationAsArgument.kt"); } + + @Test + @TestMetadata("typealiasedAsKClassArg.kt") + public void testTypealiasedAsKClassArg() throws Exception { + runTest("compiler/testData/diagnostics/tests/multiplatform/hmpp/multiplatformCompositeAnalysis/annotationMatching/typealiasedAsKClassArg.kt"); + } } } }