diff --git a/compiler/testData/codegen/box/cinterop/objc/forwardDeclarationsCast.kt b/compiler/testData/codegen/box/cinterop/objc/forwardDeclarationsCast.kt new file mode 100644 index 00000000000..c95f2fff2aa --- /dev/null +++ b/compiler/testData/codegen/box/cinterop/objc/forwardDeclarationsCast.kt @@ -0,0 +1,113 @@ +// TARGET_BACKEND: NATIVE +// `import objcnames` somehow works only with NATIVE_STANDALONE test directive +// NATIVE_STANDALONE +// DISABLE_NATIVE: isAppleTarget=false + +// MODULE: a +// FILE: a.def +language = Objective-C +--- +#import +#include + +struct ForwardDeclaredStruct; +@class ForwardDeclaredClass; +@protocol ForwardDeclaredProtocol; + +NSString* consumeProtocol(id s) { + return [NSString stringWithUTF8String:"Protocol"]; +} +NSString* consumeClass(ForwardDeclaredClass* s) { + return [NSString stringWithUTF8String:"Class"]; +} +NSString* consumeStruct(struct ForwardDeclaredStruct* s) { + return [NSString stringWithUTF8String:"Struct"]; +} + +// MODULE: b +// FILE: b.def +language = Objective-C +headers = b.h +--- + +// FILE: b.h +#define NS_FORMAT_ARGUMENT(X) +#import + +@protocol ForwardDeclaredProtocol +@end + +@interface ForwardDeclaredProtocolImpl : NSObject +@end; + + +@interface ForwardDeclaredClass : NSObject +@end; + +struct ForwardDeclaredStruct {}; + +id produceProtocol(); +ForwardDeclaredClass* produceClass(); +struct ForwardDeclaredStruct* produceStruct(); + +// FILE: b.m +#import "b.h" + +@implementation ForwardDeclaredProtocolImpl : NSObject +@end; + +@implementation ForwardDeclaredClass : NSObject +@end; + +id produceProtocol() { + return [ForwardDeclaredProtocolImpl new]; +} + +ForwardDeclaredClass* produceClass() { + return [ForwardDeclaredClass new]; +} + +struct ForwardDeclaredStruct S; + +struct ForwardDeclaredStruct* produceStruct() { + return &S; +} + +// MODULE: lib(a) +// FILE: lib.kt +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + +import cnames.structs.ForwardDeclaredStruct +import objcnames.classes.ForwardDeclaredClass +import objcnames.protocols.ForwardDeclaredProtocolProtocol +import a.* +import kotlinx.cinterop.* + +fun testStruct(s: Any?) = consumeStruct(s as CPointer) +fun testClass(s: Any?) = consumeClass(s as ForwardDeclaredClass) +fun testProtocol(s: Any?) = consumeProtocol(s as ForwardDeclaredProtocolProtocol) + + +fun testClass2Impl(s: Any?) = consumeClass(s as T) +fun testProtocol2Impl(s: Any?) = consumeProtocol(s as T) + +fun testClass2(s: Any?) = testClass2Impl(s) +fun testProtocol2(s: Any?) = testProtocol2Impl(s) + +// MODULE: main(lib, b) +// FILE: main.kt +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + +import a.* +import b.* + + +fun box(): String { + if (testStruct(produceStruct()) != "Struct") throw IllegalStateException() + if (testClass(produceClass()) != "Class") throw IllegalStateException() + if (testProtocol(produceProtocol()) != "Protocol") throw IllegalStateException() + if (testClass2(produceClass()) != "Class") throw IllegalStateException() + if (testProtocol2(produceProtocol()) != "Protocol") throw IllegalStateException() + + return "OK" +} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 3922ad8edbd..b3dd77b0fdd 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -1094,16 +1094,6 @@ createInterop("workerSignals") { } if (PlatformInfo.isAppleTarget(project)) { - createInterop("forwardDeclarationsCastA") { - it.defFile "interop/forwardDeclarationsCast/a.def" - } - - createInterop("forwardDeclarationsCastB") { - it.defFile "interop/forwardDeclarationsCast/b.def" - it.headers "$projectDir/interop/forwardDeclarationsCast/b.h" - it.extraOpts "-Xcompile-source", "$projectDir/interop/forwardDeclarationsCast/b.m" - } - createInterop("objcSmoke") { it.defFile 'interop/objc/objcSmoke.def' it.headers "$projectDir/interop/objc/smoke.h" @@ -1275,14 +1265,6 @@ interopTest("interop_withSpaces") { } } -interopTestBase("interop_forwardDeclarationsCast", false, true) { - enabled = PlatformInfo.isAppleTarget(project) - interop = 'forwardDeclarationsCastA' - interop2 = 'forwardDeclarationsCastB' - lib = 'interop/forwardDeclarationsCast/lib.kt' - source = 'interop/forwardDeclarationsCast/main.kt' -} - dynamicTest("interop_kt43502") { interop = "kt43502" source = "interop/kt43502/main.kt" diff --git a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/a.def b/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/a.def deleted file mode 100644 index 7ede1f093d0..00000000000 --- a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/a.def +++ /dev/null @@ -1,18 +0,0 @@ -language = Objective-C ---- -#import -#include - -struct ForwardDeclaredStruct; -@class ForwardDeclaredClass; -@protocol ForwardDeclaredProtocol; - -NSString* consumeProtocol(id s) { - return [NSString stringWithUTF8String:"Protocol"]; -} -NSString* consumeClass(ForwardDeclaredClass* s) { - return [NSString stringWithUTF8String:"Class"]; -} -NSString* consumeStruct(struct ForwardDeclaredStruct* s) { - return [NSString stringWithUTF8String:"Struct"]; -} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.def b/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.def deleted file mode 100644 index c47329cf3c9..00000000000 --- a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.def +++ /dev/null @@ -1,2 +0,0 @@ -language = Objective-C ---- \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.h b/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.h deleted file mode 100644 index 14829714d74..00000000000 --- a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.h +++ /dev/null @@ -1,18 +0,0 @@ -#define NS_FORMAT_ARGUMENT(X) -#import - -@protocol ForwardDeclaredProtocol -@end - -@interface ForwardDeclaredProtocolImpl : NSObject -@end; - - -@interface ForwardDeclaredClass : NSObject -@end; - -struct ForwardDeclaredStruct {}; - -id produceProtocol(); -ForwardDeclaredClass* produceClass(); -struct ForwardDeclaredStruct* produceStruct(); \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.m b/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.m deleted file mode 100644 index 537d56aab0a..00000000000 --- a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/b.m +++ /dev/null @@ -1,21 +0,0 @@ -#import "b.h" - -@implementation ForwardDeclaredProtocolImpl : NSObject -@end; - -@implementation ForwardDeclaredClass : NSObject -@end; - -id produceProtocol() { - return [ForwardDeclaredProtocolImpl new]; -} - -ForwardDeclaredClass* produceClass() { - return [ForwardDeclaredClass new]; -} - -struct ForwardDeclaredStruct S; - -struct ForwardDeclaredStruct* produceStruct() { - return &S; -} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/lib.kt b/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/lib.kt deleted file mode 100644 index 29947bb30c6..00000000000 --- a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/lib.kt +++ /dev/null @@ -1,18 +0,0 @@ -@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) - -import cnames.structs.ForwardDeclaredStruct -import objcnames.classes.ForwardDeclaredClass -import objcnames.protocols.ForwardDeclaredProtocolProtocol -import a.* -import kotlinx.cinterop.* - -fun testStruct(s: Any?) = consumeStruct(s as CPointer) -fun testClass(s: Any?) = consumeClass(s as ForwardDeclaredClass) -fun testProtocol(s: Any?) = consumeProtocol(s as ForwardDeclaredProtocolProtocol) - - -fun testClass2Impl(s: Any?) = consumeClass(s as T) -fun testProtocol2Impl(s: Any?) = consumeProtocol(s as T) - -fun testClass2(s: Any?) = testClass2Impl(s) -fun testProtocol2(s: Any?) = testProtocol2Impl(s) \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/main.kt b/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/main.kt deleted file mode 100644 index d1467f7bfd0..00000000000 --- a/kotlin-native/backend.native/tests/interop/forwardDeclarationsCast/main.kt +++ /dev/null @@ -1,13 +0,0 @@ -@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) - -import a.* -import b.* - - -fun main() { - if (testStruct(produceStruct()) != "Struct") throw IllegalStateException() - if (testClass(produceClass()) != "Class") throw IllegalStateException() - if (testProtocol(produceProtocol()) != "Protocol") throw IllegalStateException() - if (testClass2(produceClass()) != "Class") throw IllegalStateException() - if (testProtocol2(produceProtocol()) != "Protocol") throw IllegalStateException() -} \ No newline at end of file diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java index 15c0ac48a56..b543f9d6910 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestGenerated.java @@ -5074,6 +5074,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe runTest("compiler/testData/codegen/box/cinterop/objc/direct.kt"); } + @Test + @TestMetadata("forwardDeclarationsCast.kt") + public void testForwardDeclarationsCast() throws Exception { + runTest("compiler/testData/codegen/box/cinterop/objc/forwardDeclarationsCast.kt"); + } + @Test @TestMetadata("kt34467.kt") public void testKt34467() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java index 72f6482c8f7..e12901fcc97 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenBoxTestNoPLGenerated.java @@ -5190,6 +5190,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB runTest("compiler/testData/codegen/box/cinterop/objc/direct.kt"); } + @Test + @TestMetadata("forwardDeclarationsCast.kt") + public void testForwardDeclarationsCast() throws Exception { + runTest("compiler/testData/codegen/box/cinterop/objc/forwardDeclarationsCast.kt"); + } + @Test @TestMetadata("kt34467.kt") public void testKt34467() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java index 8f4397386f7..5d1a8e64591 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestGenerated.java @@ -4958,6 +4958,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/cinterop/objc/direct.kt"); } + @Test + @TestMetadata("forwardDeclarationsCast.kt") + public void testForwardDeclarationsCast() throws Exception { + runTest("compiler/testData/codegen/box/cinterop/objc/forwardDeclarationsCast.kt"); + } + @Test @TestMetadata("kt34467.kt") public void testKt34467() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java index 0623d959e70..75fe6db1691 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenBoxTestNoPLGenerated.java @@ -5075,6 +5075,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT runTest("compiler/testData/codegen/box/cinterop/objc/direct.kt"); } + @Test + @TestMetadata("forwardDeclarationsCast.kt") + public void testForwardDeclarationsCast() throws Exception { + runTest("compiler/testData/codegen/box/cinterop/objc/forwardDeclarationsCast.kt"); + } + @Test @TestMetadata("kt34467.kt") public void testKt34467() throws Exception {