diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 635e2950c72..d9231cafe65 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -65,6 +65,7 @@ import org.jetbrains.kotlin.resolve.BindingContextUtils; import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.annotations.AnnotationUtilKt; +import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt; import org.jetbrains.kotlin.resolve.calls.callResolverUtil.CallResolverUtilKt; import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt; import org.jetbrains.kotlin.resolve.calls.model.*; @@ -4044,9 +4045,12 @@ The "returned" value of try expression with no finally is either the last expres } private boolean isExhaustive(@NotNull KtWhenExpression whenExpression, boolean isStatement) { - boolean exhaustive = Boolean.TRUE.equals(bindingContext.get(BindingContext.EXHAUSTIVE_WHEN, whenExpression)); - return isStatement ? exhaustive || Boolean.TRUE.equals(bindingContext.get(BindingContext.IMPLICIT_EXHAUSTIVE_WHEN, whenExpression)) - : exhaustive; + if (isStatement && !BindingContextUtilsKt.isUsedAsExpression(whenExpression, bindingContext)) { + return Boolean.TRUE.equals(bindingContext.get(BindingContext.IMPLICIT_EXHAUSTIVE_WHEN, whenExpression)); + } + else { + return Boolean.TRUE.equals(bindingContext.get(BindingContext.EXHAUSTIVE_WHEN, whenExpression)); + } } public void putUnitInstanceOntoStackForNonExhaustiveWhen( diff --git a/compiler/testData/codegen/bytecodeText/when/exhaustiveWhenUnitStatement.kt b/compiler/testData/codegen/bytecodeText/when/exhaustiveWhenUnitStatement.kt new file mode 100644 index 00000000000..45c751ef715 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/when/exhaustiveWhenUnitStatement.kt @@ -0,0 +1,18 @@ +enum class AccessMode { READ, WRITE, EXECUTE } + +fun whenExpr(access: AccessMode) { + when (access) { + AccessMode.READ -> {} + AccessMode.WRITE -> {} + AccessMode.EXECUTE -> {} + } +} + +fun box(): String { + whenExpr(AccessMode.EXECUTE) + return "OK" +} + +// 1 TABLESWITCH +// 0 LOOKUPSWITCH +// 0 ATHROW diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index e239c72d40d..5b2bdd7c4db 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -1300,6 +1300,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { doTest(fileName); } + @TestMetadata("exhaustiveWhenUnitStatement.kt") + public void testExhaustiveWhenUnitStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/when/exhaustiveWhenUnitStatement.kt"); + doTest(fileName); + } + @TestMetadata("integralWhenWithNoInlinedConstants.kt") public void testIntegralWhenWithNoInlinedConstants() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/when/integralWhenWithNoInlinedConstants.kt");