diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/ShadowedDeclarationsFilter.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/ShadowedDeclarationsFilter.kt index 8580e53f445..638ccd1d5a9 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/ShadowedDeclarationsFilter.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/ShadowedDeclarationsFilter.kt @@ -31,7 +31,6 @@ import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext import org.jetbrains.kotlin.resolve.calls.context.CheckArgumentTypesMode import org.jetbrains.kotlin.resolve.calls.context.ContextDependency import org.jetbrains.kotlin.resolve.scopes.ExplicitImportsScope -import org.jetbrains.kotlin.resolve.scopes.LexicalChainedScope import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue import org.jetbrains.kotlin.types.TypeUtils @@ -164,16 +163,14 @@ public class ShadowedDeclarationsFilter( override fun getCallType() = Call.CallType.DEFAULT } - var resolutionScope = bindingContext[BindingContext.LEXICAL_SCOPE, context] ?: return descriptors + var lexicalScope = bindingContext[BindingContext.LEXICAL_SCOPE, context] ?: return descriptors if (descriptorsToImport.isNotEmpty()) { - resolutionScope = LexicalChainedScope(resolutionScope, resolutionScope.ownerDescriptor, false, null, - "Scope with explicitly imported descriptors", - ExplicitImportsScope(descriptorsToImport)) + lexicalScope = lexicalScope.addImportScope(ExplicitImportsScope(descriptorsToImport)) } val dataFlowInfo = bindingContext.getDataFlowInfo(context) - val context = BasicCallResolutionContext.create(bindingTrace, resolutionScope, newCall, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo, + val context = BasicCallResolutionContext.create(bindingTrace, lexicalScope, newCall, TypeUtils.NO_EXPECTED_TYPE, dataFlowInfo, ContextDependency.INDEPENDENT, CheckArgumentTypesMode.CHECK_VALUE_ARGUMENTS, CallChecker.DoNothing, false) val callResolver = resolutionFacade.frontendService() diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/scopeUtils.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/scopeUtils.kt index ea118ed4753..a9c784b75e7 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/scopeUtils.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/scopeUtils.kt @@ -22,7 +22,11 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.scopes.FileScope import org.jetbrains.kotlin.resolve.scopes.JetScope +import org.jetbrains.kotlin.resolve.scopes.LexicalChainedScope +import org.jetbrains.kotlin.resolve.scopes.LexicalScope +import org.jetbrains.kotlin.resolve.scopes.utils.getFileScope public fun JetScope.getAllAccessibleVariables(name: Name): Collection @@ -42,3 +46,21 @@ public fun JetScope.getVariableFromImplicitReceivers(name: Name): VariableDescri } return null } + +public fun LexicalScope.addImportScope(importScope: JetScope): LexicalScope { + val fileScope = getFileScope() + val scopeWithAdditionImport = + LexicalChainedScope(fileScope, fileScope.ownerDescriptor, false, null, "Scope with addition import", importScope) + return LexicalScopeWrapper(this, scopeWithAdditionImport) +} + +private class LexicalScopeWrapper(val delegate: LexicalScope, val fileScopeReplace: LexicalScope): LexicalScope by delegate { + override val parent: LexicalScope? by lazy(LazyThreadSafetyMode.NONE) { + if (delegate is FileScope) { + fileScopeReplace + } + else { + LexicalScopeWrapper(delegate.parent!!, fileScopeReplace) + } + } +} \ No newline at end of file