From ab2448307e0a5c247ec38a959e23c52fb3159b30 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Wed, 25 Jan 2017 16:50:06 +0300 Subject: [PATCH] Functions imported from objects should be properly mapped to "real" suspend function descriptors. --- .../coroutines/coroutineCodegenUtil.kt | 16 +++++--- .../suspendFunImportedFromObject.kt | 34 +++++++++++++++++ .../suspendFunImportedFromObject.txt | 37 +++++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 6 +++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++ ...LightAnalysisModeCodegenTestGenerated.java | 6 +++ .../semantics/JsCodegenBoxTestGenerated.java | 6 +++ 7 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt create mode 100644 compiler/testData/codegen/light-analysis/coroutines/suspendFunImportedFromObject.txt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/coroutineCodegenUtil.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/coroutineCodegenUtil.kt index c71dea262e1..816a4c0528f 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/coroutineCodegenUtil.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/coroutineCodegenUtil.kt @@ -30,9 +30,7 @@ import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtExpression import org.jetbrains.kotlin.psi.KtPsiFactory -import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.BindingTraceContext -import org.jetbrains.kotlin.resolve.DelegatingBindingTrace +import org.jetbrains.kotlin.resolve.* import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy @@ -94,10 +92,18 @@ fun ResolvedCall<*>.replaceSuspensionFunctionWithRealDescriptor( ) ) } - val function = candidateDescriptor as? SimpleFunctionDescriptor ?: return null + val function = candidateDescriptor as? FunctionDescriptor ?: return null if (!function.isSuspend || function.getUserData(INITIAL_DESCRIPTOR_FOR_SUSPEND_FUNCTION) != null) return null - val newCandidateDescriptor = getOrCreateJvmSuspendFunctionView(function, bindingContext) + val newCandidateDescriptor = + when (function) { + is FunctionImportedFromObject -> + getOrCreateJvmSuspendFunctionView(function.callableFromObject, bindingContext).asImportedFromObject() + is SimpleFunctionDescriptor -> + getOrCreateJvmSuspendFunctionView(function, bindingContext) + else -> + throw AssertionError("Unexpected suspend function descriptor: $function") + } val newCall = ResolvedCallImpl( call, diff --git a/compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt b/compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt new file mode 100644 index 00000000000..65a7e5b57c4 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt @@ -0,0 +1,34 @@ +// WITH_RUNTIME +// WITH_COROUTINES + +// FILE: stuff.kt +package stuff + +import kotlin.coroutines.intrinsics.* + +object Host { + suspend fun suspendHere(): String = suspendCoroutineOrReturn { x -> + x.resume("OK") + SUSPENDED_MARKER + } +} + + +// FILE: test.kt +import kotlin.coroutines.* +import kotlin.coroutines.intrinsics.* +import stuff.Host.suspendHere + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun box(): String { + var result = "" + + builder { + result = suspendHere() + } + + return result +} diff --git a/compiler/testData/codegen/light-analysis/coroutines/suspendFunImportedFromObject.txt b/compiler/testData/codegen/light-analysis/coroutines/suspendFunImportedFromObject.txt new file mode 100644 index 00000000000..775e7f10bd0 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/coroutines/suspendFunImportedFromObject.txt @@ -0,0 +1,37 @@ +@kotlin.Metadata +public final class CoroutineUtilKt { + public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation + public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation +} + +@kotlin.Metadata +public class EmptyContinuation { + public final static field Companion: EmptyContinuation.Companion + private final @org.jetbrains.annotations.NotNull field context: kotlin.coroutines.CoroutineContext + inner class EmptyContinuation/Companion + public @synthetic.kotlin.jvm.GeneratedByJvmOverloads method (): void + public method (@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.CoroutineContext): void + public synthetic method (p0: kotlin.coroutines.CoroutineContext, p1: int, p2: kotlin.jvm.internal.DefaultConstructorMarker): void + public @org.jetbrains.annotations.NotNull method getContext(): kotlin.coroutines.CoroutineContext + public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void + public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void +} + +@kotlin.Metadata +public final static class EmptyContinuation/Companion { + inner class EmptyContinuation/Companion + private method (): void +} + +@kotlin.Metadata +public final class TestKt { + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void +} + +@kotlin.Metadata +public final class stuff/Host { + public final static field INSTANCE: stuff.Host + private method (): void + public final @org.jetbrains.annotations.Nullable method suspendHere(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object +} 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 dbc368c4f37..8c859856d09 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 @@ -4877,6 +4877,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("suspendFunImportedFromObject.kt") + public void testSuspendFunImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendInCycle.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 750ec46299a..f7831022513 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4877,6 +4877,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("suspendFunImportedFromObject.kt") + public void testSuspendFunImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendInCycle.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java index 6105c8419c0..9c3022853d0 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java @@ -4877,6 +4877,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis doTest(fileName); } + @TestMetadata("suspendFunImportedFromObject.kt") + public void testSuspendFunImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendInCycle.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 503a5214b6f..bf4b1896c35 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 @@ -5562,6 +5562,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("suspendFunImportedFromObject.kt") + public void testSuspendFunImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendInCycle.kt");