diff --git a/compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt b/compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt new file mode 100644 index 00000000000..fdf9d5f29d6 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt @@ -0,0 +1,50 @@ +// IGNORE_BACKEND: JS_IR +// IGNORE_BACKEND: JVM_IR +// IGNORE_BACKEND: NATIVE +// WITH_COROUTINES +// WITH_RUNTIME +// COMMON_COROUTINES_TEST + +// MODULE: lib(support) +// FILE: lib.kt + +import helpers.* +import COROUTINES_PACKAGE.* +import COROUTINES_PACKAGE.intrinsics.* + +suspend inline fun inlined( + crossinline step: suspend () -> R +): R = notInlined { step() } + +suspend fun notInlined( + block: suspend () -> R +): R = block() + +// MODULE: main(lib, support) +// FILE: main.kt +// WITH_COROUTINES +// WITH_RUNTIME +// COMMON_COROUTINES_TEST + +import helpers.* +import COROUTINES_PACKAGE.* +import COROUTINES_PACKAGE.intrinsics.* + +var result = "FAIL" + +suspend fun test() { + inlined { + result = "OK" + } +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun box(): String { + builder { + test() + } + return result +} \ 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 95accd92397..f6115c3d53e 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 @@ -7033,6 +7033,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); } + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines"); + } + @TestMetadata("inlineFunctionWithOptionalParam.kt") public void testInlineFunctionWithOptionalParam_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.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 b0cc6aa7ec5..b314011b890 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -7033,6 +7033,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines"); + } + @TestMetadata("inlineFunctionWithOptionalParam.kt") public void testInlineFunctionWithOptionalParam_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.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 25248443e85..cd2f59d6974 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -7033,6 +7033,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines"); + } + @TestMetadata("inlineFunctionWithOptionalParam.kt") public void testInlineFunctionWithOptionalParam_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt", "kotlin.coroutines.experimental"); diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/removeUnusedImports.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/removeUnusedImports.kt index 9fe61908388..8b2d84f3f8a 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/removeUnusedImports.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/removeUnusedImports.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.js.inline.clean import org.jetbrains.kotlin.js.backend.ast.* +import org.jetbrains.kotlin.js.backend.ast.metadata.coroutineMetadata import org.jetbrains.kotlin.js.backend.ast.metadata.imported fun removeUnusedImports(root: JsNode) { @@ -43,4 +44,12 @@ private class UsedImportsCollector : RecursiveJsVisitor() { } super.visitNameRef(nameRef) } -} + + override fun visitFunction(x: JsFunction) { + x.coroutineMetadata?.apply { + accept(suspendObjectRef) + accept(baseClassRef) + } + super.visitFunction(x) + } +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java index 495154033dc..75528b5a3b4 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java @@ -4599,6 +4599,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { runTest("js/js.translator/testData/box/inlineMultiModule/importObjectInstance.kt"); } + @TestMetadata("importStdLib.kt") + public void testImportStdLib() throws Exception { + runTest("js/js.translator/testData/box/inlineMultiModule/importStdLib.kt"); + } + @TestMetadata("inlineInInlineWithLambdaMultiModule.kt") public void testInlineInInlineWithLambdaMultiModule() throws Exception { runTest("js/js.translator/testData/box/inlineMultiModule/inlineInInlineWithLambdaMultiModule.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java index c515832abd4..2a07f8d0c79 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java @@ -4599,6 +4599,11 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest { runTest("js/js.translator/testData/box/inlineMultiModule/importObjectInstance.kt"); } + @TestMetadata("importStdLib.kt") + public void testImportStdLib() throws Exception { + runTest("js/js.translator/testData/box/inlineMultiModule/importStdLib.kt"); + } + @TestMetadata("inlineInInlineWithLambdaMultiModule.kt") public void testInlineInInlineWithLambdaMultiModule() throws Exception { runTest("js/js.translator/testData/box/inlineMultiModule/inlineInInlineWithLambdaMultiModule.kt"); 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 3c1f962dded..e56ad33f482 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 @@ -5963,6 +5963,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true); } + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines.experimental"); + } + @TestMetadata("inlineFunctionWithOptionalParam.kt") public void testInlineFunctionWithOptionalParam_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.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 fa3361b9a46..96662e4f8f5 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 @@ -6738,6 +6738,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/coroutines/multiModule"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); } + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("inlineCrossModule.kt") + public void testInlineCrossModule_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineCrossModule.kt", "kotlin.coroutines"); + } + @TestMetadata("inlineFunctionWithOptionalParam.kt") public void testInlineFunctionWithOptionalParam_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/multiModule/inlineFunctionWithOptionalParam.kt", "kotlin.coroutines.experimental"); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/AbstractDeclarationVisitor.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/AbstractDeclarationVisitor.kt index 247c8767d53..8a329e96cc4 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/AbstractDeclarationVisitor.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/AbstractDeclarationVisitor.kt @@ -27,7 +27,10 @@ import org.jetbrains.kotlin.js.backend.ast.metadata.coroutineMetadata import org.jetbrains.kotlin.js.backend.ast.metadata.isInlineableCoroutineBody import org.jetbrains.kotlin.js.descriptorUtils.shouldBeExported import org.jetbrains.kotlin.js.translate.context.TranslationContext -import org.jetbrains.kotlin.js.translate.expression.* +import org.jetbrains.kotlin.js.translate.expression.InlineMetadata +import org.jetbrains.kotlin.js.translate.expression.translateAndAliasParameters +import org.jetbrains.kotlin.js.translate.expression.translateFunction +import org.jetbrains.kotlin.js.translate.expression.wrapWithInlineMetadata import org.jetbrains.kotlin.js.translate.general.TranslatorVisitor import org.jetbrains.kotlin.js.translate.utils.* import org.jetbrains.kotlin.psi.* @@ -123,7 +126,7 @@ abstract class AbstractDeclarationVisitor : TranslatorVisitor() { val innerContext = context.newDeclaration(descriptor).translateAndAliasParameters(descriptor, function.parameters) if (descriptor.isSuspend) { - function.fillCoroutineMetadata(context, descriptor, hasController = false) + function.fillCoroutineMetadata(innerContext, descriptor, hasController = false) } if (!descriptor.isOverridable) { diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/utils.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/utils.kt index a5a58e1457f..0141cbe2ab2 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/utils.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/utils.kt @@ -167,15 +167,13 @@ fun JsFunction.fillCoroutineMetadata( .memberScope .getContributedVariables(COROUTINE_SUSPENDED_NAME, NoLookupLocation.FROM_BACKEND).first() - val coroutineBaseClassRef = ReferenceTranslator.translateAsTypeReference(TranslationUtils.getCoroutineBaseClass(context), context) - fun getCoroutinePropertyName(id: String) = context.getNameForDescriptor(TranslationUtils.getCoroutineProperty(context, id)) coroutineMetadata = CoroutineMetadata( doResumeName = context.getNameForDescriptor(TranslationUtils.getCoroutineDoResumeFunction(context)), suspendObjectRef = ReferenceTranslator.translateAsValueReference(suspendPropertyDescriptor, context), - baseClassRef = coroutineBaseClassRef, + baseClassRef = ReferenceTranslator.translateAsTypeReference(TranslationUtils.getCoroutineBaseClass(context), context), stateName = getCoroutinePropertyName("state"), exceptionStateName = getCoroutinePropertyName("exceptionState"), finallyPathName = getCoroutinePropertyName("finallyPath"), diff --git a/js/js.translator/testData/box/inlineMultiModule/importStdLib.kt b/js/js.translator/testData/box/inlineMultiModule/importStdLib.kt new file mode 100644 index 00000000000..17bf6ddb429 --- /dev/null +++ b/js/js.translator/testData/box/inlineMultiModule/importStdLib.kt @@ -0,0 +1,28 @@ +// IGNORE_BACKEND: JS_IR +// EXPECTED_REACHABLE_NODES: 1117 +// MODULE: lib +// FILE: lib.kt + +inline fun foo(i1: Int, i2: Double): Int { + val o = object : Comparable { + override fun compareTo(other: Int) = i1-other + } + + return o.compareTo(i2.toInt()) +} + +// MODULE: main(lib) +// FILE: main.kt + +fun bar(i: Int):Int { + class Cmp2() : Comparable { + override fun compareTo(other: Int) = -other + } + + return Cmp2().compareTo(i) +} + +fun box():String { + if (foo(10, 20.0) + bar(-10) != 0) return "FAIL" + return "OK" +} \ No newline at end of file