diff --git a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt index d14b90949ba..146e7a9a3d3 100644 --- a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt +++ b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/K2JsIrCompiler.kt @@ -449,7 +449,7 @@ class K2JsIrCompiler : CLICompiler() { moduleSourceFiles, environmentForJS.configuration, sourceModule.jsFrontEndResult.jsAnalysisResult, - sortDependencies(sourceModule.descriptors), + sortDependencies(sourceModule.moduleDependencies), icData, expectDescriptorToSymbol, IrFactoryImpl, diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/JsIrLinkerLoader.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/JsIrLinkerLoader.kt index c14520bc13b..42d191f69bb 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/JsIrLinkerLoader.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/ic/JsIrLinkerLoader.kt @@ -68,7 +68,11 @@ internal class JsIrLinkerLoader( var runtimeModule: ModuleDescriptorImpl? = null // TODO: deduplicate this code using part from klib.kt - fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl = descriptors.getOrPut(current) { + fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl { + if (current in descriptors) { + return descriptors.getValue(current) + } + val isBuiltIns = current.unresolvedDependencies.isEmpty() val lookupTracker = LookupTracker.DO_NOTHING @@ -82,12 +86,15 @@ internal class JsIrLinkerLoader( ) if (isBuiltIns) runtimeModule = md - val dependencies = dependencyGraph[current]!!.map { getModuleDescriptor(it) } - md.setDependencies(listOf(md) + dependencies) - md + descriptors[current] = md + return md } - return dependencyGraph.keys.associateBy { klib -> getModuleDescriptor(klib) } + val moduleDescriptorToKotlinLibrary = dependencyGraph.keys.associateBy { klib -> getModuleDescriptor(klib) } + return moduleDescriptorToKotlinLibrary + .onEach { (key, _) -> key.setDependencies(moduleDescriptorToKotlinLibrary.keys.toList()) } + .map> { it.key to it.value } + .toMap() } data class LoadedJsIr(val linker: JsIrLinker, val loadedFragments: Map) diff --git a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt index b3ef6194adb..ee8c41b171b 100644 --- a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt +++ b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt @@ -159,12 +159,9 @@ data class IrModuleInfo( val moduleFragmentToUniqueName: Map, ) -fun sortDependencies(mapping: Map): Collection { - val m2l = mapping.map { it.value to it.key }.toMap() - - return DFS.topologicalOrder(mapping.keys) { m -> - val descriptor = mapping[m] ?: error("No descriptor found for library ${m.libraryName}") - descriptor.allDependencyModules.filter { it != descriptor }.map { m2l[it] } +fun sortDependencies(moduleDependencies: Map>): Collection { + return DFS.topologicalOrder(moduleDependencies.keys) { m -> + moduleDependencies.getValue(m) }.reversed() } @@ -225,7 +222,7 @@ fun loadIr( project, configuration, mainModule.files, - sortDependencies(depsDescriptors.descriptors), + sortDependencies(depsDescriptors.moduleDependencies), friendModules, symbolTable, messageLogger, @@ -238,7 +235,7 @@ fun loadIr( val mainModuleLib = allDependencies.find { it.libraryFile.canonicalPath == mainPath } ?: error("No module with ${mainModule.libPath} found") val moduleDescriptor = depsDescriptors.getModuleDescriptor(mainModuleLib) - val sortedDependencies = sortDependencies(depsDescriptors.descriptors) + val sortedDependencies = sortDependencies(depsDescriptors.moduleDependencies) val friendModules = mapOf(mainModuleLib.uniqueName to depsDescriptors.friendDependencies.map { it.library.uniqueName }) return getIrModuleInfoForKlib( @@ -520,7 +517,7 @@ class ModulesStructure( files, project, compilerConfiguration, - allDependencies.map { getModuleDescriptor(it.library) }, + allModuleDescriptors, friendDependencies.map { getModuleDescriptor(it.library) }, analyzer.targetEnvironment, thisIsBuiltInsModule = builtInModuleDescriptor == null, @@ -556,7 +553,21 @@ class ModulesStructure( // TODO: these are roughly equivalent to KlibResolvedModuleDescriptorsFactoryImpl. Refactor me. val descriptors = mutableMapOf() - fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl = descriptors.getOrPut(current) { + val allModuleDescriptors = run { + val descriptors = allDependencies.map { getModuleDescriptor(it.library) } + + descriptors.forEach { descriptor -> + descriptor.setDependencies(descriptors) + } + + descriptors + } + + fun getModuleDescriptor(current: KotlinLibrary): ModuleDescriptorImpl { + if (current in descriptors) { + return descriptors.getValue(current) + } + val isBuiltIns = current.unresolvedDependencies.isEmpty() val lookupTracker = compilerConfiguration[CommonConfigurationKeys.LOOKUP_TRACKER] ?: LookupTracker.DO_NOTHING @@ -570,10 +581,9 @@ class ModulesStructure( ) if (isBuiltIns) runtimeModule = md - val dependencies = moduleDependencies.getValue(current).map { getModuleDescriptor(it) } - md.setDependencies(listOf(md) + dependencies) + descriptors[current] = md - md + return md } val builtInModuleDescriptor = diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt index 9e22867657a..88df84d9e19 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/ClassicFrontend2IrConverter.kt @@ -84,12 +84,13 @@ class ClassicFrontend2IrConverter( val sourceFiles = psiFiles.values.toList() val icData = configuration.incrementalDataProvider?.getSerializedData(sourceFiles) ?: emptyList() val expectDescriptorToSymbol = mutableMapOf() + val (moduleFragment, pluginContext) = generateIrForKlibSerialization( project, sourceFiles, configuration, analysisResult, - sortDependencies(JsEnvironmentConfigurator.getAllRecursiveLibrariesFor(module, testServices)), + sortDependencies(JsEnvironmentConfigurator.getAllDependenciesMappingFor(module, testServices)), icData, expectDescriptorToSymbol, IrFactoryImpl, diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt index 8778a494148..81979e4ade5 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/services/configuration/JsEnvironmentConfigurator.kt @@ -213,6 +213,16 @@ class JsEnvironmentConfigurator(testServices: TestServices) : EnvironmentConfigu return dependencies.associateBy { testServices.jsLibraryProvider.getCompiledLibraryByDescriptor(it) } } + fun getAllDependenciesMappingFor(module: TestModule, testServices: TestServices): Map> { + val allRecursiveLibraries: Map = getAllRecursiveLibrariesFor(module, testServices) + val m2l = allRecursiveLibraries.map { it.value to it.key }.toMap() + + return allRecursiveLibraries.keys.associateWith { m -> + val descriptor = allRecursiveLibraries[m] ?: error("No descriptor found for library ${m.libraryName}") + descriptor.allDependencyModules.filter { it != descriptor }.map { m2l.getValue(it) } + } + } + fun TestModule.hasFilesToRecompile(): Boolean { return files.any { JsEnvironmentConfigurationDirectives.RECOMPILE in it.directives } } diff --git a/compiler/tests/org/jetbrains/kotlin/klib/FilePathsInKlibTest.kt b/compiler/tests/org/jetbrains/kotlin/klib/FilePathsInKlibTest.kt index e9fa8725ee5..92233bcd245 100644 --- a/compiler/tests/org/jetbrains/kotlin/klib/FilePathsInKlibTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/klib/FilePathsInKlibTest.kt @@ -67,7 +67,7 @@ class FilePathsInKlibTest : CodegenTestCase() { sourceFiles, module.compilerConfiguration, module.jsFrontEndResult.jsAnalysisResult, - sortDependencies(module.descriptors), + sortDependencies(module.moduleDependencies), icData, expectDescriptorToSymbol, IrFactoryImpl, diff --git a/js/js.tests/test/org/jetbrains/kotlin/incremental/AbstractInvalidationTest.kt b/js/js.tests/test/org/jetbrains/kotlin/incremental/AbstractInvalidationTest.kt index afb3cf310b4..50029c3a6c0 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/incremental/AbstractInvalidationTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/incremental/AbstractInvalidationTest.kt @@ -387,7 +387,7 @@ abstract class AbstractInvalidationTest( moduleSourceFiles, configuration, sourceModule.jsFrontEndResult.jsAnalysisResult, - sortDependencies(sourceModule.descriptors), + sortDependencies(sourceModule.moduleDependencies), icData, expectDescriptorToSymbol, IrFactoryImpl, diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/converters/JsIrBackendFacade.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/converters/JsIrBackendFacade.kt index 1c819982052..5954b35af85 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/converters/JsIrBackendFacade.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/converters/JsIrBackendFacade.kt @@ -208,7 +208,7 @@ class JsIrBackendFacade( return getIrModuleInfoForKlib( moduleDescriptor, - sortDependencies(JsEnvironmentConfigurator.getAllRecursiveLibrariesFor(module, testServices)) + mainModuleLib, + sortDependencies(JsEnvironmentConfigurator.getAllDependenciesMappingFor(module, testServices)) + mainModuleLib, friendModules, filesToLoad, configuration, diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/AbstractJsKLibABITestCase.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/AbstractJsKLibABITestCase.kt index 2f4ab36eeeb..ff5597f96f4 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/AbstractJsKLibABITestCase.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/AbstractJsKLibABITestCase.kt @@ -130,7 +130,7 @@ abstract class AbstractJsKLibABITestCase : KtUsefulTestCase() { moduleSourceFiles, config, sourceModule.jsFrontEndResult.jsAnalysisResult, - sortDependencies(sourceModule.descriptors), + sortDependencies(sourceModule.moduleDependencies), icData, expectDescriptorToSymbol, IrFactoryImpl,