Skip to content

Commit

Permalink
Fix issue with forward declarations ordering in relation to collision…
Browse files Browse the repository at this point in the history
…s of type names between Obj-C and Swift.
  • Loading branch information
FilipDolnik committed Apr 18, 2024
1 parent 27f261e commit d175bab
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand All @@ -37,6 +50,6 @@ class FixForwardDeclarationsPhase(
return null
}

return "@protocol " + protocols.joinToString(", ") { it.renderForwardDeclaration() } + ";"
return "@protocol " + protocols.sortedByDescending { it.name }.joinToString(", ") { it.renderForwardDeclaration() } + ";"
}
}

0 comments on commit d175bab

Please sign in to comment.