JS: fix corner case in when translation (fix crash in KT-22544)
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
// CHECK_CASES_COUNT: function=test1 count=2
|
||||||
|
// CHECK_IF_COUNT: function=test1 count=0
|
||||||
|
// CHECK_BREAKS_COUNT: function=test1 count=1
|
||||||
|
|
||||||
|
// CHECK_CASES_COUNT: function=test2 count=2
|
||||||
|
// CHECK_IF_COUNT: function=test2 count=0
|
||||||
|
// CHECK_BREAKS_COUNT: function=test2 count=1
|
||||||
|
|
||||||
|
fun test1(v: Int) {
|
||||||
|
when (v) {
|
||||||
|
1, 2 -> Unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun test2(v: Int) {
|
||||||
|
loop@ while(true) {
|
||||||
|
when (v) {
|
||||||
|
1, 2 -> break@loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
test1(1)
|
||||||
|
test2(1)
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
Generated
+6
@@ -21489,6 +21489,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("switchOptimizationSingleStatementCase.kt")
|
||||||
|
public void testSwitchOptimizationSingleStatementCase() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSingleStatementCase.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("switchOptimizationSparse.kt")
|
@TestMetadata("switchOptimizationSparse.kt")
|
||||||
public void testSwitchOptimizationSparse() throws Exception {
|
public void testSwitchOptimizationSparse() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSparse.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSparse.kt");
|
||||||
|
|||||||
+6
@@ -21489,6 +21489,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("switchOptimizationSingleStatementCase.kt")
|
||||||
|
public void testSwitchOptimizationSingleStatementCase() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSingleStatementCase.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("switchOptimizationSparse.kt")
|
@TestMetadata("switchOptimizationSparse.kt")
|
||||||
public void testSwitchOptimizationSparse() throws Exception {
|
public void testSwitchOptimizationSparse() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSparse.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSparse.kt");
|
||||||
|
|||||||
+6
@@ -21489,6 +21489,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("switchOptimizationSingleStatementCase.kt")
|
||||||
|
public void testSwitchOptimizationSingleStatementCase() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSingleStatementCase.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("switchOptimizationSparse.kt")
|
@TestMetadata("switchOptimizationSparse.kt")
|
||||||
public void testSwitchOptimizationSparse() throws Exception {
|
public void testSwitchOptimizationSparse() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSparse.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSparse.kt");
|
||||||
|
|||||||
@@ -75,7 +75,8 @@ internal class EmptyStatementElimination(private val root: JsStatement) {
|
|||||||
for (case in x.cases) {
|
for (case in x.cases) {
|
||||||
processStatements(case.statements)
|
processStatements(case.statements)
|
||||||
}
|
}
|
||||||
if (x.cases.dropLast(1).none { it is JsDefault } && x.cases.dropLast(1).all { it.statements.isEmpty() }) {
|
if (x.cases.lastOrNull() is JsDefault &&
|
||||||
|
x.cases.dropLast(1).all { it.statements.isEmpty() }) {
|
||||||
hasChanges = true
|
hasChanges = true
|
||||||
val conditionStatement = JsAstUtils.asSyntheticStatement(x.expression)
|
val conditionStatement = JsAstUtils.asSyntheticStatement(x.expression)
|
||||||
ctx.replaceMe(JsBlock(listOf(conditionStatement) + x.cases.last().statements))
|
ctx.replaceMe(JsBlock(listOf(conditionStatement) + x.cases.last().statements))
|
||||||
|
|||||||
@@ -37,7 +37,10 @@ class FunctionPostProcessor(val root: JsFunction) {
|
|||||||
|
|
||||||
fun apply() {
|
fun apply() {
|
||||||
do {
|
do {
|
||||||
val hasChanges = optimizations.fold(false) { existing, f -> existing or f() }
|
var hasChanges = false
|
||||||
|
for (opt in optimizations) {
|
||||||
|
hasChanges = hasChanges or opt()
|
||||||
|
}
|
||||||
} while (hasChanges)
|
} while (hasChanges)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+6
@@ -25251,6 +25251,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("switchOptimizationSingleStatementCase.kt")
|
||||||
|
public void testSwitchOptimizationSingleStatementCase() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSingleStatementCase.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("switchOptimizationSparse.kt")
|
@TestMetadata("switchOptimizationSparse.kt")
|
||||||
public void testSwitchOptimizationSparse() throws Exception {
|
public void testSwitchOptimizationSparse() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSparse.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/when/switchOptimizationSparse.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user