Skip to content

Commit b38b99c

Browse files
sbogolepovSpace Team
authored andcommitted
[ObjCExport] Fix the handling of unnamed lambda parameters with objcExportBlockExplicitParameterNames
^KT-77488 Fixed
1 parent 1d2d4b2 commit b38b99c

File tree

16 files changed

+112
-74
lines changed

16 files changed

+112
-74
lines changed

native/native.tests/testData/framework/objcexport/expectedLazy/expectedLazy.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,12 @@ __attribute__((swift_name("EnumValuesKt")))
543543
+ (KtNoEnumEntriesEnum *)dceAvoidance __attribute__((swift_name("dceAvoidance()")));
544544
@end
545545

546+
__attribute__((objc_subclassing_restricted))
547+
__attribute__((swift_name("ExplicitBlockParameterNamesKt")))
548+
@interface KtExplicitBlockParameterNamesKt : KtBase
549+
+ (NSString *)callMyParametersExplicitlyPleaseCb:(NSString *(^)(KtInt *, KtInt *, NSString *))cb __attribute__((swift_name("callMyParametersExplicitlyPlease(cb:)")));
550+
@end
551+
546552
__attribute__((swift_name("FunInterface")))
547553
@protocol KtFunInterface
548554
@required

native/native.tests/testData/framework/objcexport/expectedLazy/expectedLazyLegacySuspendUnit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,12 @@ __attribute__((swift_name("EnumValuesKt")))
543543
+ (KtNoEnumEntriesEnum *)dceAvoidance __attribute__((swift_name("dceAvoidance()")));
544544
@end
545545

546+
__attribute__((objc_subclassing_restricted))
547+
__attribute__((swift_name("ExplicitBlockParameterNamesKt")))
548+
@interface KtExplicitBlockParameterNamesKt : KtBase
549+
+ (NSString *)callMyParametersExplicitlyPleaseCb:(NSString *(^)(KtInt *, KtInt *, NSString *))cb __attribute__((swift_name("callMyParametersExplicitlyPlease(cb:)")));
550+
@end
551+
546552
__attribute__((swift_name("FunInterface")))
547553
@protocol KtFunInterface
548554
@required

native/native.tests/testData/framework/objcexport/expectedLazy/expectedLazyNoGenerics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,12 @@ __attribute__((swift_name("EnumValuesKt")))
543543
+ (KtNoEnumEntriesEnum *)dceAvoidance __attribute__((swift_name("dceAvoidance()")));
544544
@end
545545

546+
__attribute__((objc_subclassing_restricted))
547+
__attribute__((swift_name("ExplicitBlockParameterNamesKt")))
548+
@interface KtExplicitBlockParameterNamesKt : KtBase
549+
+ (NSString *)callMyParametersExplicitlyPleaseCb:(NSString *(^)(KtInt *, KtInt *, NSString *))cb __attribute__((swift_name("callMyParametersExplicitlyPlease(cb:)")));
550+
@end
551+
546552
__attribute__((swift_name("FunInterface")))
547553
@protocol KtFunInterface
548554
@required
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fun callMyParametersExplicitlyPlease(cb: (a: Int, b: Int, c: String) -> String): String {
2+
return cb(5, 6, "hello")
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Kt
2+
3+
private func test1() throws {
4+
let actual = ExplicitBlockParameterNamesKt.callMyParametersExplicitlyPlease(cb: { a, b, c in "\(a) \(b) \(c)" })
5+
try assertEquals(actual: actual, expected: "5 6 hello")
6+
}
7+
8+
class ExplicitBlockParameterNamesTests : SimpleTestProvider {
9+
override init() {
10+
super.init()
11+
test("Test1", test1)
12+
}
13+
}

native/objcexport-header-generator/impl/analysis-api/src/org/jetbrains/kotlin/objcexport/translateToObjCFunctionType.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ internal fun ObjCExportContext.translateToObjCFunctionType(type: KaType, returns
1919
else translateToObjCReferenceType(returnType)
2020
},
2121
parameters = listOfNotNull(type.receiverType).plus(type.parameterTypes).map { parameterType ->
22-
val name = if (this@translateToObjCFunctionType.exportSession.configuration.objcExportBlockExplicitParameterNames) {
23-
val parameterName = parameterType.getValueFromParameterNameAnnotation()?.asString() ?: ""
24-
val mangledName = unifyName(parameterName, usedNames)
25-
usedNames += mangledName
26-
mangledName
27-
} else {
28-
""
29-
}
22+
val name = blockParameterName(parameterType, usedNames)
3023
ObjCParameter(
3124
name,
3225
null,
@@ -38,6 +31,16 @@ internal fun ObjCExportContext.translateToObjCFunctionType(type: KaType, returns
3831
return analysisSession.withNullabilityOf(objCBlockPointerType, type)
3932
}
4033

34+
private fun ObjCExportContext.blockParameterName(parameterType: KaType, usedNames: MutableSet<String>): String {
35+
return if (this.exportSession.configuration.objcExportBlockExplicitParameterNames) {
36+
val parameterName = parameterType.getValueFromParameterNameAnnotation()?.asString()
37+
?: return ""
38+
val mangledName = unifyName(parameterName, usedNames)
39+
usedNames += mangledName
40+
mangledName
41+
} else ""
42+
}
43+
4144
/**
4245
* See K1 [org.jetbrains.kotlin.builtins.FunctionTypesKt.getReturnTypeFromFunctionType]
4346
*/

native/objcexport-header-generator/impl/analysis-api/test/org/jetbrains/kotlin/objcexport/testUtils/ObjCExportSessionUtil.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ import org.jetbrains.kotlin.psi.KtElement
1818

1919
inline fun <T> analyzeWithObjCExport(
2020
useSiteKtElement: KtElement,
21-
configuration: KtObjCExportConfiguration = KtObjCExportConfiguration(),
2221
action: ObjCExportContext.() -> T,
2322
): T = analyze(useSiteKtElement) {
2423
val kaSession: KaSession = this
25-
withKtObjCExportSession(configuration) {
24+
withKtObjCExportSession(KtObjCExportConfiguration()) {
2625
val exportSession = this
2726
with(ObjCExportContext(kaSession, exportSession)) {
2827
action.invoke(this)

native/objcexport-header-generator/impl/k1/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportTranslator.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -998,15 +998,7 @@ class ObjCExportTranslatorImpl(
998998
mapReferenceType(functionType.getReturnTypeFromFunctionType(), objCExportScope)
999999
},
10001000
parameterTypes.map {
1001-
val uniqueName = when (objcExportBlockExplicitParameterNames) {
1002-
true -> {
1003-
val parameterName = it.extractParameterNameFromFunctionTypeArgument()?.asString() ?: ""
1004-
val name = unifyName(parameterName, usedNames)
1005-
usedNames += name
1006-
name
1007-
}
1008-
else -> ""
1009-
}
1001+
val uniqueName = blockParameterName(it, usedNames)
10101002
ObjCParameter(
10111003
uniqueName,
10121004
null,
@@ -1016,6 +1008,16 @@ class ObjCExportTranslatorImpl(
10161008
)
10171009
}
10181010

1011+
private fun blockParameterName(parameterType: KotlinType, usedNames: MutableSet<String>): String {
1012+
return if (objcExportBlockExplicitParameterNames) {
1013+
val parameterName = parameterType.extractParameterNameFromFunctionTypeArgument()?.asString()
1014+
?: return ""
1015+
val name = unifyName(parameterName, usedNames)
1016+
usedNames += name
1017+
name
1018+
} else ""
1019+
}
1020+
10191021
private fun mapFunctionType(
10201022
kotlinType: KotlinType,
10211023
objCExportScope: ObjCExportScope,

native/objcexport-header-generator/test/org/jetbrains/kotlin/backend/konan/tests/ObjCExportTypeTranslationTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class ObjCExportTypeTranslationTest(
401401
""".trimIndent()
402402
)
403403

404-
assertEquals("B *(^)(A *) -> void", header.renderTypesOfSymbol("foo"))
404+
assertEquals("B *(^)(A *a) -> void", header.renderTypesOfSymbol("foo"))
405405
}
406406

407407
@Test
@@ -415,7 +415,7 @@ class ObjCExportTypeTranslationTest(
415415
""".trimIndent()
416416
)
417417

418-
assertEquals("C *(^)(A *, B *) -> void", header.renderTypesOfSymbol("foo"))
418+
assertEquals("C *(^)(A *a, B *b) -> void", header.renderTypesOfSymbol("foo"))
419419
}
420420

421421
@Test
@@ -429,7 +429,7 @@ class ObjCExportTypeTranslationTest(
429429
""".trimIndent()
430430
)
431431

432-
assertEquals("C *(^)(A *, B *) -> void", header.renderTypesOfSymbol("foo"))
432+
assertEquals("C *(^)(A *, B *b) -> void", header.renderTypesOfSymbol("foo"))
433433
}
434434

435435
@Test

native/objcexport-header-generator/testData/dependencies/coroutineDispatcherKey/!coroutineDispatcherKey.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ __attribute__((objc_subclassing_restricted))
3838
* kotlin.ExperimentalStdlibApi
3939
*/
4040
@interface KotlinAbstractCoroutineContextKey<B, E> : Base <KotlinCoroutineContextKey>
41-
- (instancetype)initWithBaseKey:(id<KotlinCoroutineContextKey>)baseKey safeCast:(E _Nullable (^)(id<KotlinCoroutineContextElement>))safeCast __attribute__((swift_name("init(baseKey:safeCast:)"))) __attribute__((objc_designated_initializer));
41+
- (instancetype)initWithBaseKey:(id<KotlinCoroutineContextKey>)baseKey safeCast:(E _Nullable (^)(id<KotlinCoroutineContextElement> element))safeCast __attribute__((swift_name("init(baseKey:safeCast:)"))) __attribute__((objc_designated_initializer));
4242
@end
4343

4444

@@ -51,7 +51,7 @@ __attribute__((swift_name("Kotlinx_coroutines_coreCoroutineDispatcher.Key")))
5151
@interface Kotlinx_coroutines_coreCoroutineDispatcherKey : KotlinAbstractCoroutineContextKey<id<KotlinContinuationInterceptor>, Kotlinx_coroutines_coreCoroutineDispatcher *>
5252
+ (instancetype)alloc __attribute__((unavailable));
5353
+ (instancetype)allocWithZone:(struct _NSZone *)zone __attribute__((unavailable));
54-
- (instancetype)initWithBaseKey:(id<KotlinCoroutineContextKey>)baseKey safeCast:(id<KotlinCoroutineContextElement> _Nullable (^)(id<KotlinCoroutineContextElement>))safeCast __attribute__((swift_name("init(baseKey:safeCast:)"))) __attribute__((objc_designated_initializer)) __attribute__((unavailable));
54+
- (instancetype)initWithBaseKey:(id<KotlinCoroutineContextKey>)baseKey safeCast:(id<KotlinCoroutineContextElement> _Nullable (^)(id<KotlinCoroutineContextElement> element))safeCast __attribute__((swift_name("init(baseKey:safeCast:)"))) __attribute__((objc_designated_initializer)) __attribute__((unavailable));
5555
+ (instancetype)key __attribute__((swift_name("init()")));
5656
@property (class, readonly, getter=shared) Kotlinx_coroutines_coreCoroutineDispatcherKey *shared __attribute__((swift_name("shared")));
5757
@end

0 commit comments

Comments
 (0)