From 7578dbf8f2ed0e2c4ca7a74c9b00f3a641a23a71 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kuzmich Date: Wed, 15 Aug 2018 15:47:02 +0300 Subject: [PATCH] [JS BE] KT-22053 Fix constructor delegation of immediate subtype of Error --- .../js/test/semantics/BoxJsTestGenerated.java | 5 +++ .../test/semantics/IrBoxJsTestGenerated.java | 5 +++ .../translate/declaration/ClassTranslator.kt | 4 ++- .../box/expression/try/exceptionToString.kt | 1 - .../testData/box/expression/try/kt22053.kt | 32 +++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 js/js.translator/testData/box/expression/try/kt22053.kt diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java index 83c536bf419..4a8facb06a2 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java @@ -3004,6 +3004,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { runTest("js/js.translator/testData/box/expression/try/exceptionToString.kt"); } + @TestMetadata("kt22053.kt") + public void testKt22053() throws Exception { + runTest("js/js.translator/testData/box/expression/try/kt22053.kt"); + } + @TestMetadata("multipleCatchBlocks.kt") public void testMultipleCatchBlocks() throws Exception { runTest("js/js.translator/testData/box/expression/try/multipleCatchBlocks.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java index 6b708e6b888..60d21c289d0 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrBoxJsTestGenerated.java @@ -3004,6 +3004,11 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest { runTest("js/js.translator/testData/box/expression/try/exceptionToString.kt"); } + @TestMetadata("kt22053.kt") + public void testKt22053() throws Exception { + runTest("js/js.translator/testData/box/expression/try/kt22053.kt"); + } + @TestMetadata("multipleCatchBlocks.kt") public void testMultipleCatchBlocks() throws Exception { runTest("js/js.translator/testData/box/expression/try/multipleCatchBlocks.kt"); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassTranslator.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassTranslator.kt index 9759b5c81cb..a130c6cbf69 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassTranslator.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassTranslator.kt @@ -291,7 +291,9 @@ class ClassTranslator private constructor( val delegationClassDescriptor = (resolvedCall?.resultingDescriptor as? ClassConstructorDescriptor)?.constructedClass if (resolvedCall != null && !KotlinBuiltIns.isAny(delegationClassDescriptor!!)) { - if (JsDescriptorUtils.isImmediateSubtypeOfError(classDescriptor)) { + val isDelegationToCurrentClass = delegationClassDescriptor == classDescriptor + val isDelegationToErrorClass = JsDescriptorUtils.isImmediateSubtypeOfError(classDescriptor) && !isDelegationToCurrentClass + if (isDelegationToErrorClass) { superCallGenerators += { val innerContext = context().innerBlock() ClassInitializerTranslator.emulateSuperCallToNativeError( diff --git a/js/js.translator/testData/box/expression/try/exceptionToString.kt b/js/js.translator/testData/box/expression/try/exceptionToString.kt index a6e6a0629ee..f14cca54d49 100644 --- a/js/js.translator/testData/box/expression/try/exceptionToString.kt +++ b/js/js.translator/testData/box/expression/try/exceptionToString.kt @@ -5,7 +5,6 @@ package foo class MyException(m: String? = null): Exception(m) class MyException2(m: String? = null): Throwable(m) // TODO: add direct inheritors of Throwable: -// - with secondary constructors // - with cause only, in the primary constructor fun check(e: Throwable, expectedString: String) { diff --git a/js/js.translator/testData/box/expression/try/kt22053.kt b/js/js.translator/testData/box/expression/try/kt22053.kt new file mode 100644 index 00000000000..6a4f3faa60a --- /dev/null +++ b/js/js.translator/testData/box/expression/try/kt22053.kt @@ -0,0 +1,32 @@ +// IGNORE_BACKEND: JS_IR +// EXPECTED_REACHABLE_NODES: 1126 +package foo + +class MyThrowable(message: String?) : Throwable("through primary: " + message) { + public var initOrder = "" + + constructor() : this(message = "secondary") { + initOrder += "2" + } + constructor(i: Int) : this() { + initOrder += "3" + } + + init { initOrder += "1" } +} + +fun box(): String { + val mt1 = MyThrowable("primary") + assertEquals(mt1.toString(), "MyThrowable: through primary: primary") + assertEquals(mt1.initOrder, "1") + + val mt2 = MyThrowable() + assertEquals(mt2.toString(), "MyThrowable: through primary: secondary") + assertEquals(mt2.initOrder, "12") + + val mt3 = MyThrowable(1) + assertEquals(mt3.toString(), "MyThrowable: through primary: secondary") + assertEquals(mt3.initOrder, "123") + + return "OK" +}