diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java index 4d8f1978704..70b7d989a36 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java @@ -66,7 +66,6 @@ public final class MiscTest extends AbstractExpressionTest { checkOutput("KT-1052.kt", "true\n"); } - public void testKt740_1() throws Exception { checkFooBoxIsTrue("KT-740.kt"); } @@ -123,6 +122,18 @@ public final class MiscTest extends AbstractExpressionTest { fooBoxTest(); } + public void testElvisReturnSimple() throws Exception { + checkFooBoxIsOk(); + } + + public void testElvisReturnNested() throws Exception { + checkFooBoxIsOk(); + } + + public void testElvisWithThrow() throws Exception { + checkFooBoxIsOk(); + } + public void testExtensionLiteralCalledInsideExtensionFunction() throws Exception { fooBoxTest(); } @@ -135,12 +146,10 @@ public final class MiscTest extends AbstractExpressionTest { checkOutput("mainFunInNestedPackage.kt", "ayee"); } - public void testPropertiesWithExplicitlyDefinedAccessorsWithoutBodies() throws Exception { fooBoxTest(); } - public void testExclExcl() throws Exception { fooBoxTest(); } diff --git a/js/js.translator/testData/expression/misc/cases/elvisReturnNested.kt b/js/js.translator/testData/expression/misc/cases/elvisReturnNested.kt new file mode 100644 index 00000000000..01fc58fb2b0 --- /dev/null +++ b/js/js.translator/testData/expression/misc/cases/elvisReturnNested.kt @@ -0,0 +1,27 @@ +/* + * Issue: KT-4159 Kotlin to JS compiler crashes on code with ?: return + * + * Expression like "val s1 : String = s ?: return null" causes compiler to crash + */ + +package foo + +fun assertEquals(expected: T, actual: T) { + if (expected != actual) throw Exception("expected: $expected, actual: $actual") +} + +fun firstNotNullLen(s1 : String?, s2 : String?, s3 : String?) : Int { + val len = (s1?.length() ?: s2?.length()) ?: + (s2?.length() ?: s3?.length()) ?: + return 0 + return len +} + +fun box(): String { + assertEquals(1, firstNotNullLen("a", null, null)) + assertEquals(2, firstNotNullLen(null, "ab", null)) + assertEquals(3, firstNotNullLen(null, null, "abc")) + assertEquals(0, firstNotNullLen(null, null, null)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/expression/misc/cases/elvisReturnSimple.kt b/js/js.translator/testData/expression/misc/cases/elvisReturnSimple.kt new file mode 100644 index 00000000000..73dcb25a7f4 --- /dev/null +++ b/js/js.translator/testData/expression/misc/cases/elvisReturnSimple.kt @@ -0,0 +1,32 @@ +/* + * Issue: KT-4159 Kotlin to JS compiler crashes on code with ?: return + * + * Expression like "val s1 : String = s ?: return null" causes compiler to crash + */ + +package foo + +fun assertEquals(expected: T, actual: T) { + if (expected != actual) throw Exception("expected: $expected, actual: $actual") +} + +fun stringLen(s : String?) : Int { + val s1 : String = s ?: return 0 + return s1.length +} + +fun stringReturnInLeftLen(s : String?) : Int { + val s1 : String = (if (s != null) { return s.length() } else { null }) ?: return 0 +} + +fun box(): String { + assertEquals(3, stringLen("box")) + assertEquals(0, stringLen("")) + assertEquals(0, stringLen(null)) + + assertEquals(3, stringReturnInLeftLen("box")) + assertEquals(0, stringReturnInLeftLen("")) + assertEquals(0, stringReturnInLeftLen(null)) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/expression/misc/cases/elvisWithThrow.kt b/js/js.translator/testData/expression/misc/cases/elvisWithThrow.kt new file mode 100644 index 00000000000..ad665ca5062 --- /dev/null +++ b/js/js.translator/testData/expression/misc/cases/elvisWithThrow.kt @@ -0,0 +1,35 @@ +package foo + +fun assertEquals(expected: T, actual: T) { + if (expected != actual) throw Exception("expected: $expected, actual: $actual") +} + +var i = 0 +fun bar(): Any? { + i++ + return null +} + +native +val Exception.message: String = noImpl + +fun box(): String { + val a: String? = null + try { + a ?: throw Exception("a") + } + catch (e: Exception) { + assertEquals("a", e.message) + } + + try { + bar() ?: throw Exception("bar()") + } + catch (e: Exception) { + assertEquals("bar()", e.message) + } + + assertEquals(1, i) + + return "OK" +} \ No newline at end of file