diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolution.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolution.kt index fcba7c4f507..652aac3af8e 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolution.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/CallableReferenceResolution.kt @@ -315,7 +315,7 @@ class CallableReferencesCandidateFactory( return callComponents.reflectionTypes.getKFunctionType( Annotations.EMPTY, null, argumentsAndReceivers, null, - returnType, descriptor.builtIns, isSuspend = false + returnType, descriptor.builtIns, descriptor.isSuspend ) to defaults } else -> error("Unsupported descriptor type: $descriptor") @@ -347,17 +347,22 @@ fun extractInputOutputTypesFromCallableReferenceExpectedType(expectedType: Unwra if (expectedType == null) return null return when { - expectedType.isFunctionType -> + expectedType.isFunctionType || expectedType.isSuspendFunctionType -> extractInputOutputTypesFromFunctionType(expectedType) ReflectionTypes.isBaseTypeForNumberedReferenceTypes(expectedType) -> InputOutputTypes(emptyList(), expectedType.arguments.single().type.unwrap()) - ReflectionTypes.isNumberedKFunctionOrKSuspendFunction(expectedType) -> { + ReflectionTypes.isNumberedKFunction(expectedType) -> { val functionFromSupertype = expectedType.immediateSupertypes().first { it.isFunctionType }.unwrap() extractInputOutputTypesFromFunctionType(functionFromSupertype) } + ReflectionTypes.isNumberedKSuspendFunction(expectedType) -> { + val kSuspendFunctionType = expectedType.immediateSupertypes().first { it.isSuspendFunctionType }.unwrap() + extractInputOutputTypesFromFunctionType(kSuspendFunctionType) + } + ReflectionTypes.isNumberedKPropertyOrKMutablePropertyType(expectedType) -> { val functionFromSupertype = expectedType.supertypes().first { it.isFunctionType }.unwrap() extractInputOutputTypesFromFunctionType(functionFromSupertype) diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.kt new file mode 100644 index 00000000000..13ddbd6fb28 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.kt @@ -0,0 +1,22 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER +// !USE_EXPERIMENTAL: kotlin.Experimental + +import kotlin.reflect.KSuspendFunction0 +import kotlin.reflect.KSuspendFunction1 +import kotlin.reflect.KFunction0 +import kotlin.reflect.KFunction1 + +fun test0(serialize: KSuspendFunction0) {} +fun test1(serialize: KSuspendFunction1) {} + +suspend fun foo() {} +suspend fun bar(x: Int) {} + +fun test() { + test0(::foo) + test1(::foo) + + test0(::bar) + test1(::bar) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.txt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.txt new file mode 100644 index 00000000000..4bc6b352130 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.txt @@ -0,0 +1,7 @@ +package + +public suspend fun bar(/*0*/ x: kotlin.Int): kotlin.Unit +public suspend fun foo(): kotlin.Unit +public fun test(): kotlin.Unit +public fun test0(/*0*/ serialize: kotlin.reflect.KSuspendFunction0): kotlin.Unit +public fun test1(/*0*/ serialize: kotlin.reflect.KSuspendFunction1): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index 7a638c63175..db01bd07110 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -1802,6 +1802,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/applyInsideCoroutine.kt"); } + @TestMetadata("callableReferenceToASuspendFunction.kt") + public void testCallableReferenceToASuspendFunction() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.kt"); + } + @TestMetadata("chainCallWithExtensionExplicitTypes.kt") public void testChainCallWithExtensionExplicitTypes() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/chainCallWithExtensionExplicitTypes.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index f1bb82621a7..fdf7742dcee 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -1802,6 +1802,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/applyInsideCoroutine.kt"); } + @TestMetadata("callableReferenceToASuspendFunction.kt") + public void testCallableReferenceToASuspendFunction() throws Exception { + runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/callableReferenceToASuspendFunction.kt"); + } + @TestMetadata("chainCallWithExtensionExplicitTypes.kt") public void testChainCallWithExtensionExplicitTypes() throws Exception { runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/inference/chainCallWithExtensionExplicitTypes.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt index 403af7ef769..57a9a75ff4c 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt @@ -152,11 +152,14 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not } fun isNumberedKFunctionOrKSuspendFunction(type: KotlinType): Boolean { + return isNumberedKFunction(type) || isNumberedKSuspendFunction(type) + } + + fun isNumberedKFunction(type: KotlinType): Boolean { val descriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return false val shortName = descriptor.name.asString() - return (shortName.length > K_FUNCTION_PREFIX.length && shortName.startsWith(K_FUNCTION_PREFIX) || - isNumberedKSuspendFunction(type)) && + return (shortName.length > K_FUNCTION_PREFIX.length && shortName.startsWith(K_FUNCTION_PREFIX)) && DescriptorUtils.getFqName(descriptor).parent().toSafe() == KOTLIN_REFLECT_FQ_NAME }