FIR2IR: coerce to Unit when "when" expr is not effectively exhaustive
This commit is contained in:
committed by
Mikhail Glukhikh
parent
62dcfcde79
commit
9e9ca4953f
@@ -581,11 +581,15 @@ class Fir2IrVisitor(
|
|||||||
KtNodeTypes.POSTFIX_EXPRESSION -> IrStatementOrigin.EXCLEXCL
|
KtNodeTypes.POSTFIX_EXPRESSION -> IrStatementOrigin.EXCLEXCL
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
// 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 effectivelyNotExhaustive = !whenExpression.isExhaustive ||
|
||||||
|
whenExpression.branches.any { it.condition is FirElseIfTrueCondition && it.result.statements.isEmpty() }
|
||||||
return conversionScope.withWhenSubject(subjectVariable) {
|
return conversionScope.withWhenSubject(subjectVariable) {
|
||||||
whenExpression.convertWithOffsets { startOffset, endOffset ->
|
whenExpression.convertWithOffsets { startOffset, endOffset ->
|
||||||
val irWhen = IrWhenImpl(
|
val irWhen = IrWhenImpl(
|
||||||
startOffset, endOffset,
|
startOffset, endOffset,
|
||||||
whenExpression.typeRef.toIrType(),
|
if (effectivelyNotExhaustive) irBuiltIns.unitType else whenExpression.typeRef.toIrType(),
|
||||||
origin
|
origin
|
||||||
).apply {
|
).apply {
|
||||||
var unconditionalBranchFound = false
|
var unconditionalBranchFound = false
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
|
|
||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
|
|
||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
// IGNORE_BACKEND_FIR: JVM_IR
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
|
|
||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ 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 Boolean type=kotlin.Boolean value=true
|
then: CONST Boolean type=kotlin.Boolean value=true
|
||||||
WHEN type=kotlin.Any origin=IF
|
WHEN type=kotlin.Unit origin=IF
|
||||||
BRANCH
|
BRANCH
|
||||||
if: GET_VAR 'flag: kotlin.Boolean declared in <root>.testEmptyBranches1' type=kotlin.Boolean origin=null
|
if: GET_VAR 'flag: kotlin.Boolean declared in <root>.testEmptyBranches1' type=kotlin.Boolean origin=null
|
||||||
then: CONST Boolean type=kotlin.Boolean value=true
|
then: CONST Boolean type=kotlin.Boolean value=true
|
||||||
@@ -45,7 +45,7 @@ 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 Boolean type=kotlin.Boolean value=true
|
then: CONST Boolean type=kotlin.Boolean value=true
|
||||||
WHEN type=kotlin.Any origin=IF
|
WHEN type=kotlin.Unit origin=IF
|
||||||
BRANCH
|
BRANCH
|
||||||
if: GET_VAR 'flag: kotlin.Boolean declared in <root>.testEmptyBranches2' type=kotlin.Boolean origin=null
|
if: GET_VAR 'flag: kotlin.Boolean declared in <root>.testEmptyBranches2' type=kotlin.Boolean origin=null
|
||||||
then: CONST Boolean type=kotlin.Boolean value=true
|
then: CONST Boolean type=kotlin.Boolean value=true
|
||||||
|
|||||||
Reference in New Issue
Block a user