diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index f95975bc59d..f42afd1c4b0 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -5048,6 +5048,16 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/casts/mutableCollections/weirdMutableCasts.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/casts/nativeCCEMessage") + @TestDataPath("$PROJECT_ROOT") + public class NativeCCEMessage { + @Test + public void testAllFilesPresentInNativeCCEMessage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts/nativeCCEMessage"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); + } + } } @Nested diff --git a/compiler/testData/codegen/box/casts/nativeCCEMessage/castAnonymousClass.kt b/compiler/testData/codegen/box/casts/nativeCCEMessage/castAnonymousClass.kt new file mode 100644 index 00000000000..17b6b486951 --- /dev/null +++ b/compiler/testData/codegen/box/casts/nativeCCEMessage/castAnonymousClass.kt @@ -0,0 +1,19 @@ +// 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 { + try { + object {} as MyObject + } catch (e: Throwable) { + if (e !is ClassCastException) return "fail 1: $e" + if (e.message != "class ${p}box$1 cannot be cast to class ${p}MyObject") return "fail 2: ${e.message}" + + return "OK" + } + + return "fail 3" +} diff --git a/compiler/testData/codegen/box/casts/nativeCCEMessage/castGlobalClass.kt b/compiler/testData/codegen/box/casts/nativeCCEMessage/castGlobalClass.kt new file mode 100644 index 00000000000..e7c58bdad77 --- /dev/null +++ b/compiler/testData/codegen/box/casts/nativeCCEMessage/castGlobalClass.kt @@ -0,0 +1,19 @@ +// 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 { + try { + MyObject() as String + } catch (e: Throwable) { + if (e !is ClassCastException) return "fail 1: $e" + if (e.message != "class ${p}MyObject cannot be cast to class kotlin.String") return "fail 2: ${e.message}" + + return "OK" + } + + return "fail 3" +} diff --git a/compiler/testData/codegen/box/casts/nativeCCEMessage/castLocalClass.kt b/compiler/testData/codegen/box/casts/nativeCCEMessage/castLocalClass.kt new file mode 100644 index 00000000000..9aadb9d7baf --- /dev/null +++ b/compiler/testData/codegen/box/casts/nativeCCEMessage/castLocalClass.kt @@ -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 { + 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" +} diff --git a/compiler/testData/codegen/box/casts/nativeCCEMessage/castToLocalClass.kt b/compiler/testData/codegen/box/casts/nativeCCEMessage/castToLocalClass.kt new file mode 100644 index 00000000000..2adb286558a --- /dev/null +++ b/compiler/testData/codegen/box/casts/nativeCCEMessage/castToLocalClass.kt @@ -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" +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index decad35f44e..e4e9a1ceb47 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -4928,6 +4928,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/casts/mutableCollections/weirdMutableCasts.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/casts/nativeCCEMessage") + @TestDataPath("$PROJECT_ROOT") + public class NativeCCEMessage { + @Test + public void testAllFilesPresentInNativeCCEMessage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts/nativeCCEMessage"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); + } + } } @Nested diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 030127f48c3..f8cc9dae48e 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -5048,6 +5048,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/casts/mutableCollections/weirdMutableCasts.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/casts/nativeCCEMessage") + @TestDataPath("$PROJECT_ROOT") + public class NativeCCEMessage { + @Test + public void testAllFilesPresentInNativeCCEMessage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts/nativeCCEMessage"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); + } + } } @Nested diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index a72cbfbe964..4411f8997e1 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -4307,6 +4307,19 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/casts/mutableCollections/weirdMutableCasts.kt"); } } + + @TestMetadata("compiler/testData/codegen/box/casts/nativeCCEMessage") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class NativeCCEMessage extends AbstractLightAnalysisModeTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); + } + + public void testAllFilesPresentInNativeCCEMessage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts/nativeCCEMessage"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); + } + } } @TestMetadata("compiler/testData/codegen/box/checkcastOptimization") diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java index f54fad66bfb..b1516aadd33 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java @@ -3614,6 +3614,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/casts/mutableCollections/weirdMutableCasts.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/casts/nativeCCEMessage") + @TestDataPath("$PROJECT_ROOT") + public class NativeCCEMessage { + @Test + public void testAllFilesPresentInNativeCCEMessage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts/nativeCCEMessage"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); + } + } } @Nested diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java index 287edbc7878..33c9fbcb50e 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java @@ -3656,6 +3656,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/casts/mutableCollections/weirdMutableCasts.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/casts/nativeCCEMessage") + @TestDataPath("$PROJECT_ROOT") + public class NativeCCEMessage { + @Test + public void testAllFilesPresentInNativeCCEMessage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts/nativeCCEMessage"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); + } + } } @Nested diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index b43fb1fc6dc..8e27e27e935 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -3237,6 +3237,19 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/casts/mutableCollections/weirdMutableCasts.kt"); } } + + @TestMetadata("compiler/testData/codegen/box/casts/nativeCCEMessage") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class NativeCCEMessage extends AbstractIrCodegenBoxWasmTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath); + } + + public void testAllFilesPresentInNativeCCEMessage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts/nativeCCEMessage"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true); + } + } } @TestMetadata("compiler/testData/codegen/box/checkcastOptimization") diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt index 1f827611e16..175d4ce53f3 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt @@ -28,7 +28,7 @@ internal fun ThrowArrayIndexOutOfBoundsException(): Nothing { @ExportForCppRuntime fun ThrowClassCastException(instance: Any, typeInfo: NativePtr): Nothing { val clazz = KClassImpl(typeInfo) - throw ClassCastException("${instance::class.qualifiedName} cannot be cast to ${clazz.qualifiedName}") + throw ClassCastException("${instance::class} cannot be cast to $clazz") } @ExportForCppRuntime diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java index 67df341b0ad..ae4e24f0350 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java @@ -3738,6 +3738,42 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/casts/mutableCollections/weirdMutableCasts.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/casts/nativeCCEMessage") + @TestDataPath("$PROJECT_ROOT") + @Tag("codegen") + @UseExtTestCaseGroupProvider() + public class NativeCCEMessage { + @Test + public void testAllFilesPresentInNativeCCEMessage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/casts/nativeCCEMessage"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true); + } + + @Test + @TestMetadata("castAnonymousClass.kt") + public void testCastAnonymousClass() throws Exception { + runTest("compiler/testData/codegen/box/casts/nativeCCEMessage/castAnonymousClass.kt"); + } + + @Test + @TestMetadata("castGlobalClass.kt") + public void testCastGlobalClass() throws Exception { + runTest("compiler/testData/codegen/box/casts/nativeCCEMessage/castGlobalClass.kt"); + } + + @Test + @TestMetadata("castLocalClass.kt") + public void testCastLocalClass() throws Exception { + runTest("compiler/testData/codegen/box/casts/nativeCCEMessage/castLocalClass.kt"); + } + + @Test + @TestMetadata("castToLocalClass.kt") + public void testCastToLocalClass() throws Exception { + runTest("compiler/testData/codegen/box/casts/nativeCCEMessage/castToLocalClass.kt"); + } + } } @Nested