psi2ir: Handle missing "then" branches as empty blocks
#KT-27933
This commit is contained in:
+6
-1
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.ir.types.IrType
|
|||||||
import org.jetbrains.kotlin.ir.types.classifierOrNull
|
import org.jetbrains.kotlin.ir.types.classifierOrNull
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffsetSkippingComments
|
import org.jetbrains.kotlin.psi.psiUtil.startOffsetSkippingComments
|
||||||
import org.jetbrains.kotlin.psi2ir.deparenthesize
|
import org.jetbrains.kotlin.psi2ir.deparenthesize
|
||||||
import org.jetbrains.kotlin.psi2ir.intermediate.defaultLoad
|
import org.jetbrains.kotlin.psi2ir.intermediate.defaultLoad
|
||||||
@@ -45,7 +46,8 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
|
|||||||
|
|
||||||
whenBranches@ while (true) {
|
whenBranches@ while (true) {
|
||||||
val irCondition = ktLastIf.condition!!.genExpr()
|
val irCondition = ktLastIf.condition!!.genExpr()
|
||||||
val irThenBranch = ktLastIf.then!!.genExpr()
|
|
||||||
|
val irThenBranch = ktLastIf.then?.genExpr() ?: generateEmptyBlockForMissingBranch(ktLastIf)
|
||||||
irBranches.add(IrBranchImpl(irCondition, irThenBranch))
|
irBranches.add(IrBranchImpl(irCondition, irThenBranch))
|
||||||
|
|
||||||
val ktElse = ktLastIf.`else`?.deparenthesize()
|
val ktElse = ktLastIf.`else`?.deparenthesize()
|
||||||
@@ -63,6 +65,9 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
|
|||||||
return createIrWhen(expression, irBranches, irElseBranch, resultType)
|
return createIrWhen(expression, irBranches, irElseBranch, resultType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun generateEmptyBlockForMissingBranch(ktLastIf: KtIfExpression) =
|
||||||
|
IrBlockImpl(ktLastIf.startOffset, ktLastIf.endOffset, context.irBuiltIns.nothingType, IrStatementOrigin.IF, listOf())
|
||||||
|
|
||||||
private fun createIrWhen(
|
private fun createIrWhen(
|
||||||
ktIf: KtIfExpression,
|
ktIf: KtIfExpression,
|
||||||
irBranches: List<IrBranch>,
|
irBranches: List<IrBranch>,
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun box(): String {
|
||||||
|
var r = ""
|
||||||
|
if (r != "") else r += "O"
|
||||||
|
if (r == "O") r += "K" else;
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
FILE fqName:<root> fileName:/kt27933.kt
|
||||||
|
FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String flags:
|
||||||
|
BLOCK_BODY
|
||||||
|
VAR name:r type:kotlin.String flags:var
|
||||||
|
CONST String type=kotlin.String value=""
|
||||||
|
WHEN type=kotlin.Unit origin=null
|
||||||
|
BRANCH
|
||||||
|
if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ
|
||||||
|
arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ
|
||||||
|
arg0: GET_VAR 'r: String' type=kotlin.String origin=null
|
||||||
|
arg1: CONST String type=kotlin.String value=""
|
||||||
|
then: BLOCK type=kotlin.Nothing origin=IF
|
||||||
|
BRANCH
|
||||||
|
if: CONST Boolean type=kotlin.Boolean value=true
|
||||||
|
then: SET_VAR 'r: String' type=kotlin.Unit origin=PLUSEQ
|
||||||
|
CALL 'plus(Any?): String' type=kotlin.String origin=PLUSEQ
|
||||||
|
$this: GET_VAR 'r: String' type=kotlin.String origin=PLUSEQ
|
||||||
|
other: CONST String type=kotlin.String value="O"
|
||||||
|
WHEN type=kotlin.Unit origin=null
|
||||||
|
BRANCH
|
||||||
|
if: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ
|
||||||
|
arg0: GET_VAR 'r: String' type=kotlin.String origin=null
|
||||||
|
arg1: CONST String type=kotlin.String value="O"
|
||||||
|
then: SET_VAR 'r: String' type=kotlin.Unit origin=PLUSEQ
|
||||||
|
CALL 'plus(Any?): String' type=kotlin.String origin=PLUSEQ
|
||||||
|
$this: GET_VAR 'r: String' type=kotlin.String origin=PLUSEQ
|
||||||
|
other: CONST String type=kotlin.String value="K"
|
||||||
|
RETURN type=kotlin.Nothing from='box(): String'
|
||||||
|
GET_VAR 'r: String' type=kotlin.String origin=null
|
||||||
@@ -947,6 +947,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
|
|||||||
runTest("compiler/testData/ir/irText/expressions/kt23030.kt");
|
runTest("compiler/testData/ir/irText/expressions/kt23030.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt27933.kt")
|
||||||
|
public void testKt27933() throws Exception {
|
||||||
|
runTest("compiler/testData/ir/irText/expressions/kt27933.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("kt28006.kt")
|
@TestMetadata("kt28006.kt")
|
||||||
public void testKt28006() throws Exception {
|
public void testKt28006() throws Exception {
|
||||||
runTest("compiler/testData/ir/irText/expressions/kt28006.kt");
|
runTest("compiler/testData/ir/irText/expressions/kt28006.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user