From 0ec1712f75bca1e497b501dba59e10aa11ab5fe1 Mon Sep 17 00:00:00 2001 From: SvyatoslavScherbina Date: Mon, 25 Feb 2019 13:39:15 +0300 Subject: [PATCH] Fix Objective-C header for deeply nested classes (#2712) Fix #2667 --- .../backend/konan/objcexport/ObjCExportNamer.kt | 17 ++++++++++++++++- backend.native/tests/framework/values/values.kt | 9 +++++++++ .../tests/framework/values/values.swift | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) 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 3afaa340e52..d8858612602 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 @@ -184,7 +184,22 @@ internal class ObjCExportNamerImpl( if (containingDeclaration is ClassDescriptor) { append(getClassOrProtocolSwiftName(containingDeclaration)) - if (!descriptor.isInterface && !containingDeclaration.isInterface) { + val importAsMember = when { + descriptor.isInterface || containingDeclaration.isInterface -> { + // Swift doesn't support neither nested nor outer protocols. + false + } + + this.contains('.') -> { + // Swift doesn't support swift_name with deeply nested names. + // It seems to support "OriginalObjCName.SwiftName" though, + // but this doesn't seem neither documented nor reliable. + false + } + + else -> true + } + if (importAsMember) { append(".").append(descriptor.name.asString()) } else { append(descriptor.name.asString().capitalize()) diff --git a/backend.native/tests/framework/values/values.kt b/backend.native/tests/framework/values/values.kt index 54523d48f7f..d6006f89e30 100644 --- a/backend.native/tests/framework/values/values.kt +++ b/backend.native/tests/framework/values/values.kt @@ -269,3 +269,12 @@ fun Any.same() = this // https://github.com/JetBrains/kotlin-native/issues/2571 val PROPERTY_NAME_MUST_NOT_BE_ALTERED_BY_SWIFT = 111 + +// https://github.com/JetBrains/kotlin-native/issues/2667 +class Deeply { + class Nested { + class Type { + val thirtyTwo = 32 + } + } +} diff --git a/backend.native/tests/framework/values/values.swift b/backend.native/tests/framework/values/values.swift index e08ff1dad15..ffd9d8a7657 100644 --- a/backend.native/tests/framework/values/values.swift +++ b/backend.native/tests/framework/values/values.swift @@ -492,6 +492,7 @@ func testPureSwiftClasses() throws { func testNames() throws { try assertEquals(actual: ValuesKt.PROPERTY_NAME_MUST_NOT_BE_ALTERED_BY_SWIFT, expected: 111) + try assertEquals(actual: Deeply.NestedType().thirtyTwo, expected: 32) } // -------- Execution of the test --------