From ac8cbcafb471ecd87e28b3862be7a905a5b31091 Mon Sep 17 00:00:00 2001 From: Pavel Kunyavskiy Date: Tue, 28 Mar 2023 18:19:49 +0200 Subject: [PATCH] [K/N] Do not export all operator component functions in data classes ^KT-57507 --- .../backend/konan/objcexport/ObjCExportMapper.kt | 11 +++++------ .../tests/objcexport/dataClassComponentMethods.kt | 5 +++++ .../objcexport/dataClassComponentMethods.swift | 11 +++++++++++ .../backend.native/tests/objcexport/expectedLazy.h | 13 +++++++++++++ .../objcexport/expectedLazyLegacySuspendUnit.h | 13 +++++++++++++ .../tests/objcexport/expectedLazyNoGenerics.h | 13 +++++++++++++ 6 files changed, 60 insertions(+), 6 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportMapper.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportMapper.kt index 949eb4e9434..81f573aec3a 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportMapper.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportMapper.kt @@ -78,12 +78,11 @@ private fun isSealedClassConstructor(descriptor: ConstructorDescriptor) = descri * Check that given [method] is a synthetic .componentN() method of a data class. */ private fun isComponentNMethod(method: CallableMemberDescriptor): Boolean { - if (method.kind == CallableMemberDescriptor.Kind.SYNTHESIZED) { - val parent = method.containingDeclaration - if (parent is ClassDescriptor && parent.isData && DataClassResolver.isComponentLike(method.name)) { - // componentN method of data class. - return true - } + if ((method as? FunctionDescriptor)?.isOperator != true) return false + val parent = method.containingDeclaration + if (parent is ClassDescriptor && parent.isData && DataClassResolver.isComponentLike(method.name)) { + // componentN method of data class. + return true } return false } diff --git a/kotlin-native/backend.native/tests/objcexport/dataClassComponentMethods.kt b/kotlin-native/backend.native/tests/objcexport/dataClassComponentMethods.kt index 79cb5c01346..02c277ec8e1 100644 --- a/kotlin-native/backend.native/tests/objcexport/dataClassComponentMethods.kt +++ b/kotlin-native/backend.native/tests/objcexport/dataClassComponentMethods.kt @@ -24,3 +24,8 @@ class RegularClassWithComponentMethods { fun component1() = 5 fun component4() = 6 + +data class DataClassWithStrangeNames(val component124: Int, val componentABC: Int) { + operator fun component15() = component124 + fun component16() = component124 +} diff --git a/kotlin-native/backend.native/tests/objcexport/dataClassComponentMethods.swift b/kotlin-native/backend.native/tests/objcexport/dataClassComponentMethods.swift index 151685f06e5..63543df03af 100644 --- a/kotlin-native/backend.native/tests/objcexport/dataClassComponentMethods.swift +++ b/kotlin-native/backend.native/tests/objcexport/dataClassComponentMethods.swift @@ -27,6 +27,16 @@ private func testTopLevelComponentMethodsAreAccessible() throws { try assertEquals(actual: DataClassComponentMethodsKt.component4(), expected: 6) } +private func testComponentExportedOrNot() throws { + try assertFalse(class_respondsToSelector(object_getClass(DataClassWithStrangeNames.self), NSSelectorFromString("component1"))); + try assertFalse(class_respondsToSelector(object_getClass(DataClassWithStrangeNames.self), NSSelectorFromString("component2"))); + try assertFalse(class_respondsToSelector(object_getClass(DataClassWithStrangeNames.self), NSSelectorFromString("component15"))); + let r = DataClassWithStrangeNames(component124: 1, componentABC:2) + try assertEquals(actual: r.component124, expected: 1) + try assertEquals(actual: r.componentABC, expected: 2) + try assertEquals(actual: r.component16(), expected: 1) +} + class DataClassComponentMethodsTests : SimpleTestProvider { override init() { super.init() @@ -34,5 +44,6 @@ class DataClassComponentMethodsTests : SimpleTestProvider { test("testCustomComponentMethodsAreAccessible", testCustomComponentMethodsAreAccessible) test("testRegularComponentMethodsAreAccessible", testRegularComponentMethodsAreAccessible) test("testTopLevelComponentMethodsAreAccessible", testTopLevelComponentMethodsAreAccessible) + test("testComponentExportedOrNot", testComponentExportedOrNot) } } \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/objcexport/expectedLazy.h b/kotlin-native/backend.native/tests/objcexport/expectedLazy.h index bd23619939c..02b115dc4b4 100644 --- a/kotlin-native/backend.native/tests/objcexport/expectedLazy.h +++ b/kotlin-native/backend.native/tests/objcexport/expectedLazy.h @@ -450,6 +450,19 @@ __attribute__((swift_name("RegularClassWithComponentMethods"))) - (int32_t)component3 __attribute__((swift_name("component3()"))); @end +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("DataClassWithStrangeNames"))) +@interface KtDataClassWithStrangeNames : KtBase +- (instancetype)initWithComponent124:(int32_t)component124 componentABC:(int32_t)componentABC __attribute__((swift_name("init(component124:componentABC:)"))) __attribute__((objc_designated_initializer)); +- (int32_t)component16 __attribute__((swift_name("component16()"))); +- (BOOL)isEqual:(id _Nullable)other __attribute__((swift_name("isEqual(_:)"))); +- (NSUInteger)hash __attribute__((swift_name("hash()"))); +- (NSString *)description __attribute__((swift_name("description()"))); +- (KtDataClassWithStrangeNames *)doCopyComponent124:(int32_t)component124 componentABC:(int32_t)componentABC __attribute__((swift_name("doCopy(component124:componentABC:)"))); +@property (readonly) int32_t component124 __attribute__((swift_name("component124"))); +@property (readonly) int32_t componentABC __attribute__((swift_name("componentABC"))); +@end + __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("DataClassComponentMethodsKt"))) @interface KtDataClassComponentMethodsKt : KtBase diff --git a/kotlin-native/backend.native/tests/objcexport/expectedLazyLegacySuspendUnit.h b/kotlin-native/backend.native/tests/objcexport/expectedLazyLegacySuspendUnit.h index 1eb42394bff..687cdd67740 100644 --- a/kotlin-native/backend.native/tests/objcexport/expectedLazyLegacySuspendUnit.h +++ b/kotlin-native/backend.native/tests/objcexport/expectedLazyLegacySuspendUnit.h @@ -450,6 +450,19 @@ __attribute__((swift_name("RegularClassWithComponentMethods"))) - (int32_t)component3 __attribute__((swift_name("component3()"))); @end +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("DataClassWithStrangeNames"))) +@interface KtDataClassWithStrangeNames : KtBase +- (instancetype)initWithComponent124:(int32_t)component124 componentABC:(int32_t)componentABC __attribute__((swift_name("init(component124:componentABC:)"))) __attribute__((objc_designated_initializer)); +- (int32_t)component16 __attribute__((swift_name("component16()"))); +- (BOOL)isEqual:(id _Nullable)other __attribute__((swift_name("isEqual(_:)"))); +- (NSUInteger)hash __attribute__((swift_name("hash()"))); +- (NSString *)description __attribute__((swift_name("description()"))); +- (KtDataClassWithStrangeNames *)doCopyComponent124:(int32_t)component124 componentABC:(int32_t)componentABC __attribute__((swift_name("doCopy(component124:componentABC:)"))); +@property (readonly) int32_t component124 __attribute__((swift_name("component124"))); +@property (readonly) int32_t componentABC __attribute__((swift_name("componentABC"))); +@end + __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("DataClassComponentMethodsKt"))) @interface KtDataClassComponentMethodsKt : KtBase diff --git a/kotlin-native/backend.native/tests/objcexport/expectedLazyNoGenerics.h b/kotlin-native/backend.native/tests/objcexport/expectedLazyNoGenerics.h index 1293266c9b0..8c8e9ad7f39 100644 --- a/kotlin-native/backend.native/tests/objcexport/expectedLazyNoGenerics.h +++ b/kotlin-native/backend.native/tests/objcexport/expectedLazyNoGenerics.h @@ -450,6 +450,19 @@ __attribute__((swift_name("RegularClassWithComponentMethods"))) - (int32_t)component3 __attribute__((swift_name("component3()"))); @end +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("DataClassWithStrangeNames"))) +@interface KtDataClassWithStrangeNames : KtBase +- (instancetype)initWithComponent124:(int32_t)component124 componentABC:(int32_t)componentABC __attribute__((swift_name("init(component124:componentABC:)"))) __attribute__((objc_designated_initializer)); +- (int32_t)component16 __attribute__((swift_name("component16()"))); +- (BOOL)isEqual:(id _Nullable)other __attribute__((swift_name("isEqual(_:)"))); +- (NSUInteger)hash __attribute__((swift_name("hash()"))); +- (NSString *)description __attribute__((swift_name("description()"))); +- (KtDataClassWithStrangeNames *)doCopyComponent124:(int32_t)component124 componentABC:(int32_t)componentABC __attribute__((swift_name("doCopy(component124:componentABC:)"))); +@property (readonly) int32_t component124 __attribute__((swift_name("component124"))); +@property (readonly) int32_t componentABC __attribute__((swift_name("componentABC"))); +@end + __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("DataClassComponentMethodsKt"))) @interface KtDataClassComponentMethodsKt : KtBase