From 21fdd2ffc3b2956b8dceed70be86e690cdb17c16 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Mon, 2 Nov 2015 14:57:52 +0300 Subject: [PATCH] Created common supertype for LexicalScope & ImportingScope -- HierarchicalScope --- .../synthetic/JavaSyntheticPropertiesScope.kt | 6 +- .../CallableReferencesResolutionUtils.kt | 4 +- .../tasks/CallableDescriptorCollectors.kt | 8 +- .../resolve/calls/tasks/TaskPrioritizer.kt | 7 +- .../resolve/lazy/FileScopeProviderImpl.kt | 18 ++-- .../kotlin/resolve/lazy/LazyImportScope.kt | 3 - .../resolve/scopes/LexicalWritableScope.kt | 2 +- .../jetbrains/kotlin/resolve/scopes/Scopes.kt | 55 ++++++------ .../kotlin/resolve/scopes/utils/ScopeUtils.kt | 87 +++++++++---------- .../resolve/ExpectedResolveDataUtil.java | 2 +- .../types/DefaultModalityModifiersTest.java | 2 +- .../kotlin/types/KotlinOverloadTest.java | 5 +- .../kotlin/types/TypeUnifierTest.java | 2 +- .../jetbrains/kotlin/types/typeTestUtils.kt | 7 +- .../codeInsight/ReferenceVariantsHelper.kt | 7 +- .../kotlin/idea/kdoc/KDocReference.kt | 5 +- .../idea/completion/HeuristicSignatures.kt | 3 +- .../idea/imports/KotlinImportOptimizer.kt | 4 +- .../callableBuilder/CallableBuilder.kt | 14 +-- .../callableBuilder/typeUtils.kt | 3 +- .../ReplaceWithAnnotationAnalyzer.kt | 3 +- .../idea/refactoring/CallableRefactoring.kt | 3 +- 22 files changed, 119 insertions(+), 131 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt index 0d816266bb5..7362f20237e 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/JavaSyntheticPropertiesScope.kt @@ -29,9 +29,7 @@ import org.jetbrains.kotlin.incremental.record import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.DescriptorUtils -import org.jetbrains.kotlin.resolve.scopes.BaseImportingScope -import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter -import org.jetbrains.kotlin.resolve.scopes.LexicalScope +import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.resolve.scopes.utils.collectSyntheticExtensionProperties import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.types.* @@ -49,7 +47,7 @@ interface SyntheticJavaPropertyDescriptor : PropertyDescriptor { val setMethod: FunctionDescriptor? companion object { - fun findByGetterOrSetter(getterOrSetter: FunctionDescriptor, resolutionScope: LexicalScope): SyntheticJavaPropertyDescriptor? { + fun findByGetterOrSetter(getterOrSetter: FunctionDescriptor, resolutionScope: HierarchicalScope): SyntheticJavaPropertyDescriptor? { val name = getterOrSetter.getName() if (name.isSpecial()) return null val identifier = name.getIdentifier() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/callableReferences/CallableReferencesResolutionUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/callableReferences/CallableReferencesResolutionUtils.kt index 845ba914ce6..eeaa6b7c7f0 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/callableReferences/CallableReferencesResolutionUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/callableReferences/CallableReferencesResolutionUtils.kt @@ -42,7 +42,7 @@ import org.jetbrains.kotlin.resolve.scopes.JetScopeUtils import org.jetbrains.kotlin.resolve.scopes.KtScope import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver -import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope +import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsLexicalScope import org.jetbrains.kotlin.resolve.source.toSourceElement import org.jetbrains.kotlin.types.FunctionPlaceholders import org.jetbrains.kotlin.types.KotlinType @@ -101,7 +101,7 @@ public fun resolvePossiblyAmbiguousCallableReference( fun resolveInScope(traceTitle: String, staticScope: KtScope): OverloadResolutionResults { val temporaryTraceAndCache = TemporaryTraceAndCache.create(context, traceTitle, reference) - val newContext = context.replaceTraceAndCache(temporaryTraceAndCache).replaceScope(staticScope.memberScopeAsImportingScope()) + val newContext = context.replaceTraceAndCache(temporaryTraceAndCache).replaceScope(staticScope.memberScopeAsLexicalScope()) val results = resolvePossiblyAmbiguousCallableReference(reference, ReceiverValue.NO_RECEIVER, newContext, resolutionMode, callResolver) resolutionMode.acceptResolution(results, temporaryTraceAndCache) return results diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/CallableDescriptorCollectors.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/CallableDescriptorCollectors.kt index 91744cd345f..f9b3e45cf35 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/CallableDescriptorCollectors.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/CallableDescriptorCollectors.kt @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.resolve.LibrarySourceHacks import org.jetbrains.kotlin.resolve.calls.tasks.createSynthesizedInvokes import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject import org.jetbrains.kotlin.resolve.descriptorUtil.hasClassObjectType -import org.jetbrains.kotlin.resolve.scopes.ImportingScope +import org.jetbrains.kotlin.resolve.scopes.HierarchicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalChainedScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.utils.* @@ -89,7 +89,7 @@ private object FunctionCollector : CallableDescriptorCollector { return lexicalScope.collectAllFromMeAndParent { - if (it !is ImportingScope && it.ownerDescriptor is FunctionDescriptor) { + if (it is LexicalScope && it.ownerDescriptor is FunctionDescriptor) { it.getContributedFunctions(name, location).filter { it.extensionReceiverParameter == null } + getConstructors(it.getContributedClassifier(name, location)) } @@ -139,7 +139,7 @@ private object FunctionCollector : CallableDescriptorCollector Boolean = { true } @@ -179,7 +179,7 @@ private object VariableCollector : CallableDescriptorCollector Boolean): Collection = emptyList() override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = null @@ -93,18 +85,23 @@ abstract class BaseLexicalScope(override val parent: LexicalScope?) : LexicalSco override fun getContributedFunctions(name: Name, location: LookupLocation): Collection = emptyList() } -abstract class BaseImportingScope(parent: ImportingScope?) : BaseLexicalScope(parent), ImportingScope { - override val parent: ImportingScope? - get() = super.parent as ImportingScope? +abstract class BaseLexicalScope( + parent: HierarchicalScope, + override val ownerDescriptor: DeclarationDescriptor +): BaseHierarchicalScope(parent), LexicalScope { + override val parent: HierarchicalScope + get() = super.parent!! - override val ownerDescriptor: DeclarationDescriptor - get() = throw UnsupportedOperationException() - - override final val isOwnerDescriptorAccessibleByLabel: Boolean + override val isOwnerDescriptorAccessibleByLabel: Boolean get() = false - override final val implicitReceiver: ReceiverParameterDescriptor? + override val implicitReceiver: ReceiverParameterDescriptor? get() = null +} + +abstract class BaseImportingScope(parent: ImportingScope?) : BaseHierarchicalScope(parent), ImportingScope { + override val parent: ImportingScope? + get() = super.parent as ImportingScope? override fun getContributedPackage(name: Name): PackageViewDescriptor? = null diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt index d97735f73f5..a45977fb7d3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt @@ -26,21 +26,21 @@ import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.util.collectionUtils.concat import org.jetbrains.kotlin.utils.Printer -public val LexicalScope.parentsWithSelf: Sequence +public val HierarchicalScope.parentsWithSelf: Sequence get() = sequence(this) { it.parent } -public val LexicalScope.parents: Sequence +public val HierarchicalScope.parents: Sequence get() = parentsWithSelf.drop(1) /** * Adds receivers to the list in order of locality, so that the closest (the most local) receiver goes first */ public fun LexicalScope.getImplicitReceiversHierarchy(): List = collectFromMeAndParent { - it.implicitReceiver + (it as? LexicalScope)?.implicitReceiver } public fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection = collectAllFromMeAndParent { - if (it.isOwnerDescriptorAccessibleByLabel && it.ownerDescriptor.name == labelName) { + if (it is LexicalScope && it.isOwnerDescriptorAccessibleByLabel && it.ownerDescriptor.name == labelName) { listOf(it.ownerDescriptor) } else { @@ -49,7 +49,7 @@ public fun LexicalScope.getDeclarationsByLabel(labelName: Name): Collection Boolean = { true } ): Collection { @@ -72,42 +72,42 @@ public fun LexicalScope.findLocalVariable(name: Name): VariableDescriptor? { } } -public fun LexicalScope.findClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? +public fun HierarchicalScope.findClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? = findFirstFromMeAndParent { it.getContributedClassifier(name, location) } -public fun LexicalScope.findPackage(name: Name): PackageViewDescriptor? +public fun HierarchicalScope.findPackage(name: Name): PackageViewDescriptor? = findFirstFromImportingScopes { it.getContributedPackage(name) } -public fun LexicalScope.collectVariables(name: Name, location: LookupLocation): Collection +public fun HierarchicalScope.collectVariables(name: Name, location: LookupLocation): Collection = collectAllFromMeAndParent { it.getContributedVariables(name, location) } -public fun LexicalScope.collectFunctions(name: Name, location: LookupLocation): Collection +public fun HierarchicalScope.collectFunctions(name: Name, location: LookupLocation): Collection = collectAllFromMeAndParent { it.getContributedFunctions(name, location) } -public fun LexicalScope.findVariable(name: Name, location: LookupLocation, predicate: (VariableDescriptor) -> Boolean = { true }): VariableDescriptor? { +public fun HierarchicalScope.findVariable(name: Name, location: LookupLocation, predicate: (VariableDescriptor) -> Boolean = { true }): VariableDescriptor? { processForMeAndParent { it.getContributedVariables(name, location).firstOrNull(predicate)?.let { return it } } return null } -public fun LexicalScope.findFunction(name: Name, location: LookupLocation, predicate: (FunctionDescriptor) -> Boolean = { true }): FunctionDescriptor? { +public fun HierarchicalScope.findFunction(name: Name, location: LookupLocation, predicate: (FunctionDescriptor) -> Boolean = { true }): FunctionDescriptor? { processForMeAndParent { it.getContributedFunctions(name, location).firstOrNull(predicate)?.let { return it } } return null } -public fun LexicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation) +public fun HierarchicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation) = collectAllFromImportingScopes { it.getContributedSyntheticExtensionProperties(receiverTypes, name, location) } -public fun LexicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection, name: Name, location: LookupLocation) +public fun HierarchicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection, name: Name, location: LookupLocation) = collectAllFromImportingScopes { it.getContributedSyntheticExtensionFunctions(receiverTypes, name, location) } -public fun LexicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection) +public fun HierarchicalScope.collectSyntheticExtensionProperties(receiverTypes: Collection) = collectAllFromImportingScopes { it.getContributedSyntheticExtensionProperties(receiverTypes) } -public fun LexicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection) +public fun HierarchicalScope.collectSyntheticExtensionFunctions(receiverTypes: Collection) = collectAllFromImportingScopes { it.getContributedSyntheticExtensionFunctions(receiverTypes) } public fun LexicalScope.takeSnapshot(): LexicalScope = if (this is LexicalWritableScope) takeSnapshot() else this @@ -115,6 +115,9 @@ public fun LexicalScope.takeSnapshot(): LexicalScope = if (this is LexicalWritab @JvmOverloads public fun KtScope.memberScopeAsImportingScope(parentScope: ImportingScope? = null): ImportingScope = MemberScopeToImportingScopeAdapter(parentScope, this) +@Deprecated("Temporary method for scope migration") +public fun KtScope.memberScopeAsLexicalScope(): LexicalScope = memberScopeAsImportingScope().asLexicalScope(getContainingDeclaration()) + private class MemberScopeToImportingScopeAdapter(override val parent: ImportingScope?, val memberScope: KtScope) : ImportingScope { override fun getContributedPackage(name: Name): PackageViewDescriptor? = memberScope.getPackage(name) @@ -133,9 +136,6 @@ private class MemberScopeToImportingScopeAdapter(override val parent: ImportingS override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) = memberScope.getDescriptors(kindFilter, nameFilter) - override val ownerDescriptor: DeclarationDescriptor - get() = memberScope.getContainingDeclaration() - override fun getContributedClassifier(name: Name, location: LookupLocation) = memberScope.getClassifier(name, location) override fun getContributedVariables(name: Name, location: LookupLocation) = memberScope.getProperties(name, location) @@ -159,7 +159,7 @@ private class MemberScopeToImportingScopeAdapter(override val parent: ImportingS } } -inline fun LexicalScope.processForMeAndParent(process: (LexicalScope) -> Unit) { +inline fun HierarchicalScope.processForMeAndParent(process: (HierarchicalScope) -> Unit) { var currentScope = this while (true) { process(currentScope) @@ -167,8 +167,8 @@ inline fun LexicalScope.processForMeAndParent(process: (LexicalScope) -> Unit) { } } -private inline fun LexicalScope.collectFromMeAndParent( - collect: (LexicalScope) -> T? +private inline fun HierarchicalScope.collectFromMeAndParent( + collect: (HierarchicalScope) -> T? ): List { var result: MutableList? = null processForMeAndParent { @@ -183,39 +183,34 @@ private inline fun LexicalScope.collectFromMeAndParent( return result ?: emptyList() } -inline fun LexicalScope.collectAllFromMeAndParent( - collect: (LexicalScope) -> Collection +inline fun HierarchicalScope.collectAllFromMeAndParent( + collect: (HierarchicalScope) -> Collection ): Collection { var result: Collection? = null processForMeAndParent { result = result.concat(collect(it)) } return result ?: emptySet() } -inline fun LexicalScope.findFirstFromMeAndParent(fetch: (LexicalScope) -> T?): T? { +inline fun HierarchicalScope.findFirstFromMeAndParent(fetch: (HierarchicalScope) -> T?): T? { processForMeAndParent { fetch(it)?.let { return it } } return null } -inline fun LexicalScope.collectAllFromImportingScopes( +inline fun HierarchicalScope.collectAllFromImportingScopes( collect: (ImportingScope) -> Collection ): Collection { return collectAllFromMeAndParent { if (it is ImportingScope) collect(it) else emptyList() } } -inline fun LexicalScope.findFirstFromImportingScopes(fetch: (ImportingScope) -> T?): T? { +inline fun HierarchicalScope.findFirstFromImportingScopes(fetch: (ImportingScope) -> T?): T? { return findFirstFromMeAndParent { if (it is ImportingScope) fetch(it) else null } } fun LexicalScope.addImportingScopes(importScopes: List): LexicalScope { - if (this is ImportingScope) { - return chainImportingScopes(importScopes, this)!! - } - else { - val lastNonImporting = parentsWithSelf.last { it !is ImportingScope } - val firstImporting = lastNonImporting.parent as ImportingScope? - val newFirstImporting = chainImportingScopes(importScopes, firstImporting) - return LexicalScopeWrapper(this, newFirstImporting) - } + val lastLexicalScope = parentsWithSelf.last { it is LexicalScope } + val firstImporting = lastLexicalScope.parent as ImportingScope + val newFirstImporting = chainImportingScopes(importScopes, firstImporting) + return LexicalScopeWrapper(this, newFirstImporting!!) } fun LexicalScope.addImportingScope(importScope: ImportingScope): LexicalScope @@ -229,22 +224,19 @@ fun ImportingScope.withParent(newParent: ImportingScope?): ImportingScope { } fun LexicalScope.replaceImportingScopes(importingScopeChain: ImportingScope?): LexicalScope { - return if (this is ImportingScope) - importingScopeChain ?: ImportingScope.Empty - else - LexicalScopeWrapper(this, importingScopeChain) + return LexicalScopeWrapper(this, importingScopeChain ?: ImportingScope.Empty) } -private class LexicalScopeWrapper(val delegate: LexicalScope, val newImportingScopeChain: ImportingScope?): LexicalScope by delegate { - override val parent: LexicalScope? by lazy(LazyThreadSafetyMode.NONE) { +private class LexicalScopeWrapper(val delegate: LexicalScope, val newImportingScopeChain: ImportingScope): LexicalScope by delegate { + override val parent: HierarchicalScope by lazy(LazyThreadSafetyMode.NONE) { assert(delegate !is ImportingScope) val parent = delegate.parent - if (parent == null || parent is ImportingScope) { - newImportingScopeChain + if (parent is LexicalScope) { + LexicalScopeWrapper(parent, newImportingScopeChain) } else { - LexicalScopeWrapper(parent, newImportingScopeChain) + newImportingScopeChain } } } @@ -256,3 +248,10 @@ fun chainImportingScopes(scopes: List, tail: ImportingScope? = n scope.withParent(current) } } + +fun ImportingScope?.asLexicalScope(ownerDescriptor: DeclarationDescriptor): LexicalScope + = object : BaseLexicalScope(this ?: ImportingScope.Empty, ownerDescriptor) { + override fun printStructure(p: Printer) { + p.println("Empty lexical scope with owner = $ownerDescriptor and parent = ${this@asLexicalScope}.") + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/resolve/ExpectedResolveDataUtil.java b/compiler/tests/org/jetbrains/kotlin/resolve/ExpectedResolveDataUtil.java index a2aba679709..d98450702bc 100644 --- a/compiler/tests/org/jetbrains/kotlin/resolve/ExpectedResolveDataUtil.java +++ b/compiler/tests/org/jetbrains/kotlin/resolve/ExpectedResolveDataUtil.java @@ -138,7 +138,7 @@ public class ExpectedResolveDataUtil { emptyModule.setDependencies(emptyModule); emptyModule.initialize(PackageFragmentProvider.Empty.INSTANCE$); - LexicalScopeImpl lexicalScope = new LexicalScopeImpl(ImportingScope.Empty.INSTANCE, classDescriptor, false, + LexicalScopeImpl lexicalScope = new LexicalScopeImpl(ScopeUtilsKt.asLexicalScope(ImportingScope.Empty.INSTANCE, emptyModule), classDescriptor, false, classDescriptor.getThisAsReceiverParameter(), "Scope with implicit this for class: " + classDescriptor); diff --git a/compiler/tests/org/jetbrains/kotlin/types/DefaultModalityModifiersTest.java b/compiler/tests/org/jetbrains/kotlin/types/DefaultModalityModifiersTest.java index 833eb219d32..442ff31dff2 100644 --- a/compiler/tests/org/jetbrains/kotlin/types/DefaultModalityModifiersTest.java +++ b/compiler/tests/org/jetbrains/kotlin/types/DefaultModalityModifiersTest.java @@ -92,7 +92,7 @@ public class DefaultModalityModifiersTest extends KotlinLiteFixture { assert aClass instanceof KtClass; AnalysisResult bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(file); final DeclarationDescriptor classDescriptor = bindingContext.getBindingContext().get(BindingContext.DECLARATION_TO_DESCRIPTOR, aClass); - return new LexicalScopeImpl(ScopeUtilsKt.memberScopeAsImportingScope(libraryScope), root, false, null, + return new LexicalScopeImpl(TypeTestUtilsKt.asLexicalScope(libraryScope), root, false, null, "JetDefaultModalityModifiersTest", RedeclarationHandler.DO_NOTHING, new Function1() { @Override diff --git a/compiler/tests/org/jetbrains/kotlin/types/KotlinOverloadTest.java b/compiler/tests/org/jetbrains/kotlin/types/KotlinOverloadTest.java index e7f30ee1dd7..4839e649125 100644 --- a/compiler/tests/org/jetbrains/kotlin/types/KotlinOverloadTest.java +++ b/compiler/tests/org/jetbrains/kotlin/types/KotlinOverloadTest.java @@ -25,8 +25,7 @@ import org.jetbrains.kotlin.resolve.FunctionDescriptorResolver; import org.jetbrains.kotlin.resolve.OverloadUtil; import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo; import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform; -import org.jetbrains.kotlin.resolve.scopes.ImportingScope; -import org.jetbrains.kotlin.resolve.scopes.utils.ScopeUtilsKt; +import org.jetbrains.kotlin.resolve.scopes.LexicalScope; import org.jetbrains.kotlin.test.ConfigurationKind; import org.jetbrains.kotlin.test.KotlinLiteFixture; import org.jetbrains.kotlin.test.KotlinTestUtils; @@ -171,7 +170,7 @@ public class KotlinOverloadTest extends KotlinLiteFixture { private FunctionDescriptor makeFunction(String funDecl) { KtNamedFunction function = KtPsiFactoryKt.KtPsiFactory(getProject()).createFunction(funDecl); - ImportingScope scope = ScopeUtilsKt.memberScopeAsImportingScope(JvmPlatform.INSTANCE$.getBuiltIns().getBuiltInsPackageScope()); + LexicalScope scope = TypeTestUtilsKt.asLexicalScope(JvmPlatform.INSTANCE$.getBuiltIns().getBuiltInsPackageScope()); return functionDescriptorResolver.resolveFunctionDescriptor(root, scope, function, KotlinTestUtils.DUMMY_TRACE, DataFlowInfo.EMPTY); } } diff --git a/compiler/tests/org/jetbrains/kotlin/types/TypeUnifierTest.java b/compiler/tests/org/jetbrains/kotlin/types/TypeUnifierTest.java index be67f3e1df0..06ae9250da0 100644 --- a/compiler/tests/org/jetbrains/kotlin/types/TypeUnifierTest.java +++ b/compiler/tests/org/jetbrains/kotlin/types/TypeUnifierTest.java @@ -201,7 +201,7 @@ public class TypeUnifierTest extends KotlinLiteFixture { } private TypeProjection makeTypeProjection(KtScope scope, String typeStr) { - LexicalScope withX = new LexicalScopeImpl(ScopeUtilsKt.memberScopeAsImportingScope(scope), scope.getContainingDeclaration(), + LexicalScope withX = new LexicalScopeImpl(TypeTestUtilsKt.asLexicalScope(scope), scope.getContainingDeclaration(), false, null, "With X", RedeclarationHandler.DO_NOTHING, new Function1() { @Override diff --git a/compiler/tests/org/jetbrains/kotlin/types/typeTestUtils.kt b/compiler/tests/org/jetbrains/kotlin/types/typeTestUtils.kt index 6d1582a7b17..14bbc05d26d 100644 --- a/compiler/tests/org/jetbrains/kotlin/types/typeTestUtils.kt +++ b/compiler/tests/org/jetbrains/kotlin/types/typeTestUtils.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.types -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.resolve.scopes.BaseLexicalScope import org.jetbrains.kotlin.resolve.scopes.KtScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope @@ -25,11 +24,9 @@ import org.jetbrains.kotlin.utils.Printer public fun KtScope.asLexicalScope(): LexicalScope { val importingScope = memberScopeAsImportingScope() - return object : BaseLexicalScope(importingScope) { - override val ownerDescriptor: DeclarationDescriptor - get() = importingScope.ownerDescriptor - + return object : BaseLexicalScope(importingScope, getContainingDeclaration()) { override fun printStructure(p: Printer) { + p.println("Util scope for tests") } } } diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/codeInsight/ReferenceVariantsHelper.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/codeInsight/ReferenceVariantsHelper.kt index 7cd94f2b0af..af8a5a9945a 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/codeInsight/ReferenceVariantsHelper.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/codeInsight/ReferenceVariantsHelper.kt @@ -30,10 +30,7 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.bindingContextUtil.getDataFlowInfo import org.jetbrains.kotlin.resolve.calls.smartcasts.SmartCastManager import org.jetbrains.kotlin.resolve.isAnnotatedAsHidden -import org.jetbrains.kotlin.resolve.scopes.DescriptorKindExclude -import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter -import org.jetbrains.kotlin.resolve.scopes.LexicalScope -import org.jetbrains.kotlin.resolve.scopes.getDescriptorsFiltered +import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver import org.jetbrains.kotlin.resolve.scopes.utils.collectDescriptorsFiltered import org.jetbrains.kotlin.resolve.scopes.utils.collectSyntheticExtensionFunctions @@ -299,7 +296,7 @@ class ReferenceVariantsHelper( } private fun MutableSet.addNonExtensionCallablesAndConstructors( - scope: LexicalScope, + scope: HierarchicalScope, kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean, constructorFilter: (ClassDescriptor) -> Boolean diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/kdoc/KDocReference.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/kdoc/KDocReference.kt index 4411aa0ca8f..bf83e30ec58 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/kdoc/KDocReference.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/kdoc/KDocReference.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.FunctionDescriptorUtil import org.jetbrains.kotlin.resolve.scopes.* +import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope import org.jetbrains.kotlin.resolve.scopes.utils.collectDescriptorsFiltered import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.resolve.source.PsiSourceElement @@ -148,10 +149,10 @@ private fun getClassInnerScope(outerScope: LexicalScope, descriptor: ClassDescri public fun getResolutionScope(resolutionFacade: ResolutionFacade, descriptor: DeclarationDescriptor): LexicalScope { return when (descriptor) { is PackageFragmentDescriptor -> - getPackageInnerScope(descriptor).memberScopeAsImportingScope() + getPackageInnerScope(descriptor).memberScopeAsImportingScope().asLexicalScope(descriptor) is PackageViewDescriptor -> - descriptor.memberScope.memberScopeAsImportingScope() + descriptor.memberScope.memberScopeAsImportingScope().asLexicalScope(descriptor) is ClassDescriptor -> getClassInnerScope(getOuterScope(descriptor, resolutionFacade), descriptor) diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/HeuristicSignatures.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/HeuristicSignatures.kt index 1fdcc31a306..84df55a1f52 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/HeuristicSignatures.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/HeuristicSignatures.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.resolve.BindingTraceContext import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.TypeResolver import org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl +import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.types.IndexedParametersSubstitution import org.jetbrains.kotlin.types.KotlinType @@ -76,7 +77,7 @@ public class HeuristicSignatures( private fun typeFromText(text: String, typeParameters: Collection): KotlinType { val typeRef = KtPsiFactory(project).createType(text) - val rootPackagesScope = SubpackagesScope(moduleDescriptor, FqName.ROOT).memberScopeAsImportingScope() + val rootPackagesScope = SubpackagesScope(moduleDescriptor, FqName.ROOT).memberScopeAsImportingScope().asLexicalScope(moduleDescriptor) val scope = LexicalScopeImpl(rootPackagesScope, moduleDescriptor, false, null, "Root packages + type parameters") { typeParameters.forEach { addClassifierDescriptor(it) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/imports/KotlinImportOptimizer.kt b/idea/src/org/jetbrains/kotlin/idea/imports/KotlinImportOptimizer.kt index b63d4e25c42..6dae9e01c97 100644 --- a/idea/src/org/jetbrains/kotlin/idea/imports/KotlinImportOptimizer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/imports/KotlinImportOptimizer.kt @@ -39,7 +39,7 @@ import org.jetbrains.kotlin.renderer.render import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.ImportPath import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor -import org.jetbrains.kotlin.resolve.scopes.LexicalScope +import org.jetbrains.kotlin.resolve.scopes.HierarchicalScope import org.jetbrains.kotlin.resolve.scopes.utils.* import java.util.* @@ -118,7 +118,7 @@ public class KotlinImportOptimizer() : ImportOptimizer { private fun isAccessibleAsMember(target: DeclarationDescriptor, place: KtElement, bindingContext: BindingContext): Boolean { if (target.containingDeclaration !is ClassDescriptor) return false - fun isInScope(scope: LexicalScope): Boolean { + fun isInScope(scope: HierarchicalScope): Boolean { return when (target) { is FunctionDescriptor -> scope.findFunction(target.name, NoLookupLocation.FROM_IDE) { it == target } != null diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt index ddf05bde303..51b8d1a361d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/callableBuilder/CallableBuilder.kt @@ -66,8 +66,10 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.scopes.* +import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope +import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsLexicalScope import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeProjectionImpl import org.jetbrains.kotlin.types.Variance @@ -85,7 +87,7 @@ private val ATTRIBUTE_FUNCTION_NAME = "FUNCTION_NAME" /** * Represents a single choice for a type (e.g. parameter type or return type). */ -class TypeCandidate(val theType: KotlinType, scope: LexicalScope? = null) { +class TypeCandidate(val theType: KotlinType, scope: HierarchicalScope? = null) { public val typeParameters: Array var renderedType: String? = null private set @@ -162,7 +164,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { private fun computeTypeCandidates( typeInfo: TypeInfo, substitutions: List, - scope: LexicalScope): List { + scope: HierarchicalScope): List { if (!typeInfo.substitutionsAllowed) return computeTypeCandidates(typeInfo) return typeCandidates.getOrPut(typeInfo) { val types = typeInfo.getPossibleTypes(this).asReversed() @@ -337,7 +339,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { mandatoryTypeParametersAsCandidates.forEach { it.render(typeParameterNameMap, fakeFunction) } } - private fun getDeclarationScope(): LexicalScope { + private fun getDeclarationScope(): HierarchicalScope { if (config.isExtension || receiverClassDescriptor == null) { return currentFileModule.getPackage(config.currentFile.packageFqName).memberScope.memberScopeAsImportingScope() } @@ -352,7 +354,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { .map { TypeProjectionImpl(it.getDefaultType()) } val memberScope = receiverClassDescriptor.getMemberScope(projections) - return LexicalScopeImpl(memberScope.memberScopeAsImportingScope(), receiverClassDescriptor, false, null, + return LexicalScopeImpl(memberScope.memberScopeAsLexicalScope(), receiverClassDescriptor, false, null, "Scope with type parameters for ${receiverClassDescriptor.getName()}") { receiverClassDescriptor.typeConstructor.parameters.forEach { addClassifierDescriptor(it) } } @@ -380,7 +382,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { } } - private fun createFakeFunctionDescriptor(scope: LexicalScope, typeParameterCount: Int): FunctionDescriptor { + private fun createFakeFunctionDescriptor(scope: HierarchicalScope, typeParameterCount: Int): FunctionDescriptor { val fakeFunction = SimpleFunctionDescriptorImpl.create( MutablePackageFragmentDescriptor(currentFileModule, FqName("fake")), Annotations.EMPTY, @@ -602,7 +604,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) { else classBody.addAfter(declaration, classBody.lBrace!!) as KtNamedDeclaration } - private fun getTypeParameterRenames(scope: LexicalScope): Map { + private fun getTypeParameterRenames(scope: HierarchicalScope): Map { val allTypeParametersNotInScope = LinkedHashSet() mandatoryTypeParametersAsCandidates.asSequence() diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/callableBuilder/typeUtils.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/callableBuilder/typeUtils.kt index c957c7b1460..e0547be8e0a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/callableBuilder/typeUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/callableBuilder/typeUtils.kt @@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass +import org.jetbrains.kotlin.resolve.scopes.HierarchicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier import org.jetbrains.kotlin.types.* @@ -80,7 +81,7 @@ private fun KotlinType.render(typeParameterNameMap: Map) = render(typeParameterNameMap, false) internal fun KotlinType.renderLong(typeParameterNameMap: Map) = render(typeParameterNameMap, true) -internal fun getTypeParameterNamesNotInScope(typeParameters: Collection, scope: LexicalScope): List { +internal fun getTypeParameterNamesNotInScope(typeParameters: Collection, scope: HierarchicalScope): List { return typeParameters.filter { typeParameter -> val classifier = scope.findClassifier(typeParameter.name, NoLookupLocation.FROM_IDE) classifier == null || classifier != typeParameter diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/ReplaceWithAnnotationAnalyzer.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/ReplaceWithAnnotationAnalyzer.kt index 4d25ba81d05..e2108d07192 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/ReplaceWithAnnotationAnalyzer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/ReplaceWithAnnotationAnalyzer.kt @@ -45,6 +45,7 @@ import org.jetbrains.kotlin.resolve.lazy.FileScopeProvider import org.jetbrains.kotlin.resolve.lazy.descriptors.ClassResolutionScopesSupport import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver +import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope import org.jetbrains.kotlin.resolve.scopes.utils.chainImportingScopes import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import org.jetbrains.kotlin.storage.LockBasedStorageManager @@ -245,7 +246,7 @@ object ReplaceWithAnnotationAnalyzer { } is PackageViewDescriptor -> - chainImportingScopes(listOf(descriptor.memberScope.memberScopeAsImportingScope()) + additionalScopes) + chainImportingScopes(listOf(descriptor.memberScope.memberScopeAsImportingScope()) + additionalScopes).asLexicalScope(ownerDescriptor) is ClassDescriptor -> { val outerScope = getResolutionScope(descriptor.containingDeclaration, ownerDescriptor, additionalScopes) ?: return null diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/CallableRefactoring.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/CallableRefactoring.kt index 04991923ab2..2fb8d0434bf 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/CallableRefactoring.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/CallableRefactoring.kt @@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.OverrideResolver import org.jetbrains.kotlin.resolve.scopes.LexicalScope +import org.jetbrains.kotlin.resolve.scopes.utils.asLexicalScope import org.jetbrains.kotlin.resolve.scopes.utils.memberScopeAsImportingScope import java.util.* @@ -194,7 +195,7 @@ fun DeclarationDescriptor.getContainingScope(): LexicalScope? { val containingDescriptor = getContainingDeclaration() ?: return null return when (containingDescriptor) { is ClassDescriptorWithResolutionScopes -> containingDescriptor.getScopeForInitializerResolution() - is PackageFragmentDescriptor -> containingDescriptor.getMemberScope().memberScopeAsImportingScope() + is PackageFragmentDescriptor -> containingDescriptor.getMemberScope().memberScopeAsImportingScope().asLexicalScope(this) else -> null } }