FIR2IR: never generate empty when #KT-55459 Fixed

In details, this commit changes the following:
- it converts FIR when without branches to empty IR block without when
- it doesn't drop empty else branches in when anymore
This commit is contained in:
Mikhail Glukhikh
2023-01-24 16:31:14 +01:00
committed by Space Team
parent b026678a34
commit ae4b8be16b
11 changed files with 85 additions and 9 deletions
@@ -1013,12 +1013,11 @@ class Fir2IrVisitor(
}
return conversionScope.withWhenSubject(subjectVariable) {
whenExpression.convertWithOffsets { startOffset, endOffset ->
// If the constant true branch has empty body, it won't be converted. Thus, the entire `when` expression is effectively _not_
// exhaustive anymore. In that case, coerce the return type of `when` expression to Unit as per the backend expectation.
val irBranches = whenExpression.branches.mapNotNullTo(mutableListOf()) { branch ->
branch.takeIf {
it.condition !is FirElseIfTrueCondition || it.result.statements.isNotEmpty()
}?.toIrWhenBranch(whenExpression.typeRef)
if (whenExpression.branches.isEmpty()) {
return@convertWithOffsets IrBlockImpl(startOffset, endOffset, irBuiltIns.unitType, origin)
}
val irBranches = whenExpression.branches.mapTo(mutableListOf()) { branch ->
branch.toIrWhenBranch(whenExpression.typeRef)
}
if (whenExpression.isProperlyExhaustive && whenExpression.branches.none { it.condition is FirElseIfTrueCondition }) {
val irResult = IrCallImpl(
@@ -2600,6 +2600,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/firProblems/deprecated.kt");
}
@Test
@TestMetadata("emptyWhen.kt")
public void testEmptyWhen() throws Exception {
runTest("compiler/testData/ir/irText/firProblems/emptyWhen.kt");
}
@Test
@TestMetadata("Fir2IrClassifierStorage.kt")
public void testFir2IrClassifierStorage() throws Exception {
@@ -2600,6 +2600,12 @@ public class LightTreeFir2IrTextTestGenerated extends AbstractLightTreeFir2IrTex
runTest("compiler/testData/ir/irText/firProblems/deprecated.kt");
}
@Test
@TestMetadata("emptyWhen.kt")
public void testEmptyWhen() throws Exception {
runTest("compiler/testData/ir/irText/firProblems/emptyWhen.kt");
}
@Test
@TestMetadata("Fir2IrClassifierStorage.kt")
public void testFir2IrClassifierStorage() throws Exception {