Native: improve ClassCastException message for local classes

Previously it was like "null cannot be cast to MyObject" or
"MyObject cannot be cast to null", because `KClass.qualifiedName` is
`null` for local classes (including anonymous ones).

Use `KClass.toString()` instead, for both object actual dynamic type
and cast target type. This string representation is generally more
meaningful for such cases, and contains useful details for local
classes.
This commit is contained in:
Svyatoslav Scherbina
2022-03-18 17:50:57 +03:00
committed by Space
parent 5c612078e5
commit cf581738c3
13 changed files with 191 additions and 1 deletions
@@ -0,0 +1,20 @@
// TARGET_BACKEND: NATIVE
class MyObject
// Test infrastructure can move declarations to a package. So we need a prefix for class names in exception messages:
val p = MyObject::class.qualifiedName!!.removeSuffix("MyObject")
fun box(): String {
class MyLocalObject
try {
MyObject() as MyLocalObject
} catch (e: Throwable) {
if (e !is ClassCastException) return "fail 1: $e"
if (e.message != "class ${p}MyObject cannot be cast to class ${p}box\$MyLocalObject") return "fail 2: ${e.message}"
return "OK"
}
return "fail 3"
}