[FIR2IR] Generate unconditional branch in exhaustive whens

This commit is contained in:
Mikhail Glukhikh
2020-03-19 14:19:43 +03:00
parent 41f5a56cb3
commit 94fe79578e
4 changed files with 15 additions and 4 deletions
@@ -504,11 +504,25 @@ class Fir2IrVisitor(
whenExpression.typeRef.toIrType(),
origin
).apply {
var unconditionalBranchFound = false
for (branch in whenExpression.branches) {
if (branch.condition !is FirElseIfTrueCondition || branch.result.statements.isNotEmpty()) {
if (branch.condition !is FirElseIfTrueCondition) {
branches += branch.accept(this@Fir2IrVisitor, data) as IrBranch
} else {
unconditionalBranchFound = true
if (branch.result.statements.isNotEmpty()) {
branches += branch.accept(this@Fir2IrVisitor, data) as IrBranch
}
}
}
if (whenExpression.isExhaustive && !unconditionalBranchFound) {
val irResult = IrCallImpl(
startOffset, endOffset, irBuiltIns.nothingType, irBuiltIns.noWhenBranchMatchedExceptionSymbol
)
branches += IrElseBranchImpl(
IrConstImpl.boolean(startOffset, endOffset, irBuiltIns.booleanType, true), irResult
)
}
}
if (subjectVariable == null) {
irWhen
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
sealed class Season {
object Warm: Season()
object Cold: Season()
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
sealed class Season {
class Warm: Season()
class Cold: Season()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// CHECK_CASES_COUNT: function=test count=0
// CHECK_IF_COUNT: function=test count=3