K2: Adjust renaming on import for current package with K1 rules

Namely, once a file in a package `foo` has import `foo.bar as baz`,
auto imported `bar` from the package name becomes inivisible in the file

^KT-54854 Fixed
This commit is contained in:
Denis.Zharkov
2023-06-22 18:37:57 +02:00
committed by Space Team
parent 5d7ac18778
commit 60f09f6512
11 changed files with 101 additions and 10 deletions
@@ -14202,6 +14202,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt"); runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt");
} }
@Test
@TestMetadata("ImportFromCurrentWithDifferentNameComplex.kt")
public void testImportFromCurrentWithDifferentNameComplex() throws Exception {
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentNameComplex.kt");
}
@Test @Test
@TestMetadata("ImportFromObject.kt") @TestMetadata("ImportFromObject.kt")
public void testImportFromObject() throws Exception { public void testImportFromObject() throws Exception {
@@ -14202,6 +14202,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt"); runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt");
} }
@Test
@TestMetadata("ImportFromCurrentWithDifferentNameComplex.kt")
public void testImportFromCurrentWithDifferentNameComplex() throws Exception {
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentNameComplex.kt");
}
@Test @Test
@TestMetadata("ImportFromObject.kt") @TestMetadata("ImportFromObject.kt")
public void testImportFromObject() throws Exception { public void testImportFromObject() throws Exception {
@@ -14202,6 +14202,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt"); runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt");
} }
@Test
@TestMetadata("ImportFromCurrentWithDifferentNameComplex.kt")
public void testImportFromCurrentWithDifferentNameComplex() throws Exception {
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentNameComplex.kt");
}
@Test @Test
@TestMetadata("ImportFromObject.kt") @TestMetadata("ImportFromObject.kt")
public void testImportFromObject() throws Exception { public void testImportFromObject() throws Exception {
@@ -14208,6 +14208,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt"); runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt");
} }
@Test
@TestMetadata("ImportFromCurrentWithDifferentNameComplex.kt")
public void testImportFromCurrentWithDifferentNameComplex() throws Exception {
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentNameComplex.kt");
}
@Test @Test
@TestMetadata("ImportFromObject.kt") @TestMetadata("ImportFromObject.kt")
public void testImportFromObject() throws Exception { public void testImportFromObject() throws Exception {
@@ -23,17 +23,23 @@ import org.jetbrains.kotlin.utils.SmartList
class FirPackageMemberScope( class FirPackageMemberScope(
val fqName: FqName, val fqName: FqName,
val session: FirSession, val session: FirSession,
private val symbolProvider: FirSymbolProvider = session.symbolProvider private val symbolProvider: FirSymbolProvider = session.symbolProvider,
excludedImportNames: Collection<FqName>? = null,
) : FirScope() { ) : FirScope() {
private val classifierCache: MutableMap<Name, FirClassifierSymbol<*>?> = mutableMapOf() private val classifierCache: MutableMap<Name, FirClassifierSymbol<*>?> = mutableMapOf()
private val functionCache: MutableMap<Name, List<FirNamedFunctionSymbol>> = mutableMapOf() private val functionCache: MutableMap<Name, List<FirNamedFunctionSymbol>> = mutableMapOf()
private val propertyCache: MutableMap<Name, List<FirPropertySymbol>> = mutableMapOf() private val propertyCache: MutableMap<Name, List<FirPropertySymbol>> = mutableMapOf()
private val excludedNames by lazy {
excludedImportNames?.mapNotNullTo(mutableSetOf()) { if (it.parent() == fqName) it.shortName() else null }.orEmpty()
}
override fun processClassifiersByNameWithSubstitution( override fun processClassifiersByNameWithSubstitution(
name: Name, name: Name,
processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit processor: (FirClassifierSymbol<*>, ConeSubstitutor) -> Unit
) { ) {
if (name.asString().isEmpty()) return if (name.asString().isEmpty()) return
if (name in excludedNames) return
val symbol = classifierCache.getOrPut(name) { val symbol = classifierCache.getOrPut(name) {
val unambiguousFqName = ClassId(fqName, name) val unambiguousFqName = ClassId(fqName, name)
@@ -46,6 +52,8 @@ class FirPackageMemberScope(
} }
override fun processFunctionsByName(name: Name, processor: (FirNamedFunctionSymbol) -> Unit) { override fun processFunctionsByName(name: Name, processor: (FirNamedFunctionSymbol) -> Unit) {
if (name in excludedNames) return
val symbols = functionCache.getOrPut(name) { val symbols = functionCache.getOrPut(name) {
symbolProvider.getTopLevelFunctionSymbols(fqName, name) symbolProvider.getTopLevelFunctionSymbols(fqName, name)
} }
@@ -55,6 +63,8 @@ class FirPackageMemberScope(
} }
override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) { override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
if (name in excludedNames) return
val symbols = propertyCache.getOrPut(name) { val symbols = propertyCache.getOrPut(name) {
symbolProvider.getTopLevelPropertySymbols(fqName, name) symbolProvider.getTopLevelPropertySymbols(fqName, name)
} }
@@ -61,7 +61,7 @@ private fun doCreateImportingScopes(
FirDefaultSimpleImportingScope(session, scopeSession, priority = DefaultImportPriority.HIGH) FirDefaultSimpleImportingScope(session, scopeSession, priority = DefaultImportPriority.HIGH)
}, },
scopeSession.getOrBuild(file.packageFqName, PACKAGE_MEMBER) { scopeSession.getOrBuild(file.packageFqName, PACKAGE_MEMBER) {
FirPackageMemberScope(file.packageFqName, session) FirPackageMemberScope(file.packageFqName, session, excludedImportNames = excludedImportNames)
}, },
// TODO: explicit simple importing scope should have highest priority (higher than inner scopes added in process) // TODO: explicit simple importing scope should have highest priority (higher than inner scopes added in process)
FirExplicitSimpleImportingScope(file.imports, session, scopeSession) FirExplicitSimpleImportingScope(file.imports, session, scopeSession)
@@ -1,8 +0,0 @@
package a
import a.A as ER
interface A {
val a: A
val b: ER
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
package a package a
import a.A as ER import a.A as ER
@@ -0,0 +1,29 @@
// ISSUE: KT-54854
package a
import a.A as ER
import a.x as y
import a.foo as bar
val x: Int = 1
fun foo(): Int = 1
class A
interface B {
val a: <!UNRESOLVED_REFERENCE!>A<!>
val b: ER
}
fun main() {
<!UNRESOLVED_REFERENCE!>A<!>()
ER()
a.A()
<!UNRESOLVED_REFERENCE!>x<!> + 1
y + 1
<!UNRESOLVED_REFERENCE!>foo<!>() + 1
bar() + 1
}
@@ -0,0 +1,29 @@
// ISSUE: KT-54854
package a
import a.A as ER
import a.x as y
import a.foo as bar
val x: Int = 1
fun foo(): Int = 1
class A
interface B {
val a: <!UNRESOLVED_REFERENCE!>A<!>
val b: ER
}
fun main() {
<!UNRESOLVED_REFERENCE!>A<!>()
ER()
a.A()
<!UNRESOLVED_REFERENCE!>x<!> <!DEBUG_INFO_MISSING_UNRESOLVED!>+<!> 1
y + 1
<!UNRESOLVED_REFERENCE!>foo<!>() <!DEBUG_INFO_MISSING_UNRESOLVED!>+<!> 1
bar() + 1
}
@@ -14208,6 +14208,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt"); runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentName.kt");
} }
@Test
@TestMetadata("ImportFromCurrentWithDifferentNameComplex.kt")
public void testImportFromCurrentWithDifferentNameComplex() throws Exception {
runTest("compiler/testData/diagnostics/tests/imports/ImportFromCurrentWithDifferentNameComplex.kt");
}
@Test @Test
@TestMetadata("ImportFromObject.kt") @TestMetadata("ImportFromObject.kt")
public void testImportFromObject() throws Exception { public void testImportFromObject() throws Exception {