From 5ee33e6ad5b167e1f433fce2781cf2a8dd9dab6e Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Fri, 17 Jun 2016 19:02:04 +0300 Subject: [PATCH] Generate last expression in coroutine block even for Unit expected type --- .../kotlin/codegen/ExpressionCodegen.java | 3 ++- .../box/coroutines/lastUnitExpression.kt | 26 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/coroutines/lastUnitExpression.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 39f9dae7be8..45570c96490 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1745,7 +1745,8 @@ public class ExpressionCodegen extends KtVisitor impleme assert resolvedCall.getValueArgumentsByIndex() != null : "Arguments were not resolved for call element: " + callOwner.getText(); KtExpression argumentExpression = resolvedCall.getValueArgumentsByIndex().get(0).getArguments().get(0).getArgumentExpression(); - if (KotlinBuiltIns.isUnit(resolvedCall.getResultingDescriptor().getValueParameters().get(0).getType())) { + if (valueToReturn == null + && KotlinBuiltIns.isUnit(resolvedCall.getResultingDescriptor().getValueParameters().get(0).getType())) { tempVariables.put(argumentExpression, StackValue.unit()); } else { diff --git a/compiler/testData/codegen/box/coroutines/lastUnitExpression.kt b/compiler/testData/codegen/box/coroutines/lastUnitExpression.kt new file mode 100644 index 00000000000..fea95ccff0d --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/lastUnitExpression.kt @@ -0,0 +1,26 @@ +class Controller { + var ok = false + var v = "fail" + suspend fun suspendHere(v: String, x: Continuation) { + this.v = v + x.resume(Unit) + } + + operator fun handleResult(u: Unit, v: Continuation) { + ok = true + } +} + +fun builder(coroutine c: Controller.() -> Continuation): String { + val controller = Controller() + c(controller).resume(Unit) + if (!controller.ok) throw java.lang.RuntimeException("Fail 1") + return controller.v +} + +fun box(): String { + + return builder { + suspendHere("OK") + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 5a4dc671af4..9854589745f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4183,6 +4183,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("lastUnitExpression.kt") + public void testLastUnitExpression() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/lastUnitExpression.kt"); + doTest(fileName); + } + @TestMetadata("manualContinuationImpl.kt") public void testManualContinuationImpl() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/manualContinuationImpl.kt");