[REPL] Fix unresolved imports caching

Before this fix, if some imports were not resolved during compilation,
this result had been saved in caches, and this import couldn't been
resolved during following compilations even if it was added to the
module dependencies. This commit adds special handling of resolution
caches for the REPL compiler.
This commit is contained in:
Ilya Muradyan
2021-03-22 14:20:36 +03:00
parent ae5fefce51
commit 14d386223b
12 changed files with 179 additions and 27 deletions
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.context.ModuleContext
import org.jetbrains.kotlin.context.MutableModuleContext
import org.jetbrains.kotlin.context.ProjectContext
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ModuleCapability
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
import org.jetbrains.kotlin.descriptors.impl.CompositePackageFragmentProvider
@@ -143,13 +144,14 @@ object TopDownAnalyzerFacadeForJVM {
klibList: List<KotlinLibrary> = emptyList(),
implicitsResolutionFilter: ImplicitsExtensionsResolutionFilter? = null,
explicitModuleDependencyList: List<ModuleDescriptorImpl> = emptyList(),
explicitModuleFriendsList: List<ModuleDescriptorImpl> = emptyList()
explicitModuleFriendsList: List<ModuleDescriptorImpl> = emptyList(),
moduleCapabilities: Map<ModuleCapability<*>, Any?> = emptyMap()
): ComponentProvider {
val jvmTarget = configuration.get(JVMConfigurationKeys.JVM_TARGET, JvmTarget.DEFAULT)
val languageVersionSettings = configuration.languageVersionSettings
val jvmPlatform = JvmPlatforms.jvmPlatformByTargetVersion(jvmTarget)
val moduleContext = createModuleContext(project, configuration, jvmPlatform)
val moduleContext = createModuleContext(project, configuration, jvmPlatform, moduleCapabilities)
val storageManager = moduleContext.storageManager
val module = moduleContext.module
@@ -318,11 +320,17 @@ object TopDownAnalyzerFacadeForJVM {
}
}
private fun createModuleContext(project: Project, configuration: CompilerConfiguration, platform: TargetPlatform?): MutableModuleContext {
private fun createModuleContext(
project: Project,
configuration: CompilerConfiguration,
platform: TargetPlatform?,
capabilities: Map<ModuleCapability<*>, Any?> = emptyMap()
): MutableModuleContext {
val projectContext = ProjectContext(project, "TopDownAnalyzer for JVM")
val builtIns = JvmBuiltIns(projectContext.storageManager, JvmBuiltIns.Kind.FROM_DEPENDENCIES)
return ContextForNewModule(
projectContext, Name.special("<${configuration.getNotNull(CommonConfigurationKeys.MODULE_NAME)}>"), builtIns, platform
projectContext, Name.special("<${configuration.getNotNull(CommonConfigurationKeys.MODULE_NAME)}>"),
builtIns, platform, capabilities
).apply {
builtIns.builtInsModule = module
}
@@ -20,6 +20,7 @@ import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ModuleCapability
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
@@ -103,8 +104,9 @@ fun ContextForNewModule(
projectContext: ProjectContext,
moduleName: Name,
builtIns: KotlinBuiltIns,
platform: TargetPlatform?
platform: TargetPlatform?,
capabilities: Map<ModuleCapability<*>, Any?> = emptyMap(),
): MutableModuleContext {
val module = ModuleDescriptorImpl(moduleName, projectContext.storageManager, builtIns, platform)
val module = ModuleDescriptorImpl(moduleName, projectContext.storageManager, builtIns, platform, capabilities)
return MutableModuleContextImpl(module, projectContext)
}
@@ -83,6 +83,7 @@ public class KotlinJavaPsiFacade implements Disposable {
}
private volatile PackageCache packageCache;
private volatile NotFoundPackagesCachingStrategy notFoundPackagesCachingStrategy = NotFoundPackagesCachingStrategy.Default.INSTANCE;
private final Project project;
private final LightModifierList emptyModifierList;
@@ -157,6 +158,10 @@ public class KotlinJavaPsiFacade implements Disposable {
}
}
public void setNotFoundPackagesCachingStrategy(NotFoundPackagesCachingStrategy notFoundPackagesCachingStrategy) {
this.notFoundPackagesCachingStrategy = notFoundPackagesCachingStrategy;
}
public LightModifierList getEmptyModifierList() {
return emptyModifierList;
}
@@ -314,6 +319,8 @@ public class KotlinJavaPsiFacade implements Disposable {
}
boolean isALibrarySearchScope = isALibrarySearchScope(searchScope);
NotFoundPackagesCachingStrategy.CacheType notFoundCacheType =
notFoundPackagesCachingStrategy.chooseStrategy(isALibrarySearchScope, qualifiedName);
{
// store found package in a long term cache if package is found in library search scope
@@ -353,24 +360,24 @@ public class KotlinJavaPsiFacade implements Disposable {
}
}
cache.hasPackageInAllScopeCache.put(qualifiedName, found);
if (found || notFoundCacheType != NotFoundPackagesCachingStrategy.CacheType.NO_CACHING)
cache.hasPackageInAllScopeCache.put(qualifiedName, found);
}
}
// qualifiedName could be like a proper package name, e.g `org.jetbrains.kotlin`
// but it could be as well part of typed text like `fooba`
//
// all those temporary names and those don't even look like a package name should be stored in a short term cache
// while names those are potentially proper package name could be stored for a long time
// (till PROJECT_ROOTS or specific VFS changes)
boolean packageLikeQName = qualifiedName.indexOf('.') > 0;
ConcurrentMap<Pair<String, GlobalSearchScope>, PsiPackage> notFoundPackageInScopeCache =
// store NULL_PACKAGE (attribute that package not found) in a long term cache if:
// - library search scope
// - qualifiedName looks like package (has `.` in its name)
isALibrarySearchScope && packageLikeQName ?
cache.packageInLibScopeCache : cache.packageInScopeCache;
ConcurrentMap<Pair<String, GlobalSearchScope>, PsiPackage> notFoundPackageInScopeCache;
switch (notFoundCacheType) {
case LIB_SCOPE:
notFoundPackageInScopeCache = cache.packageInLibScopeCache;
break;
case SCOPE:
notFoundPackageInScopeCache = cache.packageInScopeCache;
break;
case NO_CACHING:
return null;
default:
throw new IllegalStateException("Impossible enum value: " + notFoundCacheType.toString());
}
return unwrap(ConcurrencyUtil.cacheOrGet(notFoundPackageInScopeCache, key, NULL_PACKAGE));
}
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.jvm
interface NotFoundPackagesCachingStrategy {
fun chooseStrategy(isLibrarySearchScope: Boolean, qualifiedName: String): CacheType
enum class CacheType {
LIB_SCOPE, SCOPE, NO_CACHING
}
object Default : NotFoundPackagesCachingStrategy {
override fun chooseStrategy(isLibrarySearchScope: Boolean, qualifiedName: String): CacheType {
// qualifiedName could be like a proper package name, e.g `org.jetbrains.kotlin`
// but it could be as well part of typed text like `fooba`
//
// all those temporary names and those don't even look like a package name should be stored in a short term cache
// while names those are potentially proper package name could be stored for a long time
// (till PROJECT_ROOTS or specific VFS changes)
val packageLikeQName = qualifiedName.indexOf('.') > 0
return if (isLibrarySearchScope && packageLikeQName) CacheType.LIB_SCOPE
else CacheType.SCOPE
}
}
}