From cef32b3327337a8c8f0362675f280d7400288228 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 21 Dec 2016 16:02:36 +0300 Subject: [PATCH] JS: initialize fields of coroutine state machine with `undefined` value so that they match local variable semantics. Fix KT-15366 --- .../inlineFunctionWithOptionalParam.kt | 30 +++++++++++++++++++ .../inlineFunctionWithOptionalParam.txt | 27 +++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 6 ++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ ...LightAnalysisModeCodegenTestGenerated.java | 6 ++++ .../coroutine/CoroutineFunctionTransformer.kt | 2 +- .../semantics/JsCodegenBoxTestGenerated.java | 6 ++++ 7 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt create mode 100644 compiler/testData/codegen/light-analysis/coroutines/multiModule/inlineFunctionWithOptionalParam.txt diff --git a/compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt b/compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt new file mode 100644 index 00000000000..fe81fa42641 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt @@ -0,0 +1,30 @@ +// MODULE: lib +// FILE: lib.kt +inline fun foo(x: String = "OK"): String { + return x + x +} + +// MODULE: main(lib) +// FILE: main.kt +// WITH_RUNTIME +// WITH_COROUTINES +import kotlin.coroutines.* + +var result = "" + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(object : Continuation { + override fun resume(value: Unit) { + } + override fun resumeWithException(exception: Throwable) { + } + }) +} + +fun box(): String { + builder { + result = foo() + } + if (result != "OKOK") return "fail: $result" + return "OK" +} diff --git a/compiler/testData/codegen/light-analysis/coroutines/multiModule/inlineFunctionWithOptionalParam.txt b/compiler/testData/codegen/light-analysis/coroutines/multiModule/inlineFunctionWithOptionalParam.txt new file mode 100644 index 00000000000..769cc0b9c44 --- /dev/null +++ b/compiler/testData/codegen/light-analysis/coroutines/multiModule/inlineFunctionWithOptionalParam.txt @@ -0,0 +1,27 @@ +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 +} + + +public final class EmptyContinuation { + public final static field INSTANCE: EmptyContinuation + private method (): void + public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void + public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void +} + + +public final class LibKt { + public synthetic static method foo$default(p0: java.lang.String, p1: int, p2: java.lang.Object): java.lang.String + public final static @org.jetbrains.annotations.NotNull method foo(@org.jetbrains.annotations.NotNull p0: java.lang.String): java.lang.String +} + + +public final class MainKt { + private static @org.jetbrains.annotations.NotNull field result: java.lang.String + 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 + public final static @org.jetbrains.annotations.NotNull method getResult(): java.lang.String + public final static method setResult(@org.jetbrains.annotations.NotNull p0: java.lang.String): void +} 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 06867f47d3f..81c0cd28eef 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 @@ -4933,6 +4933,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("inlineFunctionWithOptionalParam.kt") + public void testInlineFunctionWithOptionalParam() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt"); + doTest(fileName); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/simple.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 8de943bf2da..c12a51f6199 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4933,6 +4933,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("inlineFunctionWithOptionalParam.kt") + public void testInlineFunctionWithOptionalParam() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt"); + doTest(fileName); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/simple.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java index b246b52e6c7..296d1f2d7d0 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeCodegenTestGenerated.java @@ -4933,6 +4933,12 @@ public class LightAnalysisModeCodegenTestGenerated extends AbstractLightAnalysis KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("inlineFunctionWithOptionalParam.kt") + public void testInlineFunctionWithOptionalParam() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt"); + doTest(fileName); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/simple.kt"); diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineFunctionTransformer.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineFunctionTransformer.kt index 161f52f4784..dfffad0c13a 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineFunctionTransformer.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineFunctionTransformer.kt @@ -83,7 +83,7 @@ class CoroutineFunctionTransformer(private val program: JsProgram, private val f } assignToField(context.metadata.exceptionStateName, program.getNumberLiteral(globalCatchBlockIndex)) for (localVariable in localVariables) { - val value = if (localVariable !in parameterNames) JsLiteral.NULL else localVariable.makeRef() + val value = if (localVariable !in parameterNames) Namer.getUndefinedExpression() else localVariable.makeRef() assignToField(function.scope.getFieldName(localVariable), value) } } 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 3e3d6f53fff..03ca841e255 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 @@ -5720,6 +5720,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); } + @TestMetadata("inlineFunctionWithOptionalParam.kt") + public void testInlineFunctionWithOptionalParam() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt"); + doTest(fileName); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/multiModule/simple.kt");