FIR/UAST: handle nullable receiver parameter of extension function as anchor

^KTIJ-25925 Fixed

GitOrigin-RevId: 46bbe1b0ee60f25039911c16b22b85e88d9abbc3
This commit is contained in:
Jinseong Jeon
2023-06-21 16:29:16 +00:00
committed by intellij-monorepo-bot
parent d09828ce08
commit 2d036b92c0
4 changed files with 37 additions and 1 deletions
@@ -79,7 +79,9 @@ abstract class AbstractKotlinUVariable(
is KtNamedDeclaration -> sourcePsi.nameIdentifier
is KtTypeReference -> sourcePsi.typeElement?.let {
// receiver param in extension function
(it as? KtUserType)?.referenceExpression?.getIdentifier() ?: it
// Unwrap the type if the receiver param is nullable
val typeElement = (it as? KtNullableType)?.innerType ?: it
(typeElement as? KtUserType)?.referenceExpression?.getIdentifier() ?: it
} ?: sourcePsi
is KtNameReferenceExpression -> sourcePsi.getReferencedNameElement()
is KtBinaryExpression, is KtCallExpression -> null // e.g. `foo("Lorem ipsum") ?: foo("dolor sit amet")`
@@ -640,4 +640,30 @@ interface UastApiFixtureTestBase : UastPluginSelection {
})
}
fun checkIdentifierOfNullableExtensionReceiver(myFixture: JavaCodeInsightTestFixture) {
myFixture.configureByText(
"main.kt", """
enum class SortOrder {
ASCENDING, DESCENDING, UNSORTED
}
fun <C> Comparator<C>?.withOrder(sortOrder: SortOrder): Comparator<C>? =
this?.let {
when (sortOrder) {
SortOrder.ASCENDING -> it
SortOrder.DESCENDING -> it.reversed()
SortOrder.UNSORTED -> null
}
}
""".trimIndent()
)
val uFile = myFixture.file.toUElement()!!
val withOrder = uFile.findElementByTextFromPsi<UMethod>("withOrder", strict = false)
.orFail("can't convert extension function: withOrder")
val extensionReceiver = withOrder.uastParameters.first()
val identifier = extensionReceiver.uastAnchor as? UIdentifier
TestCase.assertNotNull(identifier)
}
}
@@ -82,4 +82,8 @@ class FirUastApiFixtureTest : KotlinLightCodeInsightFixtureTestCase(), UastApiFi
doCheck("LambdaImplicitParameters", ::checkLambdaImplicitParameters)
}
fun testIdentifierOfNullableExtensionReceiver() {
checkIdentifierOfNullableExtensionReceiver(myFixture)
}
}
@@ -75,4 +75,8 @@ class FE1UastApiFixtureTest : KotlinLightCodeInsightFixtureTestCase(), UastApiFi
checkLambdaImplicitParameters(myFixture)
}
fun testIdentifierOfNullableExtensionReceiver() {
checkIdentifierOfNullableExtensionReceiver(myFixture)
}
}