Fix inlining of tail call suspend function
When inlining tail-call suspend function to regular suspend function in JS BE, don't forget to insert suspension point. See KT-16951
This commit is contained in:
@@ -77,6 +77,8 @@ var HasMetadata.sideEffects: SideEffectKind by MetadataProperty(default = SideEf
|
||||
*/
|
||||
var JsExpression.isSuspend: Boolean by MetadataProperty(default = false)
|
||||
|
||||
var JsExpression.isTailCallSuspend: Boolean by MetadataProperty(default = false)
|
||||
|
||||
/**
|
||||
* Denotes a reference to coroutine's `result` field that contains result of
|
||||
* last suspended invocation.
|
||||
@@ -100,7 +102,6 @@ var JsFunction.coroutineMetadata: CoroutineMetadata? by MetadataProperty(default
|
||||
|
||||
class CoroutineMetadata(
|
||||
val doResumeName: JsName,
|
||||
val resumeName: JsName,
|
||||
val stateName: JsName,
|
||||
val exceptionStateName: JsName,
|
||||
val finallyPathName: JsName,
|
||||
|
||||
+6
-1
@@ -43,9 +43,13 @@ class ReturnReplacingVisitor(
|
||||
|
||||
ctx.removeMe()
|
||||
|
||||
val returnExpression = x.expression
|
||||
val returnReplacement = getReturnReplacement(x.expression)
|
||||
if (returnReplacement != null) {
|
||||
ctx.addNext(JsExpressionStatement(returnReplacement).apply { synthetic = true })
|
||||
if (returnExpression != null && returnExpression.isTailCallSuspend) {
|
||||
returnReplacement.isSuspend = true
|
||||
}
|
||||
ctx.addNext(JsExpressionStatement(returnReplacement))
|
||||
}
|
||||
|
||||
if (breakLabel != null) {
|
||||
@@ -68,6 +72,7 @@ class ReturnReplacingVisitor(
|
||||
|
||||
fun processCoroutineResult(expression: JsExpression?): JsExpression? {
|
||||
if (!isSuspend) return expression
|
||||
if (expression != null && expression.isTailCallSuspend) return expression
|
||||
val lhs = JsNameRef("\$\$coroutineResult\$\$", JsAstUtils.stateMachineReceiver()).apply { coroutineResult = true }
|
||||
return JsAstUtils.assignment(lhs, expression ?: Namer.getUndefinedExpression())
|
||||
}
|
||||
|
||||
@@ -6298,6 +6298,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/tailOperations/suspendWithWhen.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("tailInlining.kt")
|
||||
public void testTailInlining() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/tailOperations/tailInlining.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/coroutines/unitTypeReturn")
|
||||
|
||||
+13
-11
@@ -22,10 +22,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsExpression
|
||||
import org.jetbrains.kotlin.js.backend.ast.JsNameRef
|
||||
import org.jetbrains.kotlin.js.backend.ast.metadata.SideEffectKind
|
||||
import org.jetbrains.kotlin.js.backend.ast.metadata.coroutineResult
|
||||
import org.jetbrains.kotlin.js.backend.ast.metadata.isSuspend
|
||||
import org.jetbrains.kotlin.js.backend.ast.metadata.sideEffects
|
||||
import org.jetbrains.kotlin.js.backend.ast.metadata.*
|
||||
import org.jetbrains.kotlin.js.translate.context.TranslationContext
|
||||
import org.jetbrains.kotlin.js.translate.general.Translation
|
||||
import org.jetbrains.kotlin.js.translate.reference.CallArgumentTranslator
|
||||
@@ -137,13 +134,18 @@ private fun translateFunctionCall(
|
||||
inlineResolvedCall.resultingDescriptor, context)
|
||||
}
|
||||
|
||||
if (resolvedCall.resultingDescriptor.isSuspend && context.isInStateMachine) {
|
||||
context.currentBlock.statements += JsAstUtils.asSyntheticStatement(callExpression.apply { isSuspend = true })
|
||||
val coroutineRef = TranslationUtils.translateContinuationArgument(context, resolvedCall)
|
||||
return context.defineTemporary(JsNameRef("\$\$coroutineResult\$\$", coroutineRef).apply {
|
||||
sideEffects = SideEffectKind.DEPENDS_ON_STATE
|
||||
coroutineResult = true
|
||||
})
|
||||
if (resolvedCall.resultingDescriptor.isSuspend) {
|
||||
if (context.isInStateMachine) {
|
||||
context.currentBlock.statements += JsAstUtils.asSyntheticStatement(callExpression.apply { isSuspend = true })
|
||||
val coroutineRef = TranslationUtils.translateContinuationArgument(context, resolvedCall)
|
||||
return context.defineTemporary(JsNameRef("\$\$coroutineResult\$\$", coroutineRef).apply {
|
||||
sideEffects = SideEffectKind.DEPENDS_ON_STATE
|
||||
coroutineResult = true
|
||||
})
|
||||
}
|
||||
else {
|
||||
callExpression.isTailCallSuspend = true
|
||||
}
|
||||
}
|
||||
return callExpression
|
||||
}
|
||||
|
||||
@@ -159,7 +159,6 @@ fun JsFunction.fillCoroutineMetadata(
|
||||
|
||||
coroutineMetadata = CoroutineMetadata(
|
||||
doResumeName = context.getNameForDescriptor(TranslationUtils.getCoroutineDoResumeFunction(context)),
|
||||
resumeName = context.getNameForDescriptor(TranslationUtils.getCoroutineResumeFunction(context)),
|
||||
suspendObjectRef = ReferenceTranslator.translateAsValueReference(suspendPropertyDescriptor, context),
|
||||
baseClassRef = coroutineBaseClassRef,
|
||||
stateName = getCoroutinePropertyName("state"),
|
||||
|
||||
Reference in New Issue
Block a user