KTIJ-25346 [kotlin] Handle typealiased constructors referencing SAM types in K2 Import Optimizer

There are currently no good utility in AA to do the
"SAM constructor -> SAM type" navigation;
see KT-70301.

GitOrigin-RevId: a61ee3949f7892cf19276bf7bfcdc5c9f2c8105f
This commit is contained in:
Roman Golyshev
2024-07-29 18:03:31 +02:00
committed by intellij-monorepo-bot
parent c6391f929b
commit d6d5e3888e
9 changed files with 99 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.idea.references.KDocReference
import org.jetbrains.kotlin.idea.references.KtInvokeFunctionReference
import org.jetbrains.kotlin.idea.references.KtReference
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.withClassId
@@ -131,6 +132,12 @@ internal class UsedReferencesCollector(private val file: KtFile) {
targetClass?.let { resolveTypeAliasedConstructorReference(reference, it) } ?: targetClass
}
target is KaSamConstructorSymbol -> {
val targetClass = findSamClassFor(target)
targetClass?.let { resolveTypeAliasedConstructorReference(reference, it) } ?: targetClass
}
else -> target
}
@@ -166,6 +173,20 @@ internal class UsedReferencesCollector(private val file: KtFile) {
}
}
/**
* Finds the original SAM type by the [samConstructorSymbol].
*
* A workaround for the KT-70301.
*/
private fun KaSession.findSamClassFor(samConstructorSymbol: KaSamConstructorSymbol): KaClassSymbol? {
val samCallableId = samConstructorSymbol.callableId ?: return null
if (samCallableId.isLocal) return null
val samClassId = ClassId.fromString(samCallableId.toString())
return findClass(samClassId)
}
/**
* Takes a [reference] pointing to a typealiased constructor call like `FooAlias()`,
* and [expandedClassSymbol] pointing to the expanded class `Foo`.

View File

@@ -764,6 +764,11 @@ public abstract class FirJvmOptimizeImportsTestGenerated extends AbstractFirJvmO
runTest("../../idea/tests/testData/editor/optimizeImports/common/ResolvedImportAndUnresolvedReference.kt");
}
@TestMetadata("SamConstructor_nestedClass.kt")
public void testSamConstructor_nestedClass() throws Exception {
runTest("../../idea/tests/testData/editor/optimizeImports/common/SamConstructor_nestedClass.kt");
}
@TestMetadata("SeveralClasses.kt")
public void testSeveralClasses() throws Exception {
runTest("../../idea/tests/testData/editor/optimizeImports/common/SeveralClasses.kt");
@@ -794,6 +799,11 @@ public abstract class FirJvmOptimizeImportsTestGenerated extends AbstractFirJvmO
runTest("../../idea/tests/testData/editor/optimizeImports/common/TypeAliasedConstructor_annotation.kt");
}
@TestMetadata("TypeAliasedConstructor_samConstructor.kt")
public void testTypeAliasedConstructor_samConstructor() throws Exception {
runTest("../../idea/tests/testData/editor/optimizeImports/common/TypeAliasedConstructor_samConstructor.kt");
}
@TestMetadata("TypeAliasedConstructor_sameName.kt")
public void testTypeAliasedConstructor_sameName() throws Exception {
runTest("../../idea/tests/testData/editor/optimizeImports/common/TypeAliasedConstructor_sameName.kt");

View File

@@ -501,6 +501,11 @@ public abstract class JsOptimizeImportsTestGenerated extends AbstractJsOptimizeI
runTest("testData/editor/optimizeImports/common/ResolvedImportAndUnresolvedReference.kt");
}
@TestMetadata("SamConstructor_nestedClass.kt")
public void testSamConstructor_nestedClass() throws Exception {
runTest("testData/editor/optimizeImports/common/SamConstructor_nestedClass.kt");
}
@TestMetadata("SeveralClasses.kt")
public void testSeveralClasses() throws Exception {
runTest("testData/editor/optimizeImports/common/SeveralClasses.kt");
@@ -531,6 +536,11 @@ public abstract class JsOptimizeImportsTestGenerated extends AbstractJsOptimizeI
runTest("testData/editor/optimizeImports/common/TypeAliasedConstructor_annotation.kt");
}
@TestMetadata("TypeAliasedConstructor_samConstructor.kt")
public void testTypeAliasedConstructor_samConstructor() throws Exception {
runTest("testData/editor/optimizeImports/common/TypeAliasedConstructor_samConstructor.kt");
}
@TestMetadata("TypeAliasedConstructor_sameName.kt")
public void testTypeAliasedConstructor_sameName() throws Exception {
runTest("testData/editor/optimizeImports/common/TypeAliasedConstructor_sameName.kt");

View File

@@ -769,6 +769,11 @@ public abstract class JvmOptimizeImportsTestGenerated extends AbstractJvmOptimiz
runTest("testData/editor/optimizeImports/common/ResolvedImportAndUnresolvedReference.kt");
}
@TestMetadata("SamConstructor_nestedClass.kt")
public void testSamConstructor_nestedClass() throws Exception {
runTest("testData/editor/optimizeImports/common/SamConstructor_nestedClass.kt");
}
@TestMetadata("SeveralClasses.kt")
public void testSeveralClasses() throws Exception {
runTest("testData/editor/optimizeImports/common/SeveralClasses.kt");
@@ -799,6 +804,11 @@ public abstract class JvmOptimizeImportsTestGenerated extends AbstractJvmOptimiz
runTest("testData/editor/optimizeImports/common/TypeAliasedConstructor_annotation.kt");
}
@TestMetadata("TypeAliasedConstructor_samConstructor.kt")
public void testTypeAliasedConstructor_samConstructor() throws Exception {
runTest("testData/editor/optimizeImports/common/TypeAliasedConstructor_samConstructor.kt");
}
@TestMetadata("TypeAliasedConstructor_sameName.kt")
public void testTypeAliasedConstructor_sameName() throws Exception {
runTest("testData/editor/optimizeImports/common/TypeAliasedConstructor_sameName.kt");

View File

@@ -0,0 +1,13 @@
package test
import test.Outer.Nested
class Outer {
fun interface Nested {
fun foo()
}
}
fun usage() {
Nested {}
}

View File

@@ -0,0 +1,13 @@
package test
import test.Outer.Nested
class Outer {
fun interface Nested {
fun foo()
}
}
fun usage() {
Nested {}
}

View File

@@ -0,0 +1,7 @@
package dependency
fun interface Foo {
fun bar()
}
typealias TypeAliasedFoo = Foo

View File

@@ -0,0 +1,8 @@
package test
import dependency.Foo
import dependency.TypeAliasedFoo
fun usage() {
TypeAliasedFoo {}
}

View File

@@ -0,0 +1,7 @@
package test
import dependency.TypeAliasedFoo
fun usage() {
TypeAliasedFoo {}
}