From 22cbb8720a07eb61979f5d60ba090722700f132e Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 31 Mar 2021 13:40:03 +0300 Subject: [PATCH] [FIR] Fix computing labels of anonymous functions (not lambdas) --- .../kotlin/fir/lightTree/converter/BaseConverter.kt | 4 ++++ .../fir/lightTree/converter/DeclarationsConverter.kt | 3 ++- .../src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt | 8 ++++++-- .../rawBuilder/expressions/lambdaAndAnonymousFunction.txt | 2 +- .../psi2fir/testData/rawBuilder/expressions/locals.txt | 2 +- .../box/functions/localReturnInsideFunctionExpression.kt | 3 +-- 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt index c6dd15735af..cb1a714b309 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.fir.types.builder.buildImplicitTypeRef import org.jetbrains.kotlin.lexer.KtToken import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.stubs.elements.KtFunctionElementType import kotlin.contracts.ExperimentalContracts abstract class BaseConverter( @@ -54,6 +55,9 @@ abstract class BaseConverter( } override fun LighterASTNode.getLabelName(): String? { + if (tokenType == KtNodeTypes.FUN) { + return getParent()?.getLabelName() + } this.forEachChildren { when (it.tokenType) { KtNodeTypes.LABEL_QUALIFIER -> return it.asText.replaceFirst("@", "") diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index c2456e66307..2a4375ff562 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -1327,7 +1327,8 @@ class DeclarationsConverter( val isLocal = !(parentNode?.tokenType == KT_FILE || parentNode?.tokenType == CLASS_BODY) val target: FirFunctionTarget val functionBuilder = if (identifier == null && isLocal) { - target = FirFunctionTarget(labelName = functionDeclaration.getLabelName(), isLambda = false) + val labelName = functionDeclaration.getLabelName() ?: context.calleeNamesForLambda.lastOrNull()?.identifier + target = FirFunctionTarget(labelName = labelName, isLambda = false) FirAnonymousFunctionBuilder().apply { source = functionDeclaration.toFirSourceElement() receiverTypeRef = receiverType diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index da8e9cbe50d..4db4e94326c 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -99,7 +99,11 @@ open class RawFirBuilder( } override fun PsiElement.getLabelName(): String? { - return (this as? KtExpressionWithLabel)?.getLabelName() + return when (this) { + is KtExpressionWithLabel -> getLabelName() + is KtNamedFunction -> parent.getLabelName() + else -> null + } } override fun PsiElement.getExpressionInParentheses(): PsiElement? { @@ -967,7 +971,7 @@ open class RawFirBuilder( receiverTypeRef = receiverType symbol = FirAnonymousFunctionSymbol() isLambda = false - labelName = function.getLabelName() + labelName = function.getLabelName() ?: context.calleeNamesForLambda.lastOrNull()?.identifier } } else { FirSimpleFunctionBuilder().apply { diff --git a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/expressions/lambdaAndAnonymousFunction.txt b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/expressions/lambdaAndAnonymousFunction.txt index c537c63b82a..1362a17ba7e 100644 --- a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/expressions/lambdaAndAnonymousFunction.txt +++ b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/expressions/lambdaAndAnonymousFunction.txt @@ -14,7 +14,7 @@ FILE: lambdaAndAnonymousFunction.kt } public? final? fun test_2(): R|kotlin/Unit| { run#(fun (): Int { - ^ IntegerLiteral(1) + ^@run IntegerLiteral(1) } ) } diff --git a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/expressions/locals.txt b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/expressions/locals.txt index 248137b3959..f4ea94c861e 100644 --- a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/expressions/locals.txt +++ b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/expressions/locals.txt @@ -31,7 +31,7 @@ FILE: locals.kt } .foo#() ^withLocals sum#(code#, Local#(IntegerLiteral(1)).diff#(), fun (x: Int, y: Int): { - ^ x#.plus#(y#) + ^@sum x#.plus#(y#) } ) } diff --git a/compiler/testData/codegen/box/functions/localReturnInsideFunctionExpression.kt b/compiler/testData/codegen/box/functions/localReturnInsideFunctionExpression.kt index 93aaf434710..508d7716e3e 100644 --- a/compiler/testData/codegen/box/functions/localReturnInsideFunctionExpression.kt +++ b/compiler/testData/codegen/box/functions/localReturnInsideFunctionExpression.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun simple() = fun (): Boolean { return true } fun withLabel() = l@ fun (): Boolean { return@l true } @@ -8,4 +7,4 @@ fun box(): String { if (!withLabel()()) return "Test withLabel failed" return "OK" -} \ No newline at end of file +}