psi2ir: Empty 'if' branches have type 'kotlin.Unit'

This commit is contained in:
Dmitry Petrov
2019-07-12 14:30:37 +03:00
parent 0774f800a0
commit 6963424dfc
5 changed files with 105 additions and 9 deletions
@@ -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,
+38 -1
View File
@@ -21,4 +21,41 @@ FILE fqName:<root> 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 <root>.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 <root>.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 <root>.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 <root>.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 <root>.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
+18 -3
View File
@@ -1,4 +1,19 @@
fun test(i: Int) =
if (i > 0) 1
else if (i < 0) -1
else 0
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
}
+46
View File
@@ -17,3 +17,49 @@ FILE fqName:<root> 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 <root>.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 <root>.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 <root>.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 <root>.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 <root>.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
+1 -1
View File
@@ -9,7 +9,7 @@ FILE fqName:<root> 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 <root>.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 <root>.box' type=kotlin.Unit origin=PLUSEQ