diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyExplicitImportScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyExplicitImportScope.kt index bed75f8ec57..8ee763ea62f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyExplicitImportScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/LazyExplicitImportScope.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.scopes.BaseImportingScope import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.MemberScope +import org.jetbrains.kotlin.utils.CallOnceFunction import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.addIfNotNull @@ -32,7 +33,7 @@ class LazyExplicitImportScope( private val packageFragmentForVisibilityCheck: PackageFragmentDescriptor?, private val declaredName: Name, private val aliasName: Name, - private val storeReferences: (Collection) -> Unit + private val storeReferences: CallOnceFunction, Unit> ) : BaseImportingScope(null) { override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt index a576b2727da..bc9ff086ede 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt @@ -45,6 +45,7 @@ import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.source.KotlinSourceElement import org.jetbrains.kotlin.types.expressions.ExpressionTypingContext import org.jetbrains.kotlin.types.expressions.isWithoutValueArguments +import org.jetbrains.kotlin.utils.CallOnceFunction class QualifiedExpressionResolver(val languageVersionSettings: LanguageVersionSettings) { fun resolvePackageHeader( @@ -310,22 +311,22 @@ class QualifiedExpressionResolver(val languageVersionSettings: LanguageVersionSe packageOrClassDescriptor, packageFragmentForVisibilityCheck, lastPart.name, - aliasName - ) { candidates -> - - if (candidates.isNotEmpty()) { - storeResult( - trace, - lastPart.expression, - candidates, - packageFragmentForVisibilityCheck, - position = QualifierPosition.IMPORT, - isQualifier = false - ) - } else { - tryResolveDescriptorsWhichCannotBeImported(trace, moduleDescriptor, packageOrClassDescriptor, lastPart) + aliasName, + CallOnceFunction(Unit) { candidates -> + if (candidates.isNotEmpty()) { + storeResult( + trace, + lastPart.expression, + candidates, + packageFragmentForVisibilityCheck, + position = QualifierPosition.IMPORT, + isQualifier = false + ) + } else { + tryResolveDescriptorsWhichCannotBeImported(trace, moduleDescriptor, packageOrClassDescriptor, lastPart) + } } - } + ) } private fun tryResolveDescriptorsWhichCannotBeImported( diff --git a/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java b/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java index e92c234d8a5..fb970b6385e 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java +++ b/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java @@ -305,7 +305,10 @@ public class LockBasedStorageManager implements StorageManager { private static class LockBasedLazyValue implements NullableLazyValue { private final LockBasedStorageManager storageManager; - private final Function0 computable; + /** + * Computable function is null when value has been calculated + */ + private Function0 computable; @Nullable private volatile Object value = NotValue.NOT_COMPUTED; @@ -359,6 +362,7 @@ public class LockBasedStorageManager implements StorageManager { postCompute(typedValue); value = typedValue; + computable = null; return typedValue; } catch (Throwable throwable) { diff --git a/core/util.runtime/src/org/jetbrains/kotlin/utils/CallOnceFunction.kt b/core/util.runtime/src/org/jetbrains/kotlin/utils/CallOnceFunction.kt new file mode 100644 index 00000000000..84ebe4db14b --- /dev/null +++ b/core/util.runtime/src/org/jetbrains/kotlin/utils/CallOnceFunction.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2010-2020 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.utils + +import java.util.concurrent.atomic.AtomicReference + +class CallOnceFunction(private val defaultValue: T, delegate: Function1) : Function1 { + private val functionRef: AtomicReference?> = AtomicReference(delegate) + + override fun invoke(p1: F): T = functionRef.getAndSet(null)?.invoke(p1) ?: defaultValue +} \ No newline at end of file