psi2ir: Empty 'if' branches have type 'kotlin.Unit'
This commit is contained in:
+2
-4
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.ir.declarations.IrVariable
|
|||||||
import org.jetbrains.kotlin.ir.expressions.*
|
import org.jetbrains.kotlin.ir.expressions.*
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.*
|
import org.jetbrains.kotlin.ir.expressions.impl.*
|
||||||
import org.jetbrains.kotlin.ir.types.IrType
|
import org.jetbrains.kotlin.ir.types.IrType
|
||||||
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.startOffset
|
||||||
@@ -51,8 +50,7 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
|
|||||||
val irThenBranch = ktLastIf.then?.genExpr() ?: generateEmptyBlockForMissingBranch(ktLastIf)
|
val irThenBranch = ktLastIf.then?.genExpr() ?: generateEmptyBlockForMissingBranch(ktLastIf)
|
||||||
irBranches.add(IrBranchImpl(irCondition, irThenBranch))
|
irBranches.add(IrBranchImpl(irCondition, irThenBranch))
|
||||||
|
|
||||||
val ktElse = ktLastIf.`else`?.deparenthesize()
|
when (val ktElse = ktLastIf.`else`?.deparenthesize()) {
|
||||||
when (ktElse) {
|
|
||||||
null -> break@whenBranches
|
null -> break@whenBranches
|
||||||
is KtIfExpression -> ktLastIf = ktElse
|
is KtIfExpression -> ktLastIf = ktElse
|
||||||
is KtExpression -> {
|
is KtExpression -> {
|
||||||
@@ -67,7 +65,7 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun generateEmptyBlockForMissingBranch(ktLastIf: KtIfExpression) =
|
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(
|
private fun createIrWhen(
|
||||||
ktIf: KtIfExpression,
|
ktIf: KtIfExpression,
|
||||||
|
|||||||
+38
-1
@@ -21,4 +21,41 @@ FILE fqName:<root> fileName:/ifElseIf.kt
|
|||||||
BRANCH
|
BRANCH
|
||||||
if: CONST Boolean type=kotlin.Boolean value=true
|
if: CONST Boolean type=kotlin.Boolean value=true
|
||||||
then: CONST Int type=kotlin.Int value=0
|
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
@@ -1,4 +1,19 @@
|
|||||||
fun test(i: Int) =
|
fun test(i: Int) =
|
||||||
if (i > 0) 1
|
if (i > 0) 1
|
||||||
else if (i < 0) -1
|
else if (i < 0) -1
|
||||||
else 0
|
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
|
||||||
|
}
|
||||||
@@ -17,3 +17,49 @@ FILE fqName:<root> fileName:/ifElseIf.kt
|
|||||||
BRANCH
|
BRANCH
|
||||||
if: CONST Boolean type=kotlin.Boolean value=true
|
if: CONST Boolean type=kotlin.Boolean value=true
|
||||||
then: CONST Int type=kotlin.Int value=0
|
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
@@ -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
|
$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
|
arg0: GET_VAR 'var r: kotlin.String [var] declared in <root>.box' type=kotlin.String origin=null
|
||||||
arg1: CONST String type=kotlin.String value=""
|
arg1: CONST String type=kotlin.String value=""
|
||||||
then: BLOCK type=kotlin.Nothing origin=IF
|
then: BLOCK type=kotlin.Unit origin=IF
|
||||||
BRANCH
|
BRANCH
|
||||||
if: CONST Boolean type=kotlin.Boolean value=true
|
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
|
then: SET_VAR 'var r: kotlin.String [var] declared in <root>.box' type=kotlin.Unit origin=PLUSEQ
|
||||||
|
|||||||
Reference in New Issue
Block a user