Files
kotlin-fork/compiler/testData/codegen/box/casts/nativeCCEMessage/castLocalClass.kt
T
Svyatoslav Scherbina cf581738c3 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.
2022-03-28 08:21:55 +00:00

21 lines
578 B
Kotlin
Vendored

// 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 {
MyLocalObject() as MyObject
} catch (e: Throwable) {
if (e !is ClassCastException) return "fail 1: $e"
if (e.message != "class ${p}box\$MyLocalObject cannot be cast to class ${p}MyObject") return "fail 2: ${e.message}"
return "OK"
}
return "fail 3"
}