From 871180cdc0ba3d14008225409352c57be5c71a5c Mon Sep 17 00:00:00 2001 From: Svyatoslav Kuzmich Date: Wed, 3 Jul 2019 14:35:09 +0300 Subject: [PATCH] [JS IR BE] Fix setting names of Throwable subclasses without primary constructor --- .../kotlin/ir/backend/js/lower/ThrowableLowering.kt | 9 ++++----- .../testData/box/expression/try/exceptionToString.kt | 7 +++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ThrowableLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ThrowableLowering.kt index c721ea90cbe..7cc390b9b60 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ThrowableLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/ThrowableLowering.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.ir.backend.js.lower import org.jetbrains.kotlin.backend.common.FileLoweringPass +import org.jetbrains.kotlin.backend.common.lower.callsSuper import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext @@ -82,12 +83,8 @@ class ThrowableLowering(val context: JsIrBackendContext) : FileLoweringPass { } override fun visitConstructor(declaration: IrConstructor, data: IrDeclarationParent): IrStatement { - declaration.transformChildren(this, data) - - if (!declaration.isPrimary) return declaration - val klass = data as IrClass - if (klass.defaultType.isThrowableTypeOrSubtype()) { + if (klass.defaultType.isThrowableTypeOrSubtype() && declaration.callsSuper(context.irBuiltIns)) { (declaration.body as? IrBlockBody)?.let { it.statements += JsIrBuilder.buildCall(propertySetter, unitType).apply { putValueArgument(0, JsIrBuilder.buildGetValue(klass.thisReceiver!!.symbol)) @@ -97,6 +94,8 @@ class ThrowableLowering(val context: JsIrBackendContext) : FileLoweringPass { } } + declaration.transformChildren(this, data) + return declaration } diff --git a/js/js.translator/testData/box/expression/try/exceptionToString.kt b/js/js.translator/testData/box/expression/try/exceptionToString.kt index a5f8cbe356c..ec84d0b1f8f 100644 --- a/js/js.translator/testData/box/expression/try/exceptionToString.kt +++ b/js/js.translator/testData/box/expression/try/exceptionToString.kt @@ -3,6 +3,11 @@ package foo class MyException(m: String? = null): Exception(m) class MyException2(m: String? = null): Throwable(m) + +class MyException3: Throwable { + constructor(m: String? = null) : super(m) {} +} + // TODO: add direct inheritors of Throwable: // - with cause only, in the primary constructor @@ -30,6 +35,8 @@ fun box(): String { check(MyException("aaa"), "MyException: aaa") check(MyException2(), "MyException2: null") check(MyException2("aaa"), "MyException2: aaa") + check(MyException3(), "MyException3: null") + check(MyException3("aaa"), "MyException3: aaa") return "OK" }