Fix suspend function with default argument

In JS BE, fix translation of suspend function with default argument
inherited from interface.

See KT-16658
This commit is contained in:
Alexey Andreev
2017-03-20 19:23:18 +03:00
parent 6ba3812582
commit 0606ebe0dc
8 changed files with 166 additions and 6 deletions
@@ -179,8 +179,7 @@ class CoroutineFunctionTransformer(private val program: JsProgram, private val f
val invokeResume = JsReturn(JsInvocation(JsNameRef(context.metadata.doResumeName, instanceName.makeRef()), JsLiteral.NULL))
functionWithBody.body.statements += JsIf(
suspendedName.makeRef(), JsReturn(instanceName.makeRef()), invokeResume)
functionWithBody.body.statements += JsIf(suspendedName.makeRef(), JsReturn(instanceName.makeRef()), invokeResume)
}
private fun generateCoroutineBody(
@@ -5781,6 +5781,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("overrideDefaultArgument.kt")
public void testOverrideDefaultArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/overrideDefaultArgument.kt");
doTest(fileName);
}
@TestMetadata("returnByLabel.kt")
public void testReturnByLabel() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/returnByLabel.kt");
@@ -118,7 +118,7 @@ class DeclarationBodyVisitor(
val callbackName = caller.scope.declareTemporaryName("callback" + Namer.DEFAULT_PARAMETER_IMPLEMENTOR_SUFFIX)
val callee = JsNameRef(bodyName, JsLiteral.THIS)
val defaultInvocation = JsInvocation(callee, java.util.ArrayList<JsExpression>())
val defaultInvocation = JsInvocation(callee, listOf<JsExpression>())
val callbackInvocation = JsInvocation(callbackName.makeRef())
val chosenInvocation = JsConditional(callbackName.makeRef(), callbackInvocation, defaultInvocation)
defaultInvocation.arguments += caller.parameters.map { it.name.makeRef() }
@@ -128,7 +128,12 @@ class DeclarationBodyVisitor(
caller.body.statements += FunctionBodyTranslator.setDefaultValueForArguments(descriptor, callerContext)
val returnType = descriptor.returnType!!
val statement = if (KotlinBuiltIns.isUnit(returnType)) chosenInvocation.makeStmt() else JsReturn(chosenInvocation)
val statement = if (KotlinBuiltIns.isUnit(returnType) && !descriptor.isSuspend) {
chosenInvocation.makeStmt()
}
else {
JsReturn(chosenInvocation)
}
caller.body.statements += statement
context.addFunctionToPrototype(containingClass, descriptor, caller)
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.js.translate.utils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
@@ -34,7 +33,6 @@ import org.jetbrains.kotlin.js.translate.utils.mutator.Mutator;
import org.jetbrains.kotlin.psi.KtDeclarationWithBody;
import org.jetbrains.kotlin.psi.KtExpression;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeUtils;
import java.util.ArrayList;
import java.util.HashMap;
@@ -144,6 +142,8 @@ public final class FunctionBodyTranslator extends AbstractTranslator {
return node;
}
assert declaration.getBodyExpression() != null;
assert descriptor.getReturnType() != null;
KotlinType bodyType = context().bindingContext().getType(declaration.getBodyExpression());
if (bodyType == null && KotlinBuiltIns.isCharOrNullableChar(descriptor.getReturnType()) ||
bodyType != null && KotlinBuiltIns.isCharOrNullableChar(bodyType) && TranslationUtils.shouldBoxReturnValue(descriptor)) {