diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt index 2ae266ff383..d14c6b2d707 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.psi.psiUtil.referenceExpression import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.ModifierCheckerCore @@ -483,8 +484,7 @@ class PSICallResolver( ): PSIKotlinCallArgument { val builtIns = outerCallContext.scope.ownerDescriptor.builtIns val parseErrorArgument = ParseErrorKotlinCallArgument(valueArgument, startDataFlowInfo, builtIns) - val ktExpression = KtPsiUtil.deparenthesize(valueArgument.getArgumentExpression()) ?: - return parseErrorArgument + val ktExpression = extractArgumentExpression(outerCallContext, valueArgument) ?: parseErrorArgument val argumentName = valueArgument.getArgumentName()?.asName @@ -499,6 +499,7 @@ class PSICallResolver( if (ktExpression.hasBlockBody()) builtIns.unitType else null FunctionExpressionImpl(outerCallContext, valueArgument, startDataFlowInfo, argumentName, ktExpression, receiverType, parametersTypes, returnType) } + else -> null } if (lambdaArgument != null) { @@ -563,6 +564,19 @@ class PSICallResolver( return createSimplePSICallArgument(context, valueArgument, typeInfo) ?: parseErrorArgument } + private fun extractArgumentExpression(outerCallContext: BasicCallResolutionContext, valueArgument: ValueArgument): KtExpression? { + val argumentExpression = valueArgument.getArgumentExpression() ?: return null + + val ktExpression = ArgumentTypeResolver.getFunctionLiteralArgumentIfAny(argumentExpression, outerCallContext) ?: + ArgumentTypeResolver.getCallableReferenceExpressionIfAny(argumentExpression, outerCallContext) ?: + KtPsiUtil.deparenthesize(argumentExpression) + + return when (ktExpression) { + is KtFunctionLiteral -> ktExpression.getParentOfType(true) + else -> ktExpression + } + } + private fun checkNoSpread(context: BasicCallResolutionContext, valueArgument: ValueArgument) { valueArgument.getSpreadElement()?.let { context.trace.report(Errors.SPREAD_OF_LAMBDA_OR_CALLABLE_REFERENCE.on(it)) diff --git a/compiler/testData/codegen/box/regressions/functionLiteralAsLastExpressionInBlock.kt b/compiler/testData/codegen/box/regressions/functionLiteralAsLastExpressionInBlock.kt new file mode 100644 index 00000000000..13ed03338d2 --- /dev/null +++ b/compiler/testData/codegen/box/regressions/functionLiteralAsLastExpressionInBlock.kt @@ -0,0 +1,8 @@ +fun box(): String { + val p: (String) -> Boolean = if (true) { + { true } + } else { + { true } + } + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index f07b20c6115..810442954fa 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -16598,6 +16598,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("functionLiteralAsLastExpressionInBlock.kt") + public void testFunctionLiteralAsLastExpressionInBlock() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/functionLiteralAsLastExpressionInBlock.kt"); + doTest(fileName); + } + @TestMetadata("generic.kt") public void testGeneric() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/generic.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index e8501248308..ec6bd92cd46 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -16598,6 +16598,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("functionLiteralAsLastExpressionInBlock.kt") + public void testFunctionLiteralAsLastExpressionInBlock() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/functionLiteralAsLastExpressionInBlock.kt"); + doTest(fileName); + } + @TestMetadata("generic.kt") public void testGeneric() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/generic.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index f35172d5d72..ba614909224 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -16598,6 +16598,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("functionLiteralAsLastExpressionInBlock.kt") + public void testFunctionLiteralAsLastExpressionInBlock() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/functionLiteralAsLastExpressionInBlock.kt"); + doTest(fileName); + } + @TestMetadata("generic.kt") public void testGeneric() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/generic.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 86dd4b7c84f..714548953aa 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -20246,6 +20246,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("functionLiteralAsLastExpressionInBlock.kt") + public void testFunctionLiteralAsLastExpressionInBlock() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/functionLiteralAsLastExpressionInBlock.kt"); + doTest(fileName); + } + @TestMetadata("generic.kt") public void testGeneric() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/generic.kt");