From 194d1cfccf6c0404ba83eb166eb41225cb06655a Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Fri, 21 Oct 2022 22:17:52 +0200 Subject: [PATCH] [FIR] Filter out incorrect library provides for dependencyProviders Get rid of providers duplication --- ...CompilerTestFE10TestdataTestGenerated.java | 18 ++++++++ ...irOldFrontendDiagnosticsTestGenerated.java | 18 ++++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 18 ++++++++ .../impl/FirDependenciesSymbolProviderImpl.kt | 42 ++++++++++++++++--- ...arImportOfExpectEnumWithActualTypeAlias.kt | 3 ++ .../tests/noLibraryProvidersDuplication.kt | 15 +++++++ .../tests/noLibraryProvidersDuplication.txt | 8 ++++ .../noLibraryProvidersDuplicationWithMpp.kt | 18 ++++++++ .../noLibraryProvidersDuplicationWithMpp.txt | 12 ++++++ .../noSymbolProvidersDuplicationInDiamond.kt | 19 +++++++++ .../noSymbolProvidersDuplicationInDiamond.txt | 19 +++++++++ .../test/runners/DiagnosticTestGenerated.java | 18 ++++++++ 12 files changed, 202 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.kt create mode 100644 compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.txt create mode 100644 compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.kt create mode 100644 compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.txt create mode 100644 compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt create mode 100644 compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.txt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 0f4c15a5379..11802f5456f 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -495,6 +495,24 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/NamedFunctionTypeParameterInSupertype.kt"); } + @Test + @TestMetadata("noLibraryProvidersDuplication.kt") + public void testNoLibraryProvidersDuplication() throws Exception { + runTest("compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.kt"); + } + + @Test + @TestMetadata("noLibraryProvidersDuplicationWithMpp.kt") + public void testNoLibraryProvidersDuplicationWithMpp() throws Exception { + runTest("compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.kt"); + } + + @Test + @TestMetadata("noSymbolProvidersDuplicationInDiamond.kt") + public void testNoSymbolProvidersDuplicationInDiamond() throws Exception { + runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt"); + } + @Test @TestMetadata("Nullability.kt") public void testNullability() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index 9f45fea4ea6..7f818c757e7 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -495,6 +495,24 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/NamedFunctionTypeParameterInSupertype.kt"); } + @Test + @TestMetadata("noLibraryProvidersDuplication.kt") + public void testNoLibraryProvidersDuplication() throws Exception { + runTest("compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.kt"); + } + + @Test + @TestMetadata("noLibraryProvidersDuplicationWithMpp.kt") + public void testNoLibraryProvidersDuplicationWithMpp() throws Exception { + runTest("compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.kt"); + } + + @Test + @TestMetadata("noSymbolProvidersDuplicationInDiamond.kt") + public void testNoSymbolProvidersDuplicationInDiamond() throws Exception { + runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt"); + } + @Test @TestMetadata("Nullability.kt") public void testNullability() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index 18db5eabfc7..f1617953378 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -495,6 +495,24 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/NamedFunctionTypeParameterInSupertype.kt"); } + @Test + @TestMetadata("noLibraryProvidersDuplication.kt") + public void testNoLibraryProvidersDuplication() throws Exception { + runTest("compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.kt"); + } + + @Test + @TestMetadata("noLibraryProvidersDuplicationWithMpp.kt") + public void testNoLibraryProvidersDuplicationWithMpp() throws Exception { + runTest("compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.kt"); + } + + @Test + @TestMetadata("noSymbolProvidersDuplicationInDiamond.kt") + public void testNoSymbolProvidersDuplicationInDiamond() throws Exception { + runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt"); + } + @Test @TestMetadata("Nullability.kt") public void testNullability() throws Exception { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirDependenciesSymbolProviderImpl.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirDependenciesSymbolProviderImpl.kt index cdea686381c..9f8d9ce004e 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirDependenciesSymbolProviderImpl.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirDependenciesSymbolProviderImpl.kt @@ -33,16 +33,46 @@ open class FirDependenciesSymbolProviderImpl(session: FirSession) : FirDependenc protected open val dependencyProviders by lazy { val moduleData = session.nullableModuleData ?: error("FirDependenciesSymbolProvider should not be created if there are no dependencies") - val result = (moduleData.dependencies + moduleData.friendDependencies + moduleData.dependsOnDependencies) + val visited = mutableSetOf() + (moduleData.dependencies + moduleData.friendDependencies + moduleData.dependsOnDependencies) .mapNotNull { session.sessionProvider?.getSession(it) } - .sortedBy { it.kind } .map { it.symbolProvider } - result.flatMap { - when (it) { - is FirCompositeSymbolProvider -> it.providers - else -> listOf(it) + .flatMap { it.flatten(visited, collectSourceProviders = it.session.kind == FirSession.Kind.Source) } + .sortedBy { it.session.kind } + } + + /* It eliminates dependency and composite providers since the current dependency provider is composite in fact. + * To prevent duplications and resolving errors, library or source providers from other modules should be filtered out during flattening. + * It depends on the session's kind of the top-level provider */ + private fun FirSymbolProvider.flatten( + visited: MutableSet, + collectSourceProviders: Boolean + ): List { + val result = mutableListOf() + + fun FirSymbolProvider.collectProviders() { + if (!visited.add(this)) return + when { + this is FirDependenciesSymbolProviderImpl -> { + for (provider in dependencyProviders) { + provider.collectProviders() + } + } + this is FirCompositeSymbolProvider -> { + for (provider in providers) { + provider.collectProviders() + } + } + collectSourceProviders && session.kind == FirSession.Kind.Source || + !collectSourceProviders && session.kind == FirSession.Kind.Library -> { + result.add(this) + } } } + + collectProviders() + + return result } @OptIn(FirSymbolProviderInternals::class) diff --git a/compiler/testData/codegen/box/multiplatform/starImportOfExpectEnumWithActualTypeAlias.kt b/compiler/testData/codegen/box/multiplatform/starImportOfExpectEnumWithActualTypeAlias.kt index e9d1b2a3bd3..9d7e94ec842 100644 --- a/compiler/testData/codegen/box/multiplatform/starImportOfExpectEnumWithActualTypeAlias.kt +++ b/compiler/testData/codegen/box/multiplatform/starImportOfExpectEnumWithActualTypeAlias.kt @@ -3,6 +3,9 @@ // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // IGNORE_BACKEND: NATIVE +// IGNORE_BACKEND_K2: JVM_IR +// K2 status: caused by: java.lang.IllegalStateException: Should not be here! +// It will be fixed after merging of MPP branch // MODULE: lib-common // FILE: common.kt diff --git a/compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.kt b/compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.kt new file mode 100644 index 00000000000..cb1424350e9 --- /dev/null +++ b/compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.kt @@ -0,0 +1,15 @@ +// FIR_IDENTICAL +// TARGET_BACKEND: JVM +// WITH_STDLIB + +// MODULE: lib + +// MODULE: main()()(lib) + +fun nullIfEmpty(list: List): List? { + return if (list.isNotEmpty()) { + list + } else { + null + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.txt b/compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.txt new file mode 100644 index 00000000000..d46355f64f6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.txt @@ -0,0 +1,8 @@ +// -- Module: -- +package + + +// -- Module:
-- +package + +public fun nullIfEmpty(/*0*/ list: kotlin.collections.List): kotlin.collections.List? diff --git a/compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.kt b/compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.kt new file mode 100644 index 00000000000..593a46c4b0d --- /dev/null +++ b/compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.kt @@ -0,0 +1,18 @@ +// FIR_IDENTICAL +// !LANGUAGE: +MultiPlatformProjects +// TARGET_BACKEND: JVM +// WITH_STDLIB + +// MODULE: lib + +// MODULE: lib2 + +// MODULE: main(lib2)()(lib) + +fun nullIfEmpty(list: List): List? { + return if (list.isNotEmpty()) { + list + } else { + null + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.txt b/compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.txt new file mode 100644 index 00000000000..8b6292db83a --- /dev/null +++ b/compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.txt @@ -0,0 +1,12 @@ +// -- Module: -- +package + + +// -- Module: -- +package + + +// -- Module:
-- +package + +public fun nullIfEmpty(/*0*/ list: kotlin.collections.List): kotlin.collections.List? diff --git a/compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt b/compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt new file mode 100644 index 00000000000..faca9a00b4b --- /dev/null +++ b/compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt @@ -0,0 +1,19 @@ +// FIR_IDENTICAL +// FIR_IDE_IGNORE +// !LANGUAGE: +MultiPlatformProjects +// TARGET_BACKEND: JVM + +// MODULE: common +// TARGET_PLATFORM: Common +expect fun g0(): String + +// MODULE: intermediate1()()(common) +// TARGET_PLATFORM: Common + +// MODULE: intermediate2()()(common) +// TARGET_PLATFORM: Common + +// MODULE: main()()(intermediate1, intermediate2) +// TARGET_PLATFORM: JVM + +actual fun g0(): String = "OK" diff --git a/compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.txt b/compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.txt new file mode 100644 index 00000000000..017749104e1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.txt @@ -0,0 +1,19 @@ +// -- Module: -- +package + +public expect fun g0(): kotlin.String + +// -- Module: -- +package + +public expect fun g0(): kotlin.String + +// -- Module: -- +package + +public expect fun g0(): kotlin.String + +// -- Module:
-- +package + +public actual fun g0(): kotlin.String 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 a5d742726bb..88880fe4b26 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 @@ -495,6 +495,24 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/NamedFunctionTypeParameterInSupertype.kt"); } + @Test + @TestMetadata("noLibraryProvidersDuplication.kt") + public void testNoLibraryProvidersDuplication() throws Exception { + runTest("compiler/testData/diagnostics/tests/noLibraryProvidersDuplication.kt"); + } + + @Test + @TestMetadata("noLibraryProvidersDuplicationWithMpp.kt") + public void testNoLibraryProvidersDuplicationWithMpp() throws Exception { + runTest("compiler/testData/diagnostics/tests/noLibraryProvidersDuplicationWithMpp.kt"); + } + + @Test + @TestMetadata("noSymbolProvidersDuplicationInDiamond.kt") + public void testNoSymbolProvidersDuplicationInDiamond() throws Exception { + runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt"); + } + @Test @TestMetadata("Nullability.kt") public void testNullability() throws Exception {