diff --git a/compiler/testData/builtin-classes.txt b/compiler/testData/builtin-classes.txt index 210d015fd59..f67c7d9da2d 100644 --- a/compiler/testData/builtin-classes.txt +++ b/compiler/testData/builtin-classes.txt @@ -1279,7 +1279,10 @@ public final class String : kotlin.Comparable, kotlin.CharSequenc } public open class Throwable { - /*primary*/ public constructor Throwable(/*0*/ message: kotlin.String? = ..., /*1*/ cause: kotlin.Throwable? = ...) + public constructor Throwable() + public constructor Throwable(/*0*/ message: kotlin.String?) + /*primary*/ public constructor Throwable(/*0*/ message: kotlin.String?, /*1*/ cause: kotlin.Throwable?) + public constructor Throwable(/*0*/ cause: kotlin.Throwable?) public final val cause: kotlin.Throwable? public final fun (): kotlin.Throwable? public final val message: kotlin.String? diff --git a/compiler/testData/codegen/box/intrinsics/throwable.kt b/compiler/testData/codegen/box/intrinsics/throwable.kt new file mode 100644 index 00000000000..c0b573ae74e --- /dev/null +++ b/compiler/testData/codegen/box/intrinsics/throwable.kt @@ -0,0 +1,22 @@ +fun box(): String { + val s: String? = "OK" + val t: Throwable? = Throwable("test", null) + + val z = Throwable(s, t) + if (z.message !== s) return "fail 1: ${z.message}" + if (z.cause !== t) return "fail 2: ${z.cause}" + + val z2 = Throwable(s) + if (z2.message !== s) return "fail 3: ${z2.message}" + if (z2.cause !== null) return "fail 4: ${z2.cause}" + + val z3 = Throwable(t) + if (z3.message != "java.lang.Throwable: test") return "fail 5: ${z3.message}" + if (z3.cause !== t) return "fail 6: ${z2.cause}" + + val z4 = Throwable() + if (z4.message !== null) return "fail 7: ${z4.message}" + if (z4.cause !== null) return "fail 8: ${z4.cause}" + + return z.message!! +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/intrinsics/throwableCallableReference.kt b/compiler/testData/codegen/box/intrinsics/throwableCallableReference.kt new file mode 100644 index 00000000000..9de2b47c97f --- /dev/null +++ b/compiler/testData/codegen/box/intrinsics/throwableCallableReference.kt @@ -0,0 +1,30 @@ +import kotlin.reflect.KFunction2 +import kotlin.reflect.KFunction1 +import kotlin.reflect.KFunction0 + +fun box(): String { + val s: String? = "OK" + val t: Throwable? = Throwable("test", null) + var thr1: KFunction2 = ::Throwable + val z = thr1(s, t) + if (z.message !== s) return "fail 1: ${z.message}" + if (z.cause !== t) return "fail 2: ${z.cause}" + + var thr2: KFunction1 = ::Throwable + + val z2 = thr2(s) + if (z2.message !== s) return "fail 3: ${z2.message}" + if (z2.cause !== null) return "fail 4: ${z2.cause}" + + var thr3: KFunction1 = ::Throwable + val z3 = thr3(t) + if (z3.message != "java.lang.Throwable: test") return "fail 5: ${z3.message}" + if (z3.cause !== t) return "fail 6: ${z2.cause}" + + var thr4: KFunction0 = ::Throwable + val z4 = thr4() + if (z4.message !== null) return "fail 7: ${z4.message}" + if (z4.cause !== null) return "fail 8: ${z4.cause}" + + return z.message!! +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/intrinsics/throwableParamOrder.kt b/compiler/testData/codegen/box/intrinsics/throwableParamOrder.kt new file mode 100644 index 00000000000..7708a6de220 --- /dev/null +++ b/compiler/testData/codegen/box/intrinsics/throwableParamOrder.kt @@ -0,0 +1,17 @@ +var res = "" + +fun getM(): String { + res += "M" + return "OK" +} + +fun getT(): Throwable { + res += "T" + return Throwable("test", null) +} + +fun box(): String { + val z = Throwable(cause = getT(), message = getM()) + if (res != "TM") return "Wrong argument calculation order: $res" + return z.message!! +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java index bba2121594e..b619ffc4b99 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -4765,6 +4765,24 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("throwable.kt") + public void testThrowable() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/intrinsics/throwable.kt"); + doTest(fileName); + } + + @TestMetadata("throwableCallableReference.kt") + public void testThrowableCallableReference() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/intrinsics/throwableCallableReference.kt"); + doTest(fileName); + } + + @TestMetadata("throwableParamOrder.kt") + public void testThrowableParamOrder() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/intrinsics/throwableParamOrder.kt"); + doTest(fileName); + } + @TestMetadata("tostring.kt") public void testTostring() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/intrinsics/tostring.kt"); diff --git a/core/builtins/native/kotlin/Throwable.kt b/core/builtins/native/kotlin/Throwable.kt index c5f978f949b..77eab929f36 100644 --- a/core/builtins/native/kotlin/Throwable.kt +++ b/core/builtins/native/kotlin/Throwable.kt @@ -22,7 +22,13 @@ package kotlin * @param message the detail message string. * @param cause the cause of this throwable. */ -public open class Throwable(val message: String? = null, val cause: Throwable? = null) { +public open class Throwable(val message: String?, val cause: Throwable?) { + + constructor(message: String?) : this(message, null) + + constructor(cause: Throwable?) : this(cause?.toString(), cause) + + constructor() : this(null, null) /** * Prints the stack trace of this throwable to the standard output. */