From 134bec6de80114216f34db8294b7a955efb7ce46 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 14 Apr 2017 19:44:11 +0300 Subject: [PATCH] Move dependency on built-ins in IDE higher for common modules See the comment in ModuleInfo --- .../kotlin/analyzer/AnalyzerFacade.kt | 57 ++++++++++--------- .../analyzer/common/DefaultAnalyzerFacade.kt | 2 +- .../resolve/lazy/lazyResolveTestUtils.kt | 4 +- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/analyzer/AnalyzerFacade.kt b/compiler/frontend/src/org/jetbrains/kotlin/analyzer/AnalyzerFacade.kt index 04770b9af5e..3479bb27aa1 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/analyzer/AnalyzerFacade.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/analyzer/AnalyzerFacade.kt @@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.TargetEnvironment import org.jetbrains.kotlin.resolve.TargetPlatform import org.jetbrains.kotlin.utils.keysToMap import java.util.* +import kotlin.coroutines.experimental.buildSequence class ResolverForModule( val packageFragmentProvider: PackageFragmentProvider, @@ -130,27 +131,22 @@ interface ModuleInfo { fun dependencies(): List val platform: TargetPlatform? get() = null fun modulesWhoseInternalsAreVisible(): Collection = listOf() - fun dependencyOnBuiltIns(): DependencyOnBuiltIns = DependenciesOnBuiltIns.LAST val capabilities: Map, Any?> get() = mapOf(Capability to this) - //TODO: (module refactoring) provide dependency on builtins after runtime in IDEA - interface DependencyOnBuiltIns { - fun adjustDependencies(builtinsModule: ModuleDescriptorImpl, dependencies: MutableList) - } + // For common modules, we add built-ins at the beginning of the dependencies list, after the SDK. + // This is needed because if a JVM module depends on the common module, we should use JVM built-ins for resolution of both modules. + // The common module usually depends on kotlin-stdlib-common which may or may not have its own (common, non-JVM) built-ins, + // but if they are present, they should come after JVM built-ins in the dependencies list, because JVM built-ins contain + // additional members dependent on the JDK + fun dependencyOnBuiltIns(): ModuleInfo.DependencyOnBuiltIns = + if (platform == TargetPlatform.Default) + ModuleInfo.DependencyOnBuiltIns.AFTER_SDK + else + ModuleInfo.DependencyOnBuiltIns.LAST - enum class DependenciesOnBuiltIns : DependencyOnBuiltIns { - NONE { - override fun adjustDependencies(builtinsModule: ModuleDescriptorImpl, dependencies: MutableList) { - //do nothing - } - }, - LAST { - override fun adjustDependencies(builtinsModule: ModuleDescriptorImpl, dependencies: MutableList) { - dependencies.add(builtinsModule) - } - }; - } + //TODO: (module refactoring) provide dependency on builtins after runtime in IDEA + enum class DependencyOnBuiltIns { NONE, AFTER_SDK, LAST } companion object { val Capability = ModuleDescriptor.Capability("ModuleInfo") @@ -180,16 +176,24 @@ abstract class AnalyzerFacade { }, delegateResolver) for (module in modules) { - resolverForProject.descriptorForModule(module).setDependencies(LazyModuleDependencies( + val moduleDescriptor = resolverForProject.descriptorForModule(module) + moduleDescriptor.setDependencies(LazyModuleDependencies( storageManager, computeDependencies = { - val orderedDependencies = listOfNotNull(firstDependency) + module.dependencies() - val dependenciesDescriptors = orderedDependencies.mapTo(ArrayList()) { dependencyInfo -> - resolverForProject.descriptorForModule(dependencyInfo as M) - } - module.dependencyOnBuiltIns().adjustDependencies( - resolverForProject.descriptorForModule(module).builtIns.builtInsModule, dependenciesDescriptors) - dependenciesDescriptors + buildSequence { + if (firstDependency != null) { + yield(resolverForProject.descriptorForModule(firstDependency)) + } + if (module.dependencyOnBuiltIns() == ModuleInfo.DependencyOnBuiltIns.AFTER_SDK) { + yield(moduleDescriptor.builtIns.builtInsModule) + } + for (dependency in module.dependencies()) { + yield(resolverForProject.descriptorForModule(dependency as M)) + } + if (module.dependencyOnBuiltIns() == ModuleInfo.DependencyOnBuiltIns.LAST) { + yield(moduleDescriptor.builtIns.builtInsModule) + } + }.toList() }, computeModulesWhoseInternalsAreVisible = { module.modulesWhoseInternalsAreVisible().mapTo(LinkedHashSet()) { @@ -202,8 +206,7 @@ abstract class AnalyzerFacade { .filter { modulePlatforms(it) != MultiTargetPlatform.Common && module in it.dependencies() } .mapTo(mutableSetOf(), resolverForProject::descriptorForModule) } - ) - ) + )) } for (module in modules) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/analyzer/common/DefaultAnalyzerFacade.kt b/compiler/frontend/src/org/jetbrains/kotlin/analyzer/common/DefaultAnalyzerFacade.kt index 182bcff624e..e7208eed07b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/analyzer/common/DefaultAnalyzerFacade.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/analyzer/common/DefaultAnalyzerFacade.kt @@ -60,7 +60,7 @@ object DefaultAnalyzerFacade : AnalyzerFacade() { override fun dependencies() = listOf(this) override fun dependencyOnBuiltIns(): ModuleInfo.DependencyOnBuiltIns = - if (dependOnOldBuiltIns) ModuleInfo.DependenciesOnBuiltIns.LAST else ModuleInfo.DependenciesOnBuiltIns.NONE + if (dependOnOldBuiltIns) ModuleInfo.DependencyOnBuiltIns.LAST else ModuleInfo.DependencyOnBuiltIns.NONE } fun analyzeFiles( diff --git a/compiler/tests-common/org/jetbrains/kotlin/resolve/lazy/lazyResolveTestUtils.kt b/compiler/tests-common/org/jetbrains/kotlin/resolve/lazy/lazyResolveTestUtils.kt index ee2b00178d4..3052c060fdc 100644 --- a/compiler/tests-common/org/jetbrains/kotlin/resolve/lazy/lazyResolveTestUtils.kt +++ b/compiler/tests-common/org/jetbrains/kotlin/resolve/lazy/lazyResolveTestUtils.kt @@ -51,7 +51,7 @@ private class TestModule(val dependsOnBuiltIns: Boolean) : ModuleInfo { override fun dependencies() = listOf(this) override fun dependencyOnBuiltIns() = if (dependsOnBuiltIns) - ModuleInfo.DependenciesOnBuiltIns.LAST + ModuleInfo.DependencyOnBuiltIns.LAST else - ModuleInfo.DependenciesOnBuiltIns.NONE + ModuleInfo.DependencyOnBuiltIns.NONE }