Back-end JVM: more accurate handling of when expressions with Unit result #KT-12192 Fixed
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user