KJS IR: Fix KT-45738 - Consider recursive checkForPrimitiveOrPattern

Recursive results from checkForPrimitiveOrPattern were ignored. If it found a case/condition that could not be optimized the resulting "false" was not propagated. This would lead to a "optimized" when without all conditions.
- see KT-45738
- The return is now lifted out of the when to make it more obvious what is going on.
- New test for mixed multiple conditions in when
This commit is contained in:
Andi Wenger
2021-04-28 18:32:27 +02:00
committed by Roman
parent caff279255
commit f9d2ca68ce
10 changed files with 72 additions and 7 deletions
@@ -69,16 +69,17 @@ class SwitchOptimizer(private val context: JsGenerationContext, private val last
if (constExpr !is IrConst<*>) return false
if (!constExpr.isTrueConstant()) return false
when (branchExpr) {
return when (branchExpr) {
is IrWhen -> checkForPrimitiveOrPattern(branchExpr, constants)
is IrCall -> {
val constant = tryToExtractEqeqeqConst(branchExpr) ?: return false
constants += constant
is IrCall -> when (val constant = tryToExtractEqeqeqConst(branchExpr)) {
null -> false
else -> {
constants += constant
true
}
}
else -> return false
else -> false
}
return true
}
if (!checkBranchIsOrPattern(thenBranch.result, thenBranch.condition)) return false