From eb420a1e0ff35c9853d24a1788c11d4c27cc2e41 Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Fri, 20 Jul 2018 19:12:45 +0300 Subject: [PATCH] Fix [#KT-23921] * make name collector in inliner process handle JsCatch * visit JsParameter during live analyser in coroutine transformer --- .../catchWithInlineInsideSuspend.kt | 39 +++++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 10 +++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 +++++ .../LightAnalysisModeTestGenerated.java | 10 +++++ .../kotlin/js/coroutine/CoroutinePasses.kt | 6 +++ .../kotlin/js/inline/util/collectUtils.kt | 5 +++ .../IrJsCodegenBoxTestGenerated.java | 5 +++ .../semantics/JsCodegenBoxTestGenerated.java | 10 +++++ 8 files changed, 95 insertions(+) create mode 100644 compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt diff --git a/compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt b/compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt new file mode 100644 index 00000000000..f63feb61371 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt @@ -0,0 +1,39 @@ +// IGNORE_BACKEND: JS_IR +// IGNORE_BACKEND: JVM_IR +// WITH_RUNTIME +// WITH_COROUTINES +// COMMON_COROUTINES_TEST +import helpers.* +import COROUTINES_PACKAGE.* +import COROUTINES_PACKAGE.intrinsics.* + +class Controller { + var result = "FAIL" +} + +fun faz(): String { throw IllegalStateException("OK") } + +inline fun exceptionGet(a: () -> String): String { + try { + return a() + } catch (e: Throwable) { + return e.message!! + } +} + +suspend fun baz() {} + +suspend fun Controller.bar() { + result = exceptionGet { faz() } + exceptionGet { baz(); "ignored" } +} + +fun builder(c: suspend Controller.() -> Unit): String { + val controller = Controller() + c.startCoroutine(controller, EmptyContinuation) + return controller.result +} + +fun box(): String { + return builder() { bar() } +} \ No newline at end of file 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 15cfe4456b6..2d1e3a27355 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 @@ -5304,6 +5304,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/beginWithException.kt", "kotlin.coroutines"); } + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines"); + } + @TestMetadata("coercionToUnit.kt") public void testCoercionToUnit_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/coercionToUnit.kt", "kotlin.coroutines.experimental"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 50097da3d44..653dfaa7129 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -5304,6 +5304,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/beginWithException.kt", "kotlin.coroutines"); } + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines"); + } + @TestMetadata("coercionToUnit.kt") public void testCoercionToUnit_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/coercionToUnit.kt", "kotlin.coroutines.experimental"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index ca2759dd778..d96e95126fd 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -5304,6 +5304,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/beginWithException.kt", "kotlin.coroutines"); } + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines"); + } + @TestMetadata("coercionToUnit.kt") public void testCoercionToUnit_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/coercionToUnit.kt", "kotlin.coroutines.experimental"); 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 2862daa2159..57b0a0c67d4 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 @@ -288,6 +288,12 @@ fun List.collectVariablesSurvivingBetweenBlocks(localVariables: } + override fun visitParameter(x: JsParameter) { + varDeclaredIn[x.name]?.add(blockIndex) + varDefinedIn[x.name]?.add(blockIndex) + super.visitParameter(x) + } + override fun visitBinaryExpression(x: JsBinaryOperation) { val lhs = x.arg1 if (x.operator.isAssignment && lhs is JsNameRef) { diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/util/collectUtils.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/util/collectUtils.kt index d91ce442812..29a6c2f33e9 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/util/collectUtils.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/util/collectUtils.kt @@ -104,6 +104,11 @@ fun collectDefinedNames(scope: JsNode): Set { super.visitExpressionStatement(x) } + override fun visitCatch(x: JsCatch) { + names += x.parameter.name + super.visitCatch(x) + } + // Skip function expression, since it does not introduce name in scope of containing function. // The only exception is function statement, that is handled with the code above. override fun visitFunction(x: JsFunction) { } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index 2cbd7783cd0..fec1a77b1ce 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -5004,6 +5004,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/beginWithException.kt", "kotlin.coroutines.experimental"); } + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines.experimental"); + } + @TestMetadata("coercionToUnit.kt") public void testCoercionToUnit_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/coercionToUnit.kt", "kotlin.coroutines.experimental"); 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 d33a6284cf4..fca26870f53 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 @@ -5069,6 +5069,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/beginWithException.kt", "kotlin.coroutines"); } + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("catchWithInlineInsideSuspend.kt") + public void testCatchWithInlineInsideSuspend_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/catchWithInlineInsideSuspend.kt", "kotlin.coroutines"); + } + @TestMetadata("coercionToUnit.kt") public void testCoercionToUnit_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/coercionToUnit.kt", "kotlin.coroutines.experimental");