Fix "kotlin.TypeCastException has no message on Native"

#KT-35544 Fixed

Merge-request: KT-MR-6392
Merged-by: Vladimir Sukharev <Vladimir.Sukharev@jetbrains.com>
This commit is contained in:
Vladimir Sukharev
2022-06-03 07:40:26 +00:00
committed by Space
parent 6fc27c22f4
commit eabf6dac7f
3 changed files with 13 additions and 3 deletions
@@ -1556,9 +1556,10 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
with(functionGenerationContext) {
ifThen(not(genInstanceOf(srcArg, dstClass))) {
if (dstClass.defaultType.isObjCObjectType()) {
val dstFullClassName = dstClass.fqNameWhenAvailable?.toString() ?: dstClass.name.toString()
callDirect(
context.ir.symbols.throwTypeCastException.owner,
emptyList(),
listOf(srcArg, context.llvm.staticData.kotlinStringLiteral(dstFullClassName).llvm),
Lifetime.GLOBAL,
null
)
@@ -0,0 +1,9 @@
import kotlin.test.*
import objcTests.*
@Test
fun testKT35544() {
val exception = assertFailsWith<TypeCastException> {
123 as NSString
}
assertEquals("class kotlin.Int cannot be cast to class objcTests.NSString", exception.message)
}
@@ -32,8 +32,8 @@ fun ThrowClassCastException(instance: Any, typeInfo: NativePtr): Nothing {
}
@ExportForCppRuntime
fun ThrowTypeCastException(): Nothing {
throw TypeCastException()
fun ThrowTypeCastException(instance: Any, typeName: String): Nothing {
throw TypeCastException("${instance::class} cannot be cast to class $typeName")
}
@ExportForCppRuntime