diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/scopes/LexicalChainedScope.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/scopes/LexicalChainedScope.kt index d0b851a8631..eedc723c805 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/scopes/LexicalChainedScope.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/scopes/LexicalChainedScope.kt @@ -21,7 +21,7 @@ import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.scopes.utils.takeSnapshot -import org.jetbrains.kotlin.util.collectionUtils.getFirstMatch +import org.jetbrains.kotlin.util.collectionUtils.getFirstClassifierDiscriminateHeaders import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes import org.jetbrains.kotlin.utils.Printer @@ -40,7 +40,7 @@ class LexicalChainedScope @JvmOverloads constructor( override fun getContributedDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean) = getFromAllScopes(memberScopes) { it.getContributedDescriptors() } - override fun getContributedClassifier(name: Name, location: LookupLocation) = getFirstMatch(memberScopes) { it.getContributedClassifier(name, location) } + override fun getContributedClassifier(name: Name, location: LookupLocation) = getFirstClassifierDiscriminateHeaders(memberScopes) { it.getContributedClassifier(name, location) } override fun getContributedVariables(name: Name, location: LookupLocation) = getFromAllScopes(memberScopes) { it.getContributedVariables(name, location) } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/JvmPackageScope.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/JvmPackageScope.kt index c9ef9c1aa81..e44a8603b62 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/JvmPackageScope.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/JvmPackageScope.kt @@ -28,7 +28,7 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.storage.getValue -import org.jetbrains.kotlin.util.collectionUtils.getFirstMatch +import org.jetbrains.kotlin.util.collectionUtils.getFirstClassifierDiscriminateHeaders import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.toReadOnlyList @@ -52,7 +52,7 @@ class JvmPackageScope( val javaClassifier = javaScope.getContributedClassifier(name, location) if (javaClassifier != null) return javaClassifier - return getFirstMatch(kotlinScopes) { it.getContributedClassifier(name, location) } + return getFirstClassifierDiscriminateHeaders(kotlinScopes) { it.getContributedClassifier(name, location) } } override fun getContributedVariables(name: Name, location: LookupLocation): Collection { diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/ChainedMemberScope.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/ChainedMemberScope.kt index 74bc02ae28a..c085613d6ab 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/ChainedMemberScope.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/scopes/ChainedMemberScope.kt @@ -21,7 +21,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.util.collectionUtils.getFirstMatch +import org.jetbrains.kotlin.util.collectionUtils.getFirstClassifierDiscriminateHeaders import org.jetbrains.kotlin.util.collectionUtils.getFromAllScopes import org.jetbrains.kotlin.utils.Printer @@ -30,7 +30,7 @@ class ChainedMemberScope( private val scopes: List ) : MemberScope { override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? - = getFirstMatch(scopes) { it.getContributedClassifier(name, location) } + = getFirstClassifierDiscriminateHeaders(scopes) { it.getContributedClassifier(name, location) } override fun getContributedVariables(name: Name, location: LookupLocation): Collection = getFromAllScopes(scopes) { it.getContributedVariables(name, location) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/util/scopeUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/util/scopeUtils.kt index 92f8493fa5b..20c89c1fc73 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/util/scopeUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/util/scopeUtils.kt @@ -16,6 +16,8 @@ package org.jetbrains.kotlin.util.collectionUtils +import org.jetbrains.kotlin.descriptors.ClassifierDescriptor +import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters import java.util.* /** @@ -70,11 +72,20 @@ inline fun getFromAllScopes(firstScope: Scope, restScopes: List getFirstMatch(scopes: List, callback: (Scope) -> T?): T? { +inline fun getFirstClassifierDiscriminateHeaders(scopes: List, callback: (Scope) -> T?): T? { // NOTE: This is performance-sensitive; please don't replace with map().firstOrNull() + var result: T? = null for (scope in scopes) { - val result = callback(scope) - if (result != null) return result + val newResult = callback(scope) + if (newResult != null) { + if (newResult is ClassifierDescriptorWithTypeParameters && newResult.isHeader) { + if (result == null) result = newResult + } + // this class is Impl or usual class + else { + return newResult + } + } } - return null + return result }