From 3faf6cc3e81870b51c32398e474bfbf09fcbb447 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 7 Apr 2015 15:26:29 +0300 Subject: [PATCH] Correct value passed as containingDeclarationOrModule to SmartCastUtils --- .../codeInsight/ReferenceVariantsHelper.kt | 10 ++++------ .../kotlin/idea/util/extensionsUtils.kt | 20 +++++++++++++------ .../kotlin/idea/core/KotlinIndicesHelper.kt | 9 ++++----- 3 files changed, 22 insertions(+), 17 deletions(-) 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 ba25e86ef5b..e506acb3279 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 @@ -70,6 +70,7 @@ public class ReferenceVariantsHelper( ): Collection { val parent = expression.getParent() val resolutionScope = context[BindingContext.RESOLUTION_SCOPE, expression] ?: return listOf() + val containingDeclaration = resolutionScope.getContainingDeclaration() if (parent is JetImportDirective || parent is JetPackageDirective) { return resolutionScope.getDescriptorsFiltered(kindFilter.restrictedToKinds(DescriptorKindFilter.PACKAGES_MASK), nameFilter) @@ -100,10 +101,7 @@ public class ReferenceVariantsHelper( val receiverValue = ExpressionReceiver(receiverExpression, expressionType) val dataFlowInfo = context.getDataFlowInfo(expression) - for (variant in SmartCastUtils.getSmartCastVariantsWithLessSpecificExcluded(receiverValue, - context, - resolutionScope.getContainingDeclaration(), - dataFlowInfo)) { + for (variant in SmartCastUtils.getSmartCastVariantsWithLessSpecificExcluded(receiverValue, context, containingDeclaration, dataFlowInfo)) { descriptors.addMembersFromReceiver(variant, callType, kindFilter, nameFilter) } @@ -129,7 +127,7 @@ public class ReferenceVariantsHelper( if (descriptor is CallableDescriptor && descriptor.getExtensionReceiverParameter() != null) { val dispatchReceiver = descriptor.getDispatchReceiverParameter() if (dispatchReceiver == null || dispatchReceiver in receivers) { - descriptorsSet.addAll(descriptor.substituteExtensionIfCallable(receiverValues, context, dataFlowInfo, CallType.NORMAL)) + descriptorsSet.addAll(descriptor.substituteExtensionIfCallable(receiverValues, context, dataFlowInfo, CallType.NORMAL, containingDeclaration)) } } else { @@ -196,7 +194,7 @@ public class ReferenceVariantsHelper( val extensionsFilter = kindFilter.exclude(DescriptorKindExclude.NonExtensions) fun processExtension(descriptor: DeclarationDescriptor) { - addAll((descriptor as CallableDescriptor).substituteExtensionIfCallable(receiver, callType, context, dataFlowInfo)) + addAll((descriptor as CallableDescriptor).substituteExtensionIfCallable(receiver, callType, context, dataFlowInfo, resolutionScope.getContainingDeclaration())) } // process member extensions from implicit receivers separately to filter out ones from implicit receivers with no instance diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/extensionsUtils.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/extensionsUtils.kt index e816cb4016e..a464b24be17 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/extensionsUtils.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/extensionsUtils.kt @@ -46,9 +46,10 @@ public fun CallableDescriptor.substituteExtensionIfCallable( receivers: Collection, context: BindingContext, dataFlowInfo: DataFlowInfo, - callType: CallType + callType: CallType, + containingDeclarationOrModule: DeclarationDescriptor ): Collection { - val sequence = receivers.sequence().flatMap { substituteExtensionIfCallable(it, callType, context, dataFlowInfo).sequence() } + val sequence = receivers.sequence().flatMap { substituteExtensionIfCallable(it, callType, context, dataFlowInfo, containingDeclarationOrModule).sequence() } if (getTypeParameters().isEmpty()) { // optimization for non-generic callables return sequence.firstOrNull()?.let { listOf(it) } ?: listOf() } @@ -57,19 +58,26 @@ public fun CallableDescriptor.substituteExtensionIfCallable( } } -public fun CallableDescriptor.substituteExtensionIfCallableWithImplicitReceiver(scope: JetScope, context: BindingContext, dataFlowInfo: DataFlowInfo): Collection - = substituteExtensionIfCallable(scope.getImplicitReceiversWithInstance().map { it.getValue() }, context, dataFlowInfo, CallType.NORMAL) +public fun CallableDescriptor.substituteExtensionIfCallableWithImplicitReceiver( + scope: JetScope, + context: BindingContext, + dataFlowInfo: DataFlowInfo +): Collection { + val receiverValues = scope.getImplicitReceiversWithInstance().map { it.getValue() } + return substituteExtensionIfCallable(receiverValues, context, dataFlowInfo, CallType.NORMAL, scope.getContainingDeclaration()) +} public fun CallableDescriptor.substituteExtensionIfCallable( receiver: ReceiverValue, callType: CallType, bindingContext: BindingContext, - dataFlowInfo: DataFlowInfo + dataFlowInfo: DataFlowInfo, + containingDeclarationOrModule: DeclarationDescriptor ): Collection { if (!receiver.exists()) return listOf() if (!callType.canCall(this)) return listOf() - var types = SmartCastUtils.getSmartCastVariants(receiver, bindingContext, getContainingDeclaration(), dataFlowInfo).sequence() + var types = SmartCastUtils.getSmartCastVariants(receiver, bindingContext, containingDeclarationOrModule, dataFlowInfo).sequence() if (callType == CallType.SAFE) { types = types.map { it.makeNotNullable() } diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt index 8e97a7a3846..ab850cbd477 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt @@ -85,9 +85,8 @@ public class KotlinIndicesHelper( if (receiverValues.isEmpty()) return emptyList() val dataFlowInfo = bindingContext.getDataFlowInfo(expression) - val containingDeclaration = bindingContext[BindingContext.RESOLUTION_SCOPE, expression]?.getContainingDeclaration() ?: return emptyList() - val receiverTypeNames = possibleReceiverTypeNames(receiverValues.map { it.first }, containingDeclaration, dataFlowInfo) + val receiverTypeNames = possibleReceiverTypeNames(receiverValues.map { it.first }, dataFlowInfo) val index = JetTopLevelExtensionsByReceiverTypeIndex.INSTANCE @@ -102,10 +101,10 @@ public class KotlinIndicesHelper( return findSuitableExtensions(declarations, receiverValues, dataFlowInfo, bindingContext) } - private fun possibleReceiverTypeNames(receiverValues: Collection, containingDeclaration: DeclarationDescriptor, dataFlowInfo: DataFlowInfo): Set { + private fun possibleReceiverTypeNames(receiverValues: Collection, dataFlowInfo: DataFlowInfo): Set { val result = HashSet() for (receiverValue in receiverValues) { - for (type in SmartCastUtils.getSmartCastVariants(receiverValue, bindingContext, containingDeclaration, dataFlowInfo)) { + for (type in SmartCastUtils.getSmartCastVariants(receiverValue, bindingContext, moduleDescriptor, dataFlowInfo)) { result.addTypeNames(type) } } @@ -150,7 +149,7 @@ public class KotlinIndicesHelper( fun processDescriptor(descriptor: CallableDescriptor) { if (visibilityFilter(descriptor)) { for ((receiverValue, callType) in receiverValues) { - result.addAll(descriptor.substituteExtensionIfCallable(receiverValue, callType, bindingContext, dataFlowInfo)) + result.addAll(descriptor.substituteExtensionIfCallable(receiverValue, callType, bindingContext, dataFlowInfo, moduleDescriptor)) } } }