diff --git a/compiler/testData/codegen/box/coroutines/localDelegate.kt b/compiler/testData/codegen/box/coroutines/localDelegate.kt new file mode 100644 index 00000000000..d2169d806a8 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/localDelegate.kt @@ -0,0 +1,29 @@ +// WITH_RUNTIME +// WITH_COROUTINES +import kotlin.coroutines.* +import kotlin.coroutines.intrinsics.SUSPENDED_MARKER +import kotlin.coroutines.intrinsics.suspendCoroutineOrReturn + +class OkDelegate { + operator fun getValue(receiver: Any?, property: Any?): String = "OK" +} + +suspend fun suspendWithValue(value: T): T = suspendCoroutineOrReturn { c -> + c.resume(value) + SUSPENDED_MARKER +} + +fun launch(c: suspend () -> String): String { + var result: String = "fail: result not assigned" + c.startCoroutine(handleResultContinuation { value -> + result = value + }) + return result +} + +fun box(): String { + return launch { + val ok by OkDelegate() + ok + } +} \ No newline at end of file diff --git a/compiler/testData/codegen/light-analysis/coroutines/localDelegate.txt b/compiler/testData/codegen/light-analysis/coroutines/localDelegate.txt new file mode 100644 index 00000000000..736996e0c9a --- /dev/null +++ b/compiler/testData/codegen/light-analysis/coroutines/localDelegate.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 LocalDelegateKt { + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static @org.jetbrains.annotations.NotNull method launch(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): java.lang.String + public final static @org.jetbrains.annotations.Nullable method suspendWithValue(p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object +} + +@kotlin.Metadata +public final class OkDelegate { + public method (): void + public final @org.jetbrains.annotations.NotNull method getValue(@org.jetbrains.annotations.Nullable p0: java.lang.Object, @org.jetbrains.annotations.Nullable p1: java.lang.Object): java.lang.String +} 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 e1992d276a4..512b871678d 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 @@ -4769,6 +4769,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("localDelegate.kt") + public void testLocalDelegate() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/localDelegate.kt"); + doTest(fileName); + } + @TestMetadata("multipleInvokeCalls.kt") public void testMultipleInvokeCalls() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multipleInvokeCalls.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 6cdb9c393fa..b222a33b344 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4769,6 +4769,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("localDelegate.kt") + public void testLocalDelegate() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/localDelegate.kt"); + doTest(fileName); + } + @TestMetadata("multipleInvokeCalls.kt") public void testMultipleInvokeCalls() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multipleInvokeCalls.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java index b64605ff21c..45273d83d14 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java @@ -4769,6 +4769,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis doTest(fileName); } + @TestMetadata("localDelegate.kt") + public void testLocalDelegate() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/localDelegate.kt"); + doTest(fileName); + } + @TestMetadata("multipleInvokeCalls.kt") public void testMultipleInvokeCalls() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multipleInvokeCalls.kt"); diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutinePasses.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutinePasses.kt index df74249cb3f..8b77880c580 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutinePasses.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutinePasses.kt @@ -211,6 +211,8 @@ fun JsBlock.replaceSpecialReferences(context: CoroutineTransformationContext) { ctx.replaceMe(JsNameRef(context.receiverFieldName, JsLiteral.THIS)) } + override fun visit(x: JsFunction, ctx: JsContext<*>) = false + override fun endVisit(x: JsNameRef, ctx: JsContext) { when { x.coroutineReceiver -> { @@ -237,6 +239,8 @@ fun JsBlock.replaceSpecialReferences(context: CoroutineTransformationContext) { fun JsBlock.replaceLocalVariables(scope: JsScope, context: CoroutineTransformationContext, localVariables: Set) { replaceSpecialReferences(context) val visitor = object : JsVisitorWithContextImpl() { + override fun visit(x: JsFunction, ctx: JsContext<*>) = false + override fun endVisit(x: JsNameRef, ctx: JsContext) { if (x.qualifier == null && x.name in localVariables) { val fieldName = scope.getFieldName(x.name!!) 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 a971b7fe02c..cc1ce7f3195 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 @@ -5454,6 +5454,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("localDelegate.kt") + public void testLocalDelegate() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/localDelegate.kt"); + doTest(fileName); + } + @TestMetadata("multipleInvokeCalls.kt") public void testMultipleInvokeCalls() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multipleInvokeCalls.kt");