From 791a7b1c310f9e78eb89e67a3b8a801b118a9b5f Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 2 Feb 2023 15:04:03 +0200 Subject: [PATCH] [FIR] Extract function type kind for lambda in argument position ^KT-56381 Fixed --- .../resolve/inference/PostponedArguments.kt | 5 ++- .../functionalTypes/inference.fir.txt | 38 +++++++++++++++++++ .../diagnostics/functionalTypes/inference.kt | 17 +++++++++ .../FirPluginDiagnosticTestGenerated.java | 6 +++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.fir.txt create mode 100644 plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt index 213644db40d..ad5b44f1fe5 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/PostponedArguments.kt @@ -63,8 +63,11 @@ fun Candidate.preprocessLambdaArgument( if (expectedType != null) { val parameters = resolvedArgument.parameters + val functionTypeKind = context.session.functionTypeService.extractSingleSpecialKindForFunction(anonymousFunction.symbol) + ?: resolvedArgument.expectedFunctionTypeKind + ?: FunctionTypeKind.Function val lambdaType = createFunctionType( - resolvedArgument.expectedFunctionTypeKind ?: FunctionTypeKind.Function, + functionTypeKind, if (resolvedArgument.coerceFirstParameterToExtensionReceiver) parameters.drop(1) else parameters, resolvedArgument.receiver, resolvedArgument.returnType, diff --git a/plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.fir.txt b/plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.fir.txt new file mode 100644 index 00000000000..8b08f8f32cf --- /dev/null +++ b/plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.fir.txt @@ -0,0 +1,38 @@ +FILE: inference.kt + public final fun kotlin/Unit|> suspendId(f: R|T|): R|T| { + ^suspendId R|/f| + } + public final fun |> composableId(f: R|T|): R|T| { + ^composableId R|/f| + } + public final fun test_1(): R|kotlin/Unit| { + R|/id||>(@R|org/jetbrains/kotlin/fir/plugin/MyComposable|() id@fun (): R|kotlin/Unit| { + ^@id Unit + } + ) + lval f: R|() -> kotlin/Unit| = R|/id#| kotlin/Unit|>(@R|org/jetbrains/kotlin/fir/plugin/MyComposable|() id@fun (): R|kotlin/Unit| { + ^@id Unit + } + ) + R|/composableId||>(composableId@fun (): R|kotlin/Unit| { + ^@composableId Unit + } + ) + R|/composableId||>(@R|org/jetbrains/kotlin/fir/plugin/MyComposable|() composableId@fun (): R|kotlin/Unit| { + ^@composableId Unit + } + ) + R|/suspendId#| kotlin/Unit|>(@R|org/jetbrains/kotlin/fir/plugin/MyComposable|() suspendId@fun (): R|kotlin/Unit| { + ^@suspendId Unit + } + ) + } + public final fun test_2(): R|kotlin/Unit| { + R|/select||>(vararg(@R|org/jetbrains/kotlin/fir/plugin/MyComposable|() select@fun (): R|kotlin/String| { + ^ String(a) + } + , select@fun (): R|kotlin/String| { + ^ String(b) + } + )) + } diff --git a/plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.kt b/plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.kt new file mode 100644 index 00000000000..63621ef7678 --- /dev/null +++ b/plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.kt @@ -0,0 +1,17 @@ +// INFERENCE_HELPERS +import org.jetbrains.kotlin.fir.plugin.MyComposable + +fun Unit> suspendId(f: T): T = f +fun Unit> composableId(f: T): T = f + +fun test_1() { + ")!>id(@MyComposable {}) + val f: () -> Unit = id(@MyComposable {}) // should be an error + ")!>composableId({}) + ")!>composableId(@MyComposable {}) + ")!>suspendId(@MyComposable {}) +} + +fun test_2() { + ")!>select(@MyComposable { "a" }, { "b" }) +} diff --git a/plugins/fir-plugin-prototype/tests-gen/org/jetbrains/kotlin/fir/plugin/runners/FirPluginDiagnosticTestGenerated.java b/plugins/fir-plugin-prototype/tests-gen/org/jetbrains/kotlin/fir/plugin/runners/FirPluginDiagnosticTestGenerated.java index 96ed142b53b..79495e9da36 100644 --- a/plugins/fir-plugin-prototype/tests-gen/org/jetbrains/kotlin/fir/plugin/runners/FirPluginDiagnosticTestGenerated.java +++ b/plugins/fir-plugin-prototype/tests-gen/org/jetbrains/kotlin/fir/plugin/runners/FirPluginDiagnosticTestGenerated.java @@ -61,6 +61,12 @@ public class FirPluginDiagnosticTestGenerated extends AbstractFirPluginDiagnosti runTest("plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/ambigousKinds.kt"); } + @Test + @TestMetadata("inference.kt") + public void testInference() throws Exception { + runTest("plugins/fir-plugin-prototype/testData/diagnostics/functionalTypes/inference.kt"); + } + @Test @TestMetadata("simple.kt") public void testSimple() throws Exception {