From f37dc279740ec8d3fda71859e832d81a4b121504 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Fri, 30 Jun 2023 12:16:13 +0300 Subject: [PATCH] K2: Fix incorrect UNRESOLVED_REFERENCE after aliased import in other file The problem is actually caused by 60f09f6512ea0b1cc1411a731bb29279b97c4bf5 It happened because we've been caching package scope using FqName key, so once one file contains aliased import it would work like other file contains it, too. So, the fix is adding excludedNames to the key, too. ^KT-59789 Fixed --- .../fir/scopes/impl/FirPackageMemberScope.kt | 6 +---- .../kotlin/fir/scopes/ImportingScopes.kt | 17 +++++++++++--- .../renamedImportInDifferentFile.fir.kt | 22 ------------------- .../imports/renamedImportInDifferentFile.kt | 1 + 4 files changed, 16 insertions(+), 30 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/imports/renamedImportInDifferentFile.fir.kt diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirPackageMemberScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirPackageMemberScope.kt index afa112a3215..84999235e2f 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirPackageMemberScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirPackageMemberScope.kt @@ -24,16 +24,12 @@ class FirPackageMemberScope( val fqName: FqName, val session: FirSession, private val symbolProvider: FirSymbolProvider = session.symbolProvider, - excludedImportNames: Collection? = null, + private val excludedNames: Set = emptySet(), ) : FirScope() { private val classifierCache: MutableMap?> = mutableMapOf() private val functionCache: MutableMap> = mutableMapOf() private val propertyCache: MutableMap> = mutableMapOf() - private val excludedNames by lazy { - excludedImportNames?.mapNotNullTo(mutableSetOf()) { if (it.parent() == fqName) it.shortName() else null }.orEmpty() - } - override fun processClassifiersByNameWithSubstitution( name: Name, processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/ImportingScopes.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/ImportingScopes.kt index ca4a5670306..276c2f18cb9 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/ImportingScopes.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/ImportingScopes.kt @@ -18,7 +18,6 @@ import org.jetbrains.kotlin.name.FqName private val ALL_IMPORTS = scopeSessionKey() private val DEFAULT_STAR_IMPORT = scopeSessionKey() private val DEFAULT_SIMPLE_IMPORT = scopeSessionKey() - private data class DefaultStarImportKey(val priority: DefaultImportPriority, val excludedImportNames: Set) fun createImportingScopes( @@ -42,6 +41,12 @@ private fun doCreateImportingScopes( file.lazyResolveToPhase(FirResolvePhase.IMPORTS) val excludedImportNames = file.imports.filter { it.aliasName != null }.mapNotNullTo(hashSetOf()) { it.importedFqName }.ifEmpty { emptySet() } + + val excludedNamesInPackage = + excludedImportNames.mapNotNullTo(mutableSetOf()) { + if (it.parent() == file.packageFqName) it.shortName() else null + } + return listOf( scopeSession.getOrBuild(DefaultStarImportKey(DefaultImportPriority.LOW, excludedImportNames), DEFAULT_STAR_IMPORT) { FirDefaultStarImportingScope(session, scopeSession, DefaultImportPriority.LOW, excludedImportNames) @@ -60,8 +65,14 @@ private fun doCreateImportingScopes( scopeSession.getOrBuild(DefaultImportPriority.HIGH, DEFAULT_SIMPLE_IMPORT) { FirDefaultSimpleImportingScope(session, scopeSession, priority = DefaultImportPriority.HIGH) }, - scopeSession.getOrBuild(file.packageFqName, PACKAGE_MEMBER) { - FirPackageMemberScope(file.packageFqName, session, excludedImportNames = excludedImportNames) + when { + excludedNamesInPackage.isEmpty() -> + // Supposed to be the most common branch, so we cache it + scopeSession.getOrBuild(file.packageFqName, PACKAGE_MEMBER) { + FirPackageMemberScope(file.packageFqName, session, excludedNames = emptySet()) + } + else -> + FirPackageMemberScope(file.packageFqName, session, excludedNames = excludedNamesInPackage) }, // TODO: explicit simple importing scope should have highest priority (higher than inner scopes added in process) FirExplicitSimpleImportingScope(file.imports, session, scopeSession) diff --git a/compiler/testData/diagnostics/tests/imports/renamedImportInDifferentFile.fir.kt b/compiler/testData/diagnostics/tests/imports/renamedImportInDifferentFile.fir.kt deleted file mode 100644 index 862725a662d..00000000000 --- a/compiler/testData/diagnostics/tests/imports/renamedImportInDifferentFile.fir.kt +++ /dev/null @@ -1,22 +0,0 @@ -// ISSUE: KT-59789 -// FILE: a/A.java -package a; -public interface A {} - -// FILE: first.kt -package b -import b.DependencyAnalyzerDependency as Dependency - -fun foo(d: Dependency) {} - -// FILE: main.kt -package b - -import a.A -interface DependencyAnalyzerDependency : A { - val parent: DependencyAnalyzerDependency? // Should be resolved -} - -fun bar(d: DependencyAnalyzerDependency) { - foo(d) -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/imports/renamedImportInDifferentFile.kt b/compiler/testData/diagnostics/tests/imports/renamedImportInDifferentFile.kt index 75242c6bab8..27ee1dcbec7 100644 --- a/compiler/testData/diagnostics/tests/imports/renamedImportInDifferentFile.kt +++ b/compiler/testData/diagnostics/tests/imports/renamedImportInDifferentFile.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // ISSUE: KT-59789 // FILE: a/A.java package a;