From 4e2107abe75730940f2257a97ace8c1ea1332873 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Tue, 18 Apr 2023 14:21:57 +0200 Subject: [PATCH] K2: Fix false positive NotImplemented error for `size` property The problem appeared because not all of the `realOverridden` have been collected because inside AbstractSerializableListDecorator some of the scopes returned the same instance as direct overridden and after that overridden tree traversal stopped without detecting real overrides. Thus, the modality of intersection for AbstractSerializableListDecorator::size and MutableSet::size was incorrectly computed to ABSTRACT The similar thing is already done at the place where we're obtaining all overrides. See https://github.com/JetBrains/kotlin/commit/c80cfb0fdb00323ba9b5e1dd98c5cbd0bfab6b8b#diff-182d90c9b8050557e4e2eb319a84b9a51fd0600c728dd0fce85cf6491c13e16dR152 ^KT-57693 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 6 ++ ...sticCompilerFE10TestDataTestGenerated.java | 6 ++ ...eeOldFrontendDiagnosticsTestGenerated.java | 6 ++ ...siOldFrontendDiagnosticsTestGenerated.java | 6 ++ .../impl/FirTypeIntersectionScopeContext.kt | 7 +- .../inheritanceThroughEmptyClass.kt | 84 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++ 7 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index 92260dc60d0..7e0a8cc536b 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -20319,6 +20319,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/hashtableInheritance.kt"); } + @Test + @TestMetadata("inheritanceThroughEmptyClass.kt") + public void testInheritanceThroughEmptyClass() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt"); + } + @Test @TestMetadata("securityProvider.kt") public void testSecurityProvider() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index 4ed44994991..32022683bcd 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -20319,6 +20319,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/hashtableInheritance.kt"); } + @Test + @TestMetadata("inheritanceThroughEmptyClass.kt") + public void testInheritanceThroughEmptyClass() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt"); + } + @Test @TestMetadata("securityProvider.kt") public void testSecurityProvider() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index 2d4c4fa0f66..bdbf039ce28 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -20319,6 +20319,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/hashtableInheritance.kt"); } + @Test + @TestMetadata("inheritanceThroughEmptyClass.kt") + public void testInheritanceThroughEmptyClass() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt"); + } + @Test @TestMetadata("securityProvider.kt") public void testSecurityProvider() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index 9d2c987d5f5..a08cec491b6 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -20325,6 +20325,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/hashtableInheritance.kt"); } + @Test + @TestMetadata("inheritanceThroughEmptyClass.kt") + public void testInheritanceThroughEmptyClass() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt"); + } + @Test @TestMetadata("securityProvider.kt") public void testSecurityProvider() throws Exception { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirTypeIntersectionScopeContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirTypeIntersectionScopeContext.kt index f02f26196aa..3e3f02c02b5 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirTypeIntersectionScopeContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirTypeIntersectionScopeContext.kt @@ -10,7 +10,6 @@ import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.fir.* import org.jetbrains.kotlin.fir.caches.* -import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration import org.jetbrains.kotlin.fir.declarations.FirVariable @@ -310,10 +309,12 @@ class FirTypeIntersectionScopeContext( symbol: D, scope: FirTypeScope, result: MutableCollection>, - visited: MutableSet, + // There's no guarantee that directOverridden(symbol) is strictly different + // It might be the same instance that when being requested with a different/new scope would return next level of overridden + visited: MutableSet>, processDirectOverridden: FirTypeScope.(D, (D, FirTypeScope) -> ProcessorAction) -> ProcessorAction, ) { - if (!visited.add(symbol)) return + if (!visited.add(scope to symbol)) return if (!symbol.fir.origin.fromSupertypes) { result.add(MemberWithBaseScope(symbol, scope)) return diff --git a/compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt b/compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt new file mode 100644 index 00000000000..78aaa4eb096 --- /dev/null +++ b/compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt @@ -0,0 +1,84 @@ +// FIR_IDENTICAL +// FULL_JDK +// ISSUE: KT-57693 + +// FILE: AbstractCollectionDecorator.java +import java.util.Collection; +import java.util.Iterator; +import java.util.function.Predicate; + +public abstract class AbstractCollectionDecorator implements Collection { + + protected Collection decorated() { + return null; + } + + public boolean add(E object) { + return this.decorated().add(object); + } + + public boolean addAll(Collection coll) { + return this.decorated().addAll(coll); + } + + public void clear() { + this.decorated().clear(); + } + + public boolean contains(Object object) { + return this.decorated().contains(object); + } + + public boolean isEmpty() { + return this.decorated().isEmpty(); + } + + public Iterator iterator() { + return this.decorated().iterator(); + } + + public boolean remove(Object object) { + return this.decorated().remove(object); + } + + public int size() { + return this.decorated().size(); + } + + public Object[] toArray() { + return this.decorated().toArray(); + } + + public T[] toArray(T[] object) { + return this.decorated().toArray(object); + } + + public boolean containsAll(Collection coll) { + return this.decorated().containsAll(coll); + } + + public boolean removeIf(Predicate filter) { + return this.decorated().removeIf(filter); + } + + public boolean removeAll(Collection coll) { + return this.decorated().removeAll(coll); + } + + public boolean retainAll(Collection coll) { + return this.decorated().retainAll(coll); + } + + public String toString() { + return this.decorated().toString(); + } +} + +// FILE: AbstractSerializableListDecorator.java +public abstract class AbstractSerializableListDecorator extends AbstractCollectionDecorator { +} + +// FILE: main.kt +import java.util.* + +class UniqueArrayList : AbstractSerializableListDecorator(), MutableSet 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 1ce5e3dc603..3058aa73935 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 @@ -20325,6 +20325,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/hashtableInheritance.kt"); } + @Test + @TestMetadata("inheritanceThroughEmptyClass.kt") + public void testInheritanceThroughEmptyClass() throws Exception { + runTest("compiler/testData/diagnostics/tests/j+k/specialBuiltIns/inheritanceThroughEmptyClass.kt"); + } + @Test @TestMetadata("securityProvider.kt") public void testSecurityProvider() throws Exception {