PSI2IR: KT-49485 chunk ktConditions to avoid stackoverflow

This commit is contained in:
Xin Wang
2022-01-20 17:26:35 +08:00
committed by Dmitry Petrov
parent b40227b66e
commit ebcc2cc3b2
4 changed files with 50 additions and 11 deletions
@@ -112,18 +112,21 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
break
}
var irBranchCondition: IrExpression? = null
for (ktCondition in ktEntry.conditions) {
val irCondition =
if (irSubject != null)
generateWhenConditionWithSubject(ktCondition, irSubject, expression.subjectExpression)
else
generateWhenConditionNoSubject(ktCondition)
irBranchCondition = irBranchCondition?.let { context.whenComma(it, irCondition) } ?: irCondition
}
// chunk conditions to avoid stackoverflow later when IrWhen is visited
for (ktConditions in ktEntry.conditions.toList().chunked(64)) {
var irBranchCondition: IrExpression? = null
for (ktCondition in ktConditions) {
val irCondition =
if (irSubject != null)
generateWhenConditionWithSubject(ktCondition, irSubject, expression.subjectExpression)
else
generateWhenConditionNoSubject(ktCondition)
irBranchCondition = irBranchCondition?.let { context.whenComma(it, irCondition) } ?: irCondition
}
val irBranchResult = ktEntry.expression!!.genExpr()
irWhen.branches.add(IrBranchImpl(irBranchCondition!!, irBranchResult))
val irBranchResult = ktEntry.expression!!.genExpr()
irWhen.branches.add(IrBranchImpl(irBranchCondition!!, irBranchResult))
}
}
if (!hasExplicitElseBranch) {
addElseBranchForExhaustiveWhenIfNeeded(irWhen, expression)