[FIR] Properly generate implicit invoke calls for a.(b)()

^KT-64891 Fixed
This commit is contained in:
Nikolay Lunyak
2024-01-15 12:57:27 +02:00
committed by Space Team
parent 8e6e447d6d
commit 3a36a786d4
9 changed files with 44 additions and 45 deletions
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.fir.lightTree.fir.ValueParameter
import org.jetbrains.kotlin.fir.lightTree.fir.WhenEntry
import org.jetbrains.kotlin.fir.lightTree.fir.addDestructuringStatements
import org.jetbrains.kotlin.fir.references.FirNamedReference
import org.jetbrains.kotlin.fir.references.FirSuperReference
import org.jetbrains.kotlin.fir.references.builder.buildErrorNamedReference
import org.jetbrains.kotlin.fir.references.builder.buildExplicitSuperReference
import org.jetbrains.kotlin.fir.references.builder.buildExplicitThisReference
@@ -651,8 +652,8 @@ class LightTreeRawFirExpressionBuilder(
SUPER_EXPRESSION -> {
superNode = node
}
PARENTHESIZED -> node.getExpressionInParentheses()?.let { process(it) } ?: run {
additionalArgument = getAsFirExpression(node, "Incorrect invoke receiver")
PARENTHESIZED -> if (node.tokenType != TokenType.ERROR_ELEMENT) {
additionalArgument = getAsFirExpression(node.getExpressionInParentheses(), "Incorrect invoke receiver")
}
TYPE_ARGUMENT_LIST -> {
firTypeArguments += declarationBuilder.convertTypeArguments(node, allowedUnderscoredTypeArgument = true)
@@ -680,6 +681,15 @@ class LightTreeRawFirExpressionBuilder(
}
)
superNode != null || additionalArgument?.calleeReference is FirSuperReference -> {
CalleeAndReceiver(
buildErrorNamedReference {
this.source = superNode?.toFirSourceElement() ?: additionalArgument?.calleeReference?.source
diagnostic = ConeSimpleDiagnostic("Super cannot be a callee", DiagnosticKind.SuperNotAllowed)
}
)
}
additionalArgument != null -> {
CalleeAndReceiver(
buildSimpleNamedReference {
@@ -691,16 +701,6 @@ class LightTreeRawFirExpressionBuilder(
)
}
superNode != null -> {
CalleeAndReceiver(
buildErrorNamedReference {
val node = superNode!!
this.source = node.toFirSourceElement()
diagnostic = ConeSimpleDiagnostic("Super cannot be a callee", DiagnosticKind.SuperNotAllowed)
}
)
}
else -> CalleeAndReceiver(
buildErrorNamedReference {
this.source = source
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.fir.expressions.builder.*
import org.jetbrains.kotlin.fir.expressions.impl.FirContractCallBlock
import org.jetbrains.kotlin.fir.expressions.impl.FirSingleExpressionBlock
import org.jetbrains.kotlin.fir.extensions.extensionService
import org.jetbrains.kotlin.fir.references.FirSuperReference
import org.jetbrains.kotlin.fir.references.builder.*
import org.jetbrains.kotlin.fir.scopes.FirScopeProvider
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
@@ -2948,8 +2949,11 @@ open class PsiRawFirBuilder(
calleeExpression: KtExpression?,
defaultSource: KtPsiSourceElement,
): CalleeAndReceiver {
return when (calleeExpression) {
is KtSimpleNameExpression ->
val parenthesizedArgument = (calleeExpression as? KtParenthesizedExpression)?.expression
?.toFirExpression("Incorrect invoke receiver")
return when {
calleeExpression is KtSimpleNameExpression ->
CalleeAndReceiver(
buildSimpleNamedReference {
source = calleeExpression.toFirSourceElement()
@@ -2957,22 +2961,32 @@ open class PsiRawFirBuilder(
}
)
is KtParenthesizedExpression -> splitToCalleeAndReceiver(calleeExpression.expression, defaultSource)
null -> {
calleeExpression is KtSuperExpression || parenthesizedArgument?.calleeReference is FirSuperReference -> {
CalleeAndReceiver(
buildErrorNamedReference {
source = defaultSource
diagnostic = ConeSyntaxDiagnostic("Call has no callee")
source = (calleeExpression as? KtSuperExpression)?.toFirSourceElement()
?: parenthesizedArgument?.calleeReference?.source
diagnostic = ConeSimpleDiagnostic("Super cannot be a callee", DiagnosticKind.SuperNotAllowed)
}
)
}
is KtSuperExpression -> {
parenthesizedArgument != null -> {
CalleeAndReceiver(
buildSimpleNamedReference {
source = defaultSource.fakeElement(KtFakeSourceElementKind.ImplicitInvokeCall)
name = OperatorNameConventions.INVOKE
},
receiverExpression = parenthesizedArgument,
isImplicitInvoke = true
)
}
calleeExpression == null -> {
CalleeAndReceiver(
buildErrorNamedReference {
source = calleeExpression.toFirSourceElement()
diagnostic = ConeSimpleDiagnostic("Super cannot be a callee", DiagnosticKind.SuperNotAllowed)
source = defaultSource
diagnostic = ConeSyntaxDiagnostic("Call has no callee")
}
)
}
@@ -1,5 +1,5 @@
FILE: inBrackets.kt
public? final? fun test(e: ( Int.() -> String )): R|kotlin/Unit| {
lval s: <implicit> = IntegerLiteral(3).e#()
lval ss: <implicit> = IntegerLiteral(3).e#()
lval ss: <implicit> = e#.invoke#(IntegerLiteral(3))
}