From 73a8b4544a460000067526257590d77891d55076 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 10 Aug 2021 21:48:47 +0300 Subject: [PATCH] FIR: don't report EQUALITY_NOT_APPLICABLE for classes from java.* Workaround for KT-48113 --- .../intellij/IntersectionWithJavaString.fir.txt | 9 +++++++++ .../intellij/IntersectionWithJavaString.kt | 5 +++++ .../kotlin/test/runners/FirDiagnosticTestGenerated.java | 6 ++++++ .../FirDiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ .../analysis/checkers/ConeTypeCompatibilityChecker.kt | 9 +++++---- .../based/DiagnosisCompilerFirTestdataTestGenerated.java | 6 ++++++ 6 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.fir.txt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.fir.txt new file mode 100644 index 00000000000..8a0ad6c9e71 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.fir.txt @@ -0,0 +1,9 @@ +FILE: IntersectionWithJavaString.kt + public final fun collapse(path: R|kotlin/String|): R|kotlin/Unit| { + lval result: R|ft| = (R|/path| as R|java/lang/String|).R|java/lang/String.replace|(String(123), String(456)) + when () { + !==(R|/result|, R|/path|) -> { + } + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt new file mode 100644 index 00000000000..a395b8e8f6d --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt @@ -0,0 +1,5 @@ +// FULL_JDK +fun collapse(path: String) { + val result = (path as java.lang.String).replace("123", "456") + if (result !== path) {} +} \ No newline at end of file diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 670f5f8ec63..e36200d3bea 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -5045,6 +5045,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/EnumWithToString.kt"); } + @Test + @TestMetadata("IntersectionWithJavaString.kt") + public void testIntersectionWithJavaString() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt"); + } + @Test @TestMetadata("UastPatterns.kt") public void testUastPatterns() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 1287f6bacbe..850b0ca91fb 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -5045,6 +5045,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/EnumWithToString.kt"); } + @Test + @TestMetadata("IntersectionWithJavaString.kt") + public void testIntersectionWithJavaString() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt"); + } + @Test @TestMetadata("UastPatterns.kt") public void testUastPatterns() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/ConeTypeCompatibilityChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/ConeTypeCompatibilityChecker.kt index ddd59aafc5d..90cb46511da 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/ConeTypeCompatibilityChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/ConeTypeCompatibilityChecker.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.Variance @@ -212,10 +213,10 @@ internal object ConeTypeCompatibilityChecker { val classes = classesOrInterfaces.filter { !it.isInterface } // Java force single inheritance, so any pair of unrelated classes are incompatible. if (classes.size >= 2) { - return if (classes.any { it.getHasPredefinedEqualityContract(this) }) { - compatibilityUpperBound - } else { - Compatibility.SOFT_INCOMPATIBLE + return when { + classes.any { it.firClass.classId.packageFqName.startsWith(Name.identifier("java")) } -> Compatibility.SOFT_INCOMPATIBLE + classes.any { it.getHasPredefinedEqualityContract(this) } -> compatibilityUpperBound + else -> Compatibility.SOFT_INCOMPATIBLE } } val finalClass = classes.firstOrNull { it.isFinal } ?: return null diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index a7ee81b3954..c0003db71ee 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -5045,6 +5045,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/EnumWithToString.kt"); } + @Test + @TestMetadata("IntersectionWithJavaString.kt") + public void testIntersectionWithJavaString() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolveWithStdlib/intellij/IntersectionWithJavaString.kt"); + } + @Test @TestMetadata("UastPatterns.kt") public void testUastPatterns() throws Exception {