From 11a8fafb2ad08e9411878bd5e25d4e328b7165e6 Mon Sep 17 00:00:00 2001 From: SvyatoslavScherbina Date: Thu, 1 Aug 2019 14:14:27 +0300 Subject: [PATCH] Don't use 'Type' as nested class name when producing framework (#3217) Fix #3167. --- .../backend/konan/objcexport/ObjCExportNamer.kt | 9 +++++++-- .../tests/framework/values/expectedLazy.h | 15 +++++++++++++++ backend.native/tests/framework/values/values.kt | 7 +++++++ .../tests/framework/values/values.swift | 1 + 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportNamer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportNamer.kt index db51be80919..84b64a19214 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportNamer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportNamer.kt @@ -120,7 +120,7 @@ internal open class ObjCExportNameTranslatorImpl( } if (importAsMember) { - append(".").append(ownName) + append(".").append(helper.mangleSwiftNestedClassName(ownName)) } else { append(ownName.capitalize()) } @@ -154,6 +154,11 @@ private class ObjCExportNamingHelper( fun getFileClassName(file: KtFile): ObjCExportNamer.ClassOrProtocolName = getFileClassName(file.name) + + fun mangleSwiftNestedClassName(name: String): String = when (name) { + "Type" -> "${name}_" // See https://github.com/JetBrains/kotlin-native/issues/3167 + else -> name + } } internal class ObjCExportNamerImpl( @@ -332,7 +337,7 @@ internal class ObjCExportNamerImpl( } val ownName = descriptor.name.asString().toIdentifier() if (importAsMember) { - append(".").append(ownName) + append(".").append(helper.mangleSwiftNestedClassName(ownName)) } else { append(ownName.capitalize()) } diff --git a/backend.native/tests/framework/values/expectedLazy.h b/backend.native/tests/framework/values/expectedLazy.h index f1dd07724bd..c44a87399cf 100644 --- a/backend.native/tests/framework/values/expectedLazy.h +++ b/backend.native/tests/framework/values/expectedLazy.h @@ -344,6 +344,21 @@ __attribute__((swift_name("WithGenericDeeply.NestedType"))) @property (readonly) int32_t thirtyThree __attribute__((swift_name("thirtyThree"))); @end; +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("TypeOuter"))) +@interface ValuesTypeOuter : KotlinBase +- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer)); ++ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead"))); +@end; + +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("TypeOuter.Type_"))) +@interface ValuesTypeOuterType_ : KotlinBase +- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer)); ++ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead"))); +@property (readonly) int32_t thirtyFour __attribute__((swift_name("thirtyFour"))); +@end; + __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("CKeywords"))) @interface ValuesCKeywords : KotlinBase diff --git a/backend.native/tests/framework/values/values.kt b/backend.native/tests/framework/values/values.kt index bf2b07c9485..0416a623831 100644 --- a/backend.native/tests/framework/values/values.kt +++ b/backend.native/tests/framework/values/values.kt @@ -290,6 +290,13 @@ class WithGenericDeeply() { } } +// https://github.com/JetBrains/kotlin-native/issues/3167 +class TypeOuter { + class Type { + val thirtyFour = 34 + } +} + data class CKeywords(val float: Float, val `enum`: Int, var goto: Boolean) interface Base1 { diff --git a/backend.native/tests/framework/values/values.swift b/backend.native/tests/framework/values/values.swift index 060ea73480b..05ccbfd0171 100644 --- a/backend.native/tests/framework/values/values.swift +++ b/backend.native/tests/framework/values/values.swift @@ -532,6 +532,7 @@ func testNames() throws { try assertEquals(actual: Deeply.NestedType().thirtyTwo, expected: 32) try assertEquals(actual: WithGenericDeeply.NestedType().thirtyThree, expected: 33) try assertEquals(actual: CKeywords(float: 1.0, enum : 42, goto: true).goto_, expected: true) + try assertEquals(actual: TypeOuter.Type_().thirtyFour, expected: 34) } class Base123 : Base23, ExtendedBase1 {