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:
@@ -0,0 +1,78 @@
|
|||||||
|
// IGNORE_BACKEND: NATIVE
|
||||||
|
// WITH_RUNTIME
|
||||||
|
// WITH_COROUTINES
|
||||||
|
import kotlin.coroutines.experimental.*
|
||||||
|
import kotlin.coroutines.experimental.intrinsics.*
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
async {
|
||||||
|
O.foo()
|
||||||
|
O.foo("second")
|
||||||
|
}
|
||||||
|
while (!finished) {
|
||||||
|
result += "--;"
|
||||||
|
proceed()
|
||||||
|
}
|
||||||
|
|
||||||
|
finished = false
|
||||||
|
asyncSuspend {
|
||||||
|
O.foo()
|
||||||
|
O.foo("second")
|
||||||
|
}
|
||||||
|
while (!finished) {
|
||||||
|
result += "--;"
|
||||||
|
proceed()
|
||||||
|
}
|
||||||
|
|
||||||
|
val expected = "before(first);--;after(first);before(second);--;after(second);--;done;"
|
||||||
|
if (result != expected + expected) return "fail: $result"
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
|
||||||
|
interface I {
|
||||||
|
suspend fun foo(x: String = "first")
|
||||||
|
}
|
||||||
|
|
||||||
|
object O : I {
|
||||||
|
override suspend fun foo(x: String) {
|
||||||
|
result += "before($x);"
|
||||||
|
sleep()
|
||||||
|
result += "after($x);"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun sleep(): Unit = suspendCoroutine { c ->
|
||||||
|
proceed = { c.resume(Unit) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun async(f: suspend () -> Unit) {
|
||||||
|
f.startCoroutine(object : Continuation<Unit> {
|
||||||
|
override fun resume(x: Unit) {
|
||||||
|
proceed = {
|
||||||
|
result += "done;"
|
||||||
|
finished = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
override fun resumeWithException(x: Throwable) {}
|
||||||
|
override val context = EmptyCoroutineContext
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fun asyncSuspend(f: suspend () -> Unit) {
|
||||||
|
val coroutine = f.createCoroutine(object : Continuation<Unit> {
|
||||||
|
override fun resume(x: Unit) {
|
||||||
|
proceed = {
|
||||||
|
result += "done;"
|
||||||
|
finished = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
override fun resumeWithException(x: Throwable) {}
|
||||||
|
override val context = EmptyCoroutineContext
|
||||||
|
})
|
||||||
|
coroutine.resume(Unit)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = ""
|
||||||
|
var proceed: () -> Unit = { }
|
||||||
|
var finished = false
|
||||||
+60
@@ -0,0 +1,60 @@
|
|||||||
|
@kotlin.Metadata
|
||||||
|
public final class CoroutineUtilKt {
|
||||||
|
public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.experimental.Continuation
|
||||||
|
public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.experimental.Continuation
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public class EmptyContinuation {
|
||||||
|
public final static field Companion: EmptyContinuation.Companion
|
||||||
|
private final @org.jetbrains.annotations.NotNull field context: kotlin.coroutines.experimental.CoroutineContext
|
||||||
|
inner class EmptyContinuation/Companion
|
||||||
|
public @synthetic.kotlin.jvm.GeneratedByJvmOverloads method <init>(): void
|
||||||
|
public method <init>(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.experimental.CoroutineContext): void
|
||||||
|
public synthetic method <init>(p0: kotlin.coroutines.experimental.CoroutineContext, p1: int, p2: kotlin.jvm.internal.DefaultConstructorMarker): void
|
||||||
|
public @org.jetbrains.annotations.NotNull method getContext(): kotlin.coroutines.experimental.CoroutineContext
|
||||||
|
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
|
||||||
|
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public final static class EmptyContinuation/Companion {
|
||||||
|
inner class EmptyContinuation/Companion
|
||||||
|
private method <init>(): void
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public interface I {
|
||||||
|
inner class I/DefaultImpls
|
||||||
|
public abstract @org.jetbrains.annotations.Nullable method foo(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.experimental.Continuation): java.lang.Object
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public final class I/DefaultImpls {
|
||||||
|
inner class I/DefaultImpls
|
||||||
|
public synthetic static method foo$default(p0: I, p1: java.lang.String, p2: kotlin.coroutines.experimental.Continuation, p3: int, p4: java.lang.Object): java.lang.Object
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public final class O {
|
||||||
|
public final static field INSTANCE: O
|
||||||
|
private method <init>(): void
|
||||||
|
public @org.jetbrains.annotations.Nullable method foo(@org.jetbrains.annotations.NotNull p0: java.lang.String, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.experimental.Continuation): java.lang.Object
|
||||||
|
}
|
||||||
|
|
||||||
|
@kotlin.Metadata
|
||||||
|
public final class OverrideDefaultArgumentKt {
|
||||||
|
private static field finished: boolean
|
||||||
|
private static @org.jetbrains.annotations.NotNull field proceed: kotlin.jvm.functions.Function0
|
||||||
|
private static @org.jetbrains.annotations.NotNull field result: java.lang.String
|
||||||
|
public final static method async(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||||
|
public final static method asyncSuspend(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
|
||||||
|
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||||
|
public final static method getFinished(): boolean
|
||||||
|
public final static @org.jetbrains.annotations.NotNull method getProceed(): kotlin.jvm.functions.Function0
|
||||||
|
public final static @org.jetbrains.annotations.NotNull method getResult(): java.lang.String
|
||||||
|
public final static method setFinished(p0: boolean): void
|
||||||
|
public final static method setProceed(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function0): void
|
||||||
|
public final static method setResult(@org.jetbrains.annotations.NotNull p0: java.lang.String): void
|
||||||
|
public final static @org.jetbrains.annotations.Nullable method sleep(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.experimental.Continuation): java.lang.Object
|
||||||
|
}
|
||||||
+6
@@ -5078,6 +5078,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("returnByLabel.kt")
|
||||||
public void testReturnByLabel() throws Exception {
|
public void testReturnByLabel() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/returnByLabel.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/returnByLabel.kt");
|
||||||
|
|||||||
@@ -5078,6 +5078,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("returnByLabel.kt")
|
||||||
public void testReturnByLabel() throws Exception {
|
public void testReturnByLabel() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/returnByLabel.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/returnByLabel.kt");
|
||||||
|
|||||||
@@ -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))
|
val invokeResume = JsReturn(JsInvocation(JsNameRef(context.metadata.doResumeName, instanceName.makeRef()), JsLiteral.NULL))
|
||||||
|
|
||||||
functionWithBody.body.statements += JsIf(
|
functionWithBody.body.statements += JsIf(suspendedName.makeRef(), JsReturn(instanceName.makeRef()), invokeResume)
|
||||||
suspendedName.makeRef(), JsReturn(instanceName.makeRef()), invokeResume)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateCoroutineBody(
|
private fun generateCoroutineBody(
|
||||||
|
|||||||
@@ -5781,6 +5781,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("returnByLabel.kt")
|
||||||
public void testReturnByLabel() throws Exception {
|
public void testReturnByLabel() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/returnByLabel.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/returnByLabel.kt");
|
||||||
|
|||||||
+7
-2
@@ -118,7 +118,7 @@ class DeclarationBodyVisitor(
|
|||||||
val callbackName = caller.scope.declareTemporaryName("callback" + Namer.DEFAULT_PARAMETER_IMPLEMENTOR_SUFFIX)
|
val callbackName = caller.scope.declareTemporaryName("callback" + Namer.DEFAULT_PARAMETER_IMPLEMENTOR_SUFFIX)
|
||||||
val callee = JsNameRef(bodyName, JsLiteral.THIS)
|
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 callbackInvocation = JsInvocation(callbackName.makeRef())
|
||||||
val chosenInvocation = JsConditional(callbackName.makeRef(), callbackInvocation, defaultInvocation)
|
val chosenInvocation = JsConditional(callbackName.makeRef(), callbackInvocation, defaultInvocation)
|
||||||
defaultInvocation.arguments += caller.parameters.map { it.name.makeRef() }
|
defaultInvocation.arguments += caller.parameters.map { it.name.makeRef() }
|
||||||
@@ -128,7 +128,12 @@ class DeclarationBodyVisitor(
|
|||||||
caller.body.statements += FunctionBodyTranslator.setDefaultValueForArguments(descriptor, callerContext)
|
caller.body.statements += FunctionBodyTranslator.setDefaultValueForArguments(descriptor, callerContext)
|
||||||
|
|
||||||
val returnType = descriptor.returnType!!
|
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
|
caller.body.statements += statement
|
||||||
|
|
||||||
context.addFunctionToPrototype(containingClass, descriptor, caller)
|
context.addFunctionToPrototype(containingClass, descriptor, caller)
|
||||||
|
|||||||
+2
-2
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.js.translate.utils;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
|
||||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
|
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
|
||||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
|
||||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
|
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.KtDeclarationWithBody;
|
||||||
import org.jetbrains.kotlin.psi.KtExpression;
|
import org.jetbrains.kotlin.psi.KtExpression;
|
||||||
import org.jetbrains.kotlin.types.KotlinType;
|
import org.jetbrains.kotlin.types.KotlinType;
|
||||||
import org.jetbrains.kotlin.types.TypeUtils;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -144,6 +142,8 @@ public final class FunctionBodyTranslator extends AbstractTranslator {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert declaration.getBodyExpression() != null;
|
||||||
|
assert descriptor.getReturnType() != null;
|
||||||
KotlinType bodyType = context().bindingContext().getType(declaration.getBodyExpression());
|
KotlinType bodyType = context().bindingContext().getType(declaration.getBodyExpression());
|
||||||
if (bodyType == null && KotlinBuiltIns.isCharOrNullableChar(descriptor.getReturnType()) ||
|
if (bodyType == null && KotlinBuiltIns.isCharOrNullableChar(descriptor.getReturnType()) ||
|
||||||
bodyType != null && KotlinBuiltIns.isCharOrNullableChar(bodyType) && TranslationUtils.shouldBoxReturnValue(descriptor)) {
|
bodyType != null && KotlinBuiltIns.isCharOrNullableChar(bodyType) && TranslationUtils.shouldBoxReturnValue(descriptor)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user