From 24d8aee2bb6822d59fabf3d3a625677b843dff00 Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Fri, 24 Jul 2020 12:21:30 +0300 Subject: [PATCH] Prioritize resolution anchors during resolution When required dependency is present both in source dependencies and in library dependencies, the former one should be selected. #KT-24309 --- .../resolve/QualifiedExpressionResolver.kt | 6 +++--- .../kotlin/descriptors/findClassInModule.kt | 17 ++++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt index e30e64f6730..aca160c93c7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt @@ -237,9 +237,9 @@ class QualifiedExpressionResolver(val languageVersionSettings: LanguageVersionSe if (!languageVersionSettings.isLibraryToSourceAnalysisEnabled) return primaryImportingScope val resolutionAnchor = moduleDescriptor.getResolutionAnchorIfAny() ?: return primaryImportingScope - val secondaryImportingScope = processReferenceInContextOf(resolutionAnchor) ?: return primaryImportingScope - if (primaryImportingScope == null) return secondaryImportingScope - return CompositePrioritizedImportingScope(primaryImportingScope, secondaryImportingScope) + val anchorImportingScope = processReferenceInContextOf(resolutionAnchor) ?: return primaryImportingScope + if (primaryImportingScope == null) return anchorImportingScope + return CompositePrioritizedImportingScope(anchorImportingScope, primaryImportingScope) } private fun doProcessImportReference( diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/findClassInModule.kt b/core/descriptors/src/org/jetbrains/kotlin/descriptors/findClassInModule.kt index 4dd0e293f09..5a32f5fd964 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/findClassInModule.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/findClassInModule.kt @@ -18,31 +18,30 @@ package org.jetbrains.kotlin.descriptors import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.resolve.RESOLUTION_ANCHOR_PROVIDER_CAPABILITY import org.jetbrains.kotlin.resolve.getResolutionAnchorIfAny -fun ModuleDescriptor.findClassifierAcrossModuleDependencies(classId: ClassId): ClassifierDescriptor? = withAnchorFallback { +fun ModuleDescriptor.findClassifierAcrossModuleDependencies(classId: ClassId): ClassifierDescriptor? = withResolutionAnchor { val packageViewDescriptor = getPackage(classId.packageFqName) val segments = classId.relativeClassName.pathSegments() val topLevelClass = packageViewDescriptor.memberScope.getContributedClassifier( segments.first(), NoLookupLocation.FROM_DESERIALIZATION - ) ?: return@withAnchorFallback null + ) ?: return@withResolutionAnchor null var result = topLevelClass for (name in segments.subList(1, segments.size)) { - if (result !is ClassDescriptor) return@withAnchorFallback null + if (result !is ClassDescriptor) return@withResolutionAnchor null result = result.unsubstitutedInnerClassesScope .getContributedClassifier(name, NoLookupLocation.FROM_DESERIALIZATION) as? ClassDescriptor - ?: return@withAnchorFallback null + ?: return@withResolutionAnchor null } - return@withAnchorFallback result + return@withResolutionAnchor result } -private inline fun ModuleDescriptor.withAnchorFallback( - crossinline doSearch: ModuleDescriptor.() -> ClassifierDescriptor? +private inline fun ModuleDescriptor.withResolutionAnchor( + crossinline doSearch: ModuleDescriptor.() -> ClassifierDescriptor? ): ClassifierDescriptor? { val anchor = getResolutionAnchorIfAny() - return if (anchor == null) doSearch() else doSearch() ?: anchor.doSearch() + return if (anchor == null) doSearch() else anchor.doSearch() ?: doSearch() } fun ModuleDescriptor.findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor? =