From 692acc463ad135202c39b27b2208992ccc128ce1 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Tue, 7 Jun 2016 13:56:12 +0300 Subject: [PATCH] Fix handleResult call generation for statement-like last expression in a block --- .../kotlin/codegen/ExpressionCodegen.java | 10 ++++--- .../coroutines/statementLikeLastExpression.kt | 29 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/coroutines/statementLikeLastExpression.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 7c2a854e1a7..e567217c9b3 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1619,12 +1619,13 @@ public class ExpressionCodegen extends KtVisitor impleme v.mark(labelBeforeLastExpression); } + // Note that this result value is potentially unused (in case of handleResult coroutine call) StackValue result = isExpression ? gen(possiblyLabeledStatement) : genStatement(possiblyLabeledStatement); if (!iterator.hasNext()) { answer = result; StackValue handleResultValue = !(possiblyLabeledStatement instanceof KtReturnExpression) - ? genControllerHandleResultCallIfNeeded(possiblyLabeledStatement, result) + ? genControllerHandleResultCallIfNeeded(possiblyLabeledStatement, possiblyLabeledStatement) : null; if (handleResultValue != null) { answer = handleResultValue; @@ -1652,7 +1653,7 @@ public class ExpressionCodegen extends KtVisitor impleme } @Nullable - private StackValue genControllerHandleResultCallIfNeeded(@NotNull KtExpression callOwner, @Nullable StackValue valueToReturn) { + private StackValue genControllerHandleResultCallIfNeeded(@NotNull KtExpression callOwner, @Nullable KtExpression valueToReturn) { ResolvedCall resolvedCall = bindingContext.get(RETURN_HANDLE_RESULT_RESOLVED_CALL, callOwner); if (resolvedCall != null) { @@ -1663,7 +1664,8 @@ public class ExpressionCodegen extends KtVisitor impleme tempVariables.put(argumentExpression, StackValue.unit()); } else { - tempVariables.put(argumentExpression, valueToReturn); + assert valueToReturn != null : "valueReturn expected to be not null for non-unit types"; + tempVariables.put(argumentExpression, gen(valueToReturn)); } tempVariables.put( @@ -1967,7 +1969,7 @@ public class ExpressionCodegen extends KtVisitor impleme Type returnType = isNonLocalReturn ? nonLocalReturn.returnType : ExpressionCodegen.this.returnType; StackValue valueToReturn = returnedExpression != null ? gen(returnedExpression) : null; - StackValue handleResultValue = genControllerHandleResultCallIfNeeded(expression, valueToReturn); + StackValue handleResultValue = genControllerHandleResultCallIfNeeded(expression, returnedExpression); if (handleResultValue != null) { handleResultValue.put(Type.VOID_TYPE, v); diff --git a/compiler/testData/codegen/box/coroutines/statementLikeLastExpression.kt b/compiler/testData/codegen/box/coroutines/statementLikeLastExpression.kt new file mode 100644 index 00000000000..6b3e8fe3be9 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/statementLikeLastExpression.kt @@ -0,0 +1,29 @@ +var globalResult = "" +class Controller { + suspend fun suspendWithValue(v: String, x: Continuation) { + x.resume(v) + } + + operator fun handleResult(x: String, c: Continuation) { + globalResult = x + } +} + +fun builder(coroutine c: Controller.() -> Continuation) { + c(Controller()).resume(Unit) +} + +fun box(): String { + + var condition = true + + builder { + if (condition) { + suspendWithValue("OK") + } else { + suspendWithValue("fail 1") + } + } + + return globalResult +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 0c8d4c6a79a..069878f330d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -4177,6 +4177,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("statementLikeLastExpression.kt") + public void testStatementLikeLastExpression() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/statementLikeLastExpression.kt"); + doTest(fileName); + } + @TestMetadata("suspendDelegation.kt") public void testSuspendDelegation() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendDelegation.kt");