diff --git a/compiler/testData/codegen/box/casts/toNativePointers.kt b/compiler/testData/codegen/box/casts/toNativePointers.kt new file mode 100644 index 00000000000..49e4165445f --- /dev/null +++ b/compiler/testData/codegen/box/casts/toNativePointers.kt @@ -0,0 +1,32 @@ +// TARGET_BACKEND: NATIVE + +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + +import kotlinx.cinterop.* + +fun getNp() : NativePtr? = null +fun getOp() : COpaquePointer? = null + +fun box() : String { + if ((null as NativePtr?) != null) return "FAIL 1.1" + if ((null as? NativePtr) != null) return "FAIL 1.2" + if (null !is NativePtr?) return "FAIL 1.3" + if (null is NativePtr) return "FAIL 1.4" + + if ((null as COpaquePointer?) != null) return "FAIL 2.1" + if ((null as? COpaquePointer) != null) return "FAIL 2.2" + if (null !is COpaquePointer?) return "FAIL 2.3" + if (null is COpaquePointer) return "FAIL 2.4" + + if ((getNp() as NativePtr?) != null) return "FAIL 3.1" + if ((getNp() as? NativePtr) != null) return "FAIL 3.2" + if (getNp() !is NativePtr?) return "FAIL 3.3" + if (getNp() is NativePtr) return "FAIL 3.4" + + if ((getOp() as COpaquePointer?) != null) return "FAIL 4.1" + if ((getOp() as? COpaquePointer) != null) return "FAIL 4.2" + if (getOp() !is COpaquePointer?) return "FAIL 4.3" + if (getOp() is COpaquePointer) return "FAIL 4.4" + + return "OK" +} \ No newline at end of file diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 596713cc5de..a8bee200702 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -1628,7 +1628,8 @@ internal class CodeGeneratorVisitor( positionAtEnd(bbExit) appendingTo(resultInstanceOfBB) { br(bbExit) } appendingTo(resultNullBB) { br(bbExit) } - val result = phi(value.type.toLLVMType(llvm)) + require(resultNull.type == resultInstanceOf.type) + val result = phi(resultNull.type) addPhiIncoming(result, resultNullBB to resultNull, resultInstanceOfBB to resultInstanceOf) result } diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestGenerated.java index 9a44bb2b719..e8e954d07c4 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestGenerated.java @@ -3682,6 +3682,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe runTest("compiler/testData/codegen/box/casts/objectToPrimitiveWithAssertion.kt"); } + @Test + @TestMetadata("toNativePointers.kt") + public void testToNativePointers() throws Exception { + runTest("compiler/testData/codegen/box/casts/toNativePointers.kt"); + } + @Test @TestMetadata("unitAsAny.kt") public void testUnitAsAny() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestNoPLGenerated.java index d19d51678b5..62c4d20efdb 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/FirNativeCodegenBoxTestNoPLGenerated.java @@ -3760,6 +3760,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB runTest("compiler/testData/codegen/box/casts/objectToPrimitiveWithAssertion.kt"); } + @Test + @TestMetadata("toNativePointers.kt") + public void testToNativePointers() throws Exception { + runTest("compiler/testData/codegen/box/casts/toNativePointers.kt"); + } + @Test @TestMetadata("unitAsAny.kt") public void testUnitAsAny() throws Exception { 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 23910348790..512833f975b 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 @@ -3644,6 +3644,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/casts/objectToPrimitiveWithAssertion.kt"); } + @Test + @TestMetadata("toNativePointers.kt") + public void testToNativePointers() throws Exception { + runTest("compiler/testData/codegen/box/casts/toNativePointers.kt"); + } + @Test @TestMetadata("unitAsAny.kt") public void testUnitAsAny() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestNoPLGenerated.java index 8376ddec6b1..bab8ca3cc9f 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestNoPLGenerated.java @@ -3683,6 +3683,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT runTest("compiler/testData/codegen/box/casts/objectToPrimitiveWithAssertion.kt"); } + @Test + @TestMetadata("toNativePointers.kt") + public void testToNativePointers() throws Exception { + runTest("compiler/testData/codegen/box/casts/toNativePointers.kt"); + } + @Test @TestMetadata("unitAsAny.kt") public void testUnitAsAny() throws Exception {