From ee74342fbed127379adc04dbfd6fff8621ad75ad Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 10 Jan 2017 12:35:04 +0300 Subject: [PATCH] JS: fix compiler crash when optimizing JS AST with empty do..while statement. See KT-15513 --- .../breakContinueInExpressions/breakFromOuter.kt | 3 --- .../kotlin/js/inline/clean/DoWhileGuardElimination.kt | 2 +- .../js/test/optimizer/DoWhileGuardEliminationTest.kt | 2 ++ .../js/test/semantics/JsCodegenBoxTestGenerated.java | 8 +------- .../do-while-guard-elimination/emptyDoWhile.optimized.js | 5 +++++ .../do-while-guard-elimination/emptyDoWhile.original.js | 5 +++++ 6 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 js/js.translator/testData/js-optimizer/do-while-guard-elimination/emptyDoWhile.optimized.js create mode 100644 js/js.translator/testData/js-optimizer/do-while-guard-elimination/emptyDoWhile.original.js diff --git a/compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakFromOuter.kt b/compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakFromOuter.kt index af81659efc5..7566e7c14a9 100644 --- a/compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakFromOuter.kt +++ b/compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakFromOuter.kt @@ -1,6 +1,3 @@ -// TODO: muted automatically, investigate should it be ran for JS or not -// IGNORE_BACKEND: JS - fun box(): String { OUTER@while (true) { var x = "" diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/DoWhileGuardElimination.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/DoWhileGuardElimination.kt index 8e1490948bb..4e837e41c57 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/DoWhileGuardElimination.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/inline/clean/DoWhileGuardElimination.kt @@ -62,7 +62,7 @@ internal class DoWhileGuardElimination(private val root: JsStatement) { val body = x.body val guard = when (body) { is JsBlock -> { - val firstStatement = body.statements.first() + val firstStatement = body.statements.firstOrNull() if (firstStatement is JsLabel && body.statements.size == 1) { firstStatement } diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/DoWhileGuardEliminationTest.kt b/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/DoWhileGuardEliminationTest.kt index 01183662217..a12acbf6914 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/DoWhileGuardEliminationTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/optimizer/DoWhileGuardEliminationTest.kt @@ -24,4 +24,6 @@ class DoWhileGuardEliminationTest : BasicOptimizerTest("do-while-guard-eliminati @Test fun innerContinue() = box() @Test fun innerBreakInLoopWithoutLabel() = box() + + @Test fun emptyDoWhile() = box() } \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 1b66e22edc3..e8c78ed62f9 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -5033,13 +5033,7 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { @TestMetadata("breakFromOuter.kt") public void testBreakFromOuter() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/controlStructures/breakContinueInExpressions/breakFromOuter.kt"); - try { - doTest(fileName); - } - catch (Throwable ignore) { - return; - } - throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); + doTest(fileName); } @TestMetadata("breakInDoWhile.kt") diff --git a/js/js.translator/testData/js-optimizer/do-while-guard-elimination/emptyDoWhile.optimized.js b/js/js.translator/testData/js-optimizer/do-while-guard-elimination/emptyDoWhile.optimized.js new file mode 100644 index 00000000000..52a535d885f --- /dev/null +++ b/js/js.translator/testData/js-optimizer/do-while-guard-elimination/emptyDoWhile.optimized.js @@ -0,0 +1,5 @@ +function box() { + do { + } while (false); + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/js-optimizer/do-while-guard-elimination/emptyDoWhile.original.js b/js/js.translator/testData/js-optimizer/do-while-guard-elimination/emptyDoWhile.original.js new file mode 100644 index 00000000000..52a535d885f --- /dev/null +++ b/js/js.translator/testData/js-optimizer/do-while-guard-elimination/emptyDoWhile.original.js @@ -0,0 +1,5 @@ +function box() { + do { + } while (false); + return "OK" +} \ No newline at end of file