From d175babd9efc87316a5477c58f9e01a1a9851d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Doln=C3=ADk?= Date: Thu, 18 Apr 2024 13:59:19 +0200 Subject: [PATCH] Fix issue with forward declarations ordering in relation to collisions of type names between Obj-C and Swift. --- .../header/FixForwardDeclarationsPhase.kt | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/SKIE/kotlin-compiler/core/src/commonMain/kotlin/co/touchlab/skie/phases/header/FixForwardDeclarationsPhase.kt b/SKIE/kotlin-compiler/core/src/commonMain/kotlin/co/touchlab/skie/phases/header/FixForwardDeclarationsPhase.kt index 38384097..fa51c9a3 100644 --- a/SKIE/kotlin-compiler/core/src/commonMain/kotlin/co/touchlab/skie/phases/header/FixForwardDeclarationsPhase.kt +++ b/SKIE/kotlin-compiler/core/src/commonMain/kotlin/co/touchlab/skie/phases/header/FixForwardDeclarationsPhase.kt @@ -28,7 +28,20 @@ class FixForwardDeclarationsPhase( return null } - return "@class " + classes.joinToString(", ") { it.renderForwardDeclaration() } + ";" + // The sorting by name length should prevent a bug in the Swift compiler where it confuses Obj-C name and Swift name which leads to compilation errors. + // This can happen if there are two types and the name of the first type is in the form `$FrameworkName$OtherTypeName`. + // In that case the Swift name of this type collides with the Obj-C name of the other type due to Kotlin adding the prefix of the framework name. + // For example, this forward declaration order works: + // ``` + // @protocol SharedSharedFlow; // Is SharedFlow in Kotlin and Swift + // @protocol SharedFlow; // Is Flow in Kotlin and Swift + // ``` + // but this does not: + // ``` + // @protocol SharedFlow; + // @protocol SharedSharedFlow; + // ``` + return "@class " + classes.sortedByDescending { it.name }.joinToString(", ") { it.renderForwardDeclaration() } + ";" } private fun getProtocolForwardDeclarations(): String? { @@ -37,6 +50,6 @@ class FixForwardDeclarationsPhase( return null } - return "@protocol " + protocols.joinToString(", ") { it.renderForwardDeclaration() } + ";" + return "@protocol " + protocols.sortedByDescending { it.name }.joinToString(", ") { it.renderForwardDeclaration() } + ";" } }