[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
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.calls.tower.ImplicitsExtensionsResolutionFilter
import org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade
import org.jetbrains.kotlin.scripting.compiler.plugin.repl.*
import org.jetbrains.kotlin.scripting.definitions.ScriptDependenciesProvider
import org.jetbrains.kotlin.scripting.resolve.skipExtensionsResolutionForImplicits
@@ -333,8 +334,10 @@ class ReplCompilationState<AnalyzerT : ReplCodeAnalyzerBase>(
override val baseScriptCompilationConfiguration: ScriptCompilationConfiguration get() = context.baseScriptCompilationConfiguration
override val environment: KotlinCoreEnvironment get() = context.environment
override val analyzerEngine: AnalyzerT by lazy {
// ReplCodeAnalyzer1(context.environment)
analyzerInit(context, implicitsResolutionFilter)
val analyzer = analyzerInit(context, implicitsResolutionFilter)
val psiFacade = KotlinJavaPsiFacade.getInstance(environment.project)
psiFacade.setNotFoundPackagesCachingStrategy(ReplNotFoundPackagesCachingStrategy)
analyzer
}
private val manglerAndSymbolTable by lazy {
@@ -0,0 +1,14 @@
/*
* 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.scripting.compiler.plugin.impl
import org.jetbrains.kotlin.resolve.jvm.NotFoundPackagesCachingStrategy
object ReplNotFoundPackagesCachingStrategy : NotFoundPackagesCachingStrategy {
override fun chooseStrategy(isLibrarySearchScope: Boolean, qualifiedName: String): NotFoundPackagesCachingStrategy.CacheType {
return NotFoundPackagesCachingStrategy.CacheType.NO_CACHING
}
}
@@ -0,0 +1,37 @@
/*
* 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.scripting.compiler.plugin.impl
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
import org.jetbrains.kotlin.descriptors.impl.LazyPackageViewDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.PackageViewDescriptorFactory
import org.jetbrains.kotlin.descriptors.packageFragments
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.storage.StorageManager
object ReplPackageViewDescriptorFactory : PackageViewDescriptorFactory {
override fun compute(module: ModuleDescriptorImpl, fqName: FqName, storageManager: StorageManager): PackageViewDescriptor {
return ReplPackageViewDescriptor(module, fqName, storageManager)
}
}
class ReplPackageViewDescriptor(
module: ModuleDescriptorImpl,
fqName: FqName,
storageManager: StorageManager
) : LazyPackageViewDescriptorImpl(module, fqName, storageManager) {
private var cachedFragments: List<PackageFragmentDescriptor>? = null
override val fragments: List<PackageFragmentDescriptor>
get() {
cachedFragments?.let { return it }
val calculatedFragments = module.packageFragmentProvider.packageFragments(fqName)
if (calculatedFragments.isNotEmpty()) cachedFragments = calculatedFragments
return calculatedFragments
}
}
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.container.ComponentProvider
import org.jetbrains.kotlin.container.get
import org.jetbrains.kotlin.descriptors.ClassDescriptorWithResolutionScopes
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.PackageViewDescriptorFactory
import org.jetbrains.kotlin.diagnostics.Severity
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtFile
@@ -33,6 +34,7 @@ import org.jetbrains.kotlin.resolve.lazy.declarations.*
import org.jetbrains.kotlin.resolve.scopes.ImportingScope
import org.jetbrains.kotlin.resolve.scopes.utils.parentsWithSelf
import org.jetbrains.kotlin.resolve.scopes.utils.replaceImportingScopes
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ReplPackageViewDescriptorFactory
import org.jetbrains.kotlin.scripting.definitions.ScriptPriorities
import kotlin.script.experimental.api.SourceCode
import kotlin.script.experimental.jvm.util.CompiledHistoryItem
@@ -65,7 +67,8 @@ open class ReplCodeAnalyzerBase(
environment.configuration,
environment::createPackagePartProvider,
{ _, _ -> ScriptMutableDeclarationProviderFactory() },
implicitsResolutionFilter = implicitsResolutionFilter
implicitsResolutionFilter = implicitsResolutionFilter,
moduleCapabilities = mapOf(PackageViewDescriptorFactory.CAPABILITY to ReplPackageViewDescriptorFactory)
)
this.module = container.get()