From 58b4ab35f07a4194fc8f5f65007d401ebdde52ab Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Mon, 15 Jul 2019 13:08:11 +0300 Subject: [PATCH] [NI] Fix passing callable references to suspend functions as functional parameter #KT-32452 --- .../fir/FirDiagnosticsSmokeTestGenerated.java | 10 +++++++ .../tests/callableReference/kt32256.kt | 26 ++++++++++++++++++ .../tests/callableReference/kt32256.txt | 9 +++++++ .../suspendCallableReference.kt | 17 ++++++++++++ .../suspendCallableReference.txt | 27 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 10 +++++++ .../DiagnosticsUsingJavacTestGenerated.java | 10 +++++++ .../kotlin/builtins/ReflectionTypes.kt | 3 ++- 8 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/diagnostics/tests/callableReference/kt32256.kt create mode 100644 compiler/testData/diagnostics/tests/callableReference/kt32256.txt create mode 100644 compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.kt create mode 100644 compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.txt diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java index df086566b2b..688d1023e36 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsSmokeTestGenerated.java @@ -1722,6 +1722,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/callableReference/kt31981.kt"); } + @TestMetadata("kt32256.kt") + public void testKt32256() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/kt32256.kt"); + } + @TestMetadata("kt7430_wrongClassOnLHS.kt") public void testKt7430_wrongClassOnLHS() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt7430_wrongClassOnLHS.kt"); @@ -1767,6 +1772,11 @@ public class FirDiagnosticsSmokeTestGenerated extends AbstractFirDiagnosticsSmok runTest("compiler/testData/diagnostics/tests/callableReference/subtypeArgumentFromRHSForReference.kt"); } + @TestMetadata("suspendCallableReference.kt") + public void testSuspendCallableReference() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.kt"); + } + @TestMetadata("typealiases.kt") public void testTypealiases() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/typealiases.kt"); diff --git a/compiler/testData/diagnostics/tests/callableReference/kt32256.kt b/compiler/testData/diagnostics/tests/callableReference/kt32256.kt new file mode 100644 index 00000000000..09de610906f --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/kt32256.kt @@ -0,0 +1,26 @@ +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER +// Issue: KT-32256 + +fun main() { + myMethod(::refA) + myMethod(::refB) + anotherMethod(::refA) + anotherMethod(::refB) +} + +suspend fun refA(input: String): String { + return input +} + +suspend fun refB(): String { + return "Hello, World!" +} + +fun myMethod(f: suspend (String) -> String) {} + +fun myMethod(f: suspend () -> String) {} + +fun anotherMethod(f: suspend (I) -> O) {} + +fun anotherMethod(f: suspend () -> O) {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/kt32256.txt b/compiler/testData/diagnostics/tests/callableReference/kt32256.txt new file mode 100644 index 00000000000..bbb9ad0c027 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/kt32256.txt @@ -0,0 +1,9 @@ +package + +public fun anotherMethod(/*0*/ f: suspend () -> O): kotlin.Unit +public fun anotherMethod(/*0*/ f: suspend (I) -> O): kotlin.Unit +public fun main(): kotlin.Unit +public fun myMethod(/*0*/ f: suspend () -> kotlin.String): kotlin.Unit +public fun myMethod(/*0*/ f: suspend (kotlin.String) -> kotlin.String): kotlin.Unit +public suspend fun refA(/*0*/ input: kotlin.String): kotlin.String +public suspend fun refB(): kotlin.String diff --git a/compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.kt b/compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.kt new file mode 100644 index 00000000000..fdde41c88ed --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.kt @@ -0,0 +1,17 @@ +// !WITH_NEW_INFERENCE +// !DIAGNOSTICS: -UNUSED_PARAMETER +// ISSUE: KT-32452 + +interface A { + suspend fun foo(input: String): String +} + +open class B { + fun call(function: suspend T.(U) -> R): R = TODO() + + fun call(function: suspend T.(U) -> List): List = TODO() +} + +class MyService : A, B() { + override suspend fun foo(input: String) = call(A::foo) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.txt b/compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.txt new file mode 100644 index 00000000000..a1f6763af3e --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.txt @@ -0,0 +1,27 @@ +package + +public interface A { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public abstract suspend fun foo(/*0*/ input: kotlin.String): kotlin.String + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public open class B { + public constructor B() + public final fun call(/*0*/ function: suspend T.(U) -> R): R + public final fun call(/*0*/ function: suspend T.(U) -> kotlin.collections.List): kotlin.collections.List + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class MyService : A, B { + public constructor MyService() + public final override /*1*/ /*fake_override*/ fun call(/*0*/ function: suspend T.(U) -> R): R + public final override /*1*/ /*fake_override*/ fun call(/*0*/ function: suspend T.(U) -> kotlin.collections.List): kotlin.collections.List + public open override /*2*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ suspend fun foo(/*0*/ input: kotlin.String): kotlin.String + public open override /*2*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*2*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index ddc810e3e34..8f838fba1aa 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -1729,6 +1729,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/callableReference/kt31981.kt"); } + @TestMetadata("kt32256.kt") + public void testKt32256() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/kt32256.kt"); + } + @TestMetadata("kt7430_wrongClassOnLHS.kt") public void testKt7430_wrongClassOnLHS() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt7430_wrongClassOnLHS.kt"); @@ -1774,6 +1779,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { runTest("compiler/testData/diagnostics/tests/callableReference/subtypeArgumentFromRHSForReference.kt"); } + @TestMetadata("suspendCallableReference.kt") + public void testSuspendCallableReference() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.kt"); + } + @TestMetadata("typealiases.kt") public void testTypealiases() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/typealiases.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index a702febbf07..6226e734e9f 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -1724,6 +1724,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/callableReference/kt31981.kt"); } + @TestMetadata("kt32256.kt") + public void testKt32256() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/kt32256.kt"); + } + @TestMetadata("kt7430_wrongClassOnLHS.kt") public void testKt7430_wrongClassOnLHS() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt7430_wrongClassOnLHS.kt"); @@ -1769,6 +1774,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/callableReference/subtypeArgumentFromRHSForReference.kt"); } + @TestMetadata("suspendCallableReference.kt") + public void testSuspendCallableReference() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/suspendCallableReference.kt"); + } + @TestMetadata("typealiases.kt") public void testTypealiases() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/typealiases.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt index bbef27bb478..67b0ddd8eeb 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/ReflectionTypes.kt @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.DescriptorUtils.COROUTINES_PACKAGE_FQ_NAME_RELEASE import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.types.* import kotlin.reflect.KProperty @@ -206,7 +207,7 @@ class ReflectionTypes(module: ModuleDescriptor, private val notFoundClasses: Not || shortName == "KCallable" || shortName == "KAnnotatedElement" } - if (packageName == KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) { + if (packageName == KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME || packageName == COROUTINES_PACKAGE_FQ_NAME_RELEASE) { return shortName.startsWith("Function") // FunctionN, Function || shortName.startsWith("SuspendFunction") }