diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt index 738b7c55bcb..d80fca3b353 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt @@ -26,7 +26,6 @@ import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.types.IrType -import org.jetbrains.kotlin.ir.types.classifierOrNull import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset @@ -51,8 +50,7 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta val irThenBranch = ktLastIf.then?.genExpr() ?: generateEmptyBlockForMissingBranch(ktLastIf) irBranches.add(IrBranchImpl(irCondition, irThenBranch)) - val ktElse = ktLastIf.`else`?.deparenthesize() - when (ktElse) { + when (val ktElse = ktLastIf.`else`?.deparenthesize()) { null -> break@whenBranches is KtIfExpression -> ktLastIf = ktElse is KtExpression -> { @@ -67,7 +65,7 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta } private fun generateEmptyBlockForMissingBranch(ktLastIf: KtIfExpression) = - IrBlockImpl(ktLastIf.startOffset, ktLastIf.endOffset, context.irBuiltIns.nothingType, IrStatementOrigin.IF, listOf()) + IrBlockImpl(ktLastIf.startOffset, ktLastIf.endOffset, context.irBuiltIns.unitType, IrStatementOrigin.IF, listOf()) private fun createIrWhen( ktIf: KtIfExpression, diff --git a/compiler/testData/ir/irText/expressions/ifElseIf.fir.txt b/compiler/testData/ir/irText/expressions/ifElseIf.fir.txt index 52d53154e3b..f92519aa730 100644 --- a/compiler/testData/ir/irText/expressions/ifElseIf.fir.txt +++ b/compiler/testData/ir/irText/expressions/ifElseIf.fir.txt @@ -21,4 +21,41 @@ FILE fqName: fileName:/ifElseIf.kt BRANCH if: CONST Boolean type=kotlin.Boolean value=true then: CONST Int type=kotlin.Int value=0 - + FUN name:testEmptyBranches1 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit + VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean + BLOCK_BODY + WHEN type=kotlin.Unit origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches1' type=kotlin.Boolean origin=null + then: BLOCK type=kotlin.Unit origin=null + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: CONST Boolean type=kotlin.Boolean value=true + WHEN type=kotlin.Unit origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches1' type=kotlin.Boolean origin=null + then: CONST Boolean type=kotlin.Boolean value=true + FUN name:testEmptyBranches2 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit + VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean + BLOCK_BODY + WHEN type=kotlin.Unit origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches2' type=kotlin.Boolean origin=null + then: BLOCK type=kotlin.Unit origin=null + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: CONST Boolean type=kotlin.Boolean value=true + WHEN type=kotlin.Unit origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches2' type=kotlin.Boolean origin=null + then: CONST Boolean type=kotlin.Boolean value=true + FUN name:testEmptyBranches3 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit + VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean + BLOCK_BODY + WHEN type=kotlin.Unit origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches3' type=kotlin.Boolean origin=null + then: BLOCK type=kotlin.Unit origin=null + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: CONST Boolean type=kotlin.Boolean value=true diff --git a/compiler/testData/ir/irText/expressions/ifElseIf.kt b/compiler/testData/ir/irText/expressions/ifElseIf.kt index 855e3698846..60fce0e5a2e 100644 --- a/compiler/testData/ir/irText/expressions/ifElseIf.kt +++ b/compiler/testData/ir/irText/expressions/ifElseIf.kt @@ -1,4 +1,19 @@ fun test(i: Int) = - if (i > 0) 1 - else if (i < 0) -1 - else 0 \ No newline at end of file + if (i > 0) 1 + else if (i < 0) -1 + else 0 + +fun testEmptyBranches1(flag: Boolean) { + if (flag) ; else true + if (flag) true else; +} + +fun testEmptyBranches2(flag: Boolean) { + if (flag) {} else true + if (flag) true else {} +} + +fun testEmptyBranches3(flag: Boolean) { + if (flag) + else true +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/ifElseIf.txt b/compiler/testData/ir/irText/expressions/ifElseIf.txt index 4be63318c3b..4cce9ef3f34 100644 --- a/compiler/testData/ir/irText/expressions/ifElseIf.txt +++ b/compiler/testData/ir/irText/expressions/ifElseIf.txt @@ -17,3 +17,49 @@ FILE fqName: fileName:/ifElseIf.kt BRANCH if: CONST Boolean type=kotlin.Boolean value=true then: CONST Int type=kotlin.Int value=0 + FUN name:testEmptyBranches1 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit + VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean + BLOCK_BODY + WHEN type=kotlin.Unit origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches1' type=kotlin.Boolean origin=null + then: BLOCK type=kotlin.Unit origin=IF + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CONST Boolean type=kotlin.Boolean value=true + WHEN type=kotlin.Unit origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches1' type=kotlin.Boolean origin=null + then: TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CONST Boolean type=kotlin.Boolean value=true + FUN name:testEmptyBranches2 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit + VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + WHEN type=kotlin.Any origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches2' type=kotlin.Boolean origin=null + then: BLOCK type=kotlin.Unit origin=null + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: CONST Boolean type=kotlin.Boolean value=true + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + WHEN type=kotlin.Any origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches2' type=kotlin.Boolean origin=null + then: CONST Boolean type=kotlin.Boolean value=true + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: BLOCK type=kotlin.Unit origin=null + FUN name:testEmptyBranches3 visibility:public modality:FINAL <> (flag:kotlin.Boolean) returnType:kotlin.Unit + VALUE_PARAMETER name:flag index:0 type:kotlin.Boolean + BLOCK_BODY + WHEN type=kotlin.Unit origin=IF + BRANCH + if: GET_VAR 'flag: kotlin.Boolean declared in .testEmptyBranches3' type=kotlin.Boolean origin=null + then: BLOCK type=kotlin.Unit origin=IF + BRANCH + if: CONST Boolean type=kotlin.Boolean value=true + then: TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + CONST Boolean type=kotlin.Boolean value=true diff --git a/compiler/testData/ir/irText/expressions/kt27933.txt b/compiler/testData/ir/irText/expressions/kt27933.txt index 3ee38bbb1f4..49dbc1d991d 100644 --- a/compiler/testData/ir/irText/expressions/kt27933.txt +++ b/compiler/testData/ir/irText/expressions/kt27933.txt @@ -9,7 +9,7 @@ FILE fqName: fileName:/kt27933.kt $this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ arg0: GET_VAR 'var r: kotlin.String [var] declared in .box' type=kotlin.String origin=null arg1: CONST String type=kotlin.String value="" - then: BLOCK type=kotlin.Nothing origin=IF + then: BLOCK type=kotlin.Unit origin=IF BRANCH if: CONST Boolean type=kotlin.Boolean value=true then: SET_VAR 'var r: kotlin.String [var] declared in .box' type=kotlin.Unit origin=PLUSEQ