Exhaustive when with 'Unit' result now also generates an exception in else branch #KT-12192 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-05-16 18:14:33 +03:00
parent 23a4184e48
commit f35fd32a25
5 changed files with 42 additions and 6 deletions
@@ -4044,12 +4044,9 @@ The "returned" value of try expression with no finally is either the last expres
}
private boolean isExhaustive(@NotNull KtWhenExpression whenExpression, boolean isStatement) {
if (isStatement) {
return Boolean.TRUE.equals(bindingContext.get(BindingContext.IMPLICIT_EXHAUSTIVE_WHEN, whenExpression));
}
else {
return Boolean.TRUE.equals(bindingContext.get(BindingContext.EXHAUSTIVE_WHEN, whenExpression));
}
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;
}
public void putUnitInstanceOntoStackForNonExhaustiveWhen(
@@ -0,0 +1,9 @@
fun test(i: Int): String {
when (i) {
0 -> return "0"
1 -> return "1"
}
return "OK"
}
fun box(): String = test(42)
@@ -0,0 +1,18 @@
enum class AccessMode { READ, WRITE, EXECUTE }
fun whenExpr(access: AccessMode) {
return when (access) {
AccessMode.READ -> {}
AccessMode.WRITE -> {}
AccessMode.EXECUTE -> {}
}
}
fun box(): String {
whenExpr(AccessMode.EXECUTE)
return "OK"
}
// 1 TABLESWITCH
// 0 LOOKUPSWITCH
// 1 ATHROW
@@ -14524,6 +14524,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("implicitExhaustiveAndReturn.kt")
public void testImplicitExhaustiveAndReturn() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/implicitExhaustiveAndReturn.kt");
doTest(fileName);
}
@TestMetadata("integralWhenWithNoInlinedConstants.kt")
public void testIntegralWhenWithNoInlinedConstants() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/integralWhenWithNoInlinedConstants.kt");
@@ -1294,6 +1294,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("exhaustiveWhenUnit.kt")
public void testExhaustiveWhenUnit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/when/exhaustiveWhenUnit.kt");
doTest(fileName);
}
@TestMetadata("integralWhenWithNoInlinedConstants.kt")
public void testIntegralWhenWithNoInlinedConstants() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/when/integralWhenWithNoInlinedConstants.kt");