-
Notifications
You must be signed in to change notification settings - Fork 6.2k
[KT-48068] Add opt-in support for NSEnum for Kotlin Native iOS via an annotation #5539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+821
−57
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
99127a7
Add opt-in support for NSEnum generation for enum classes for Kotlin …
stefanhaustein e709855
Add swiftName documentation
stefanhaustein 16d934e
Improve formatting for libraries/stdlib/src/kotlin/annotations/Native…
stefanhaustein 2cc890d
improve the meta-annotation warning
stefanhaustein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
libraries/stdlib/src/kotlin/experimental/ExperimentalObjCEnum.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package kotlin.experimental | ||
|
|
||
| /** | ||
| * This annotation marks the experimental [ObjCEnum][kotlin.native.ObjCEnum] annotation that is considered experimental and is not subject to the | ||
| * [general compatibility guarantees](https://kotlinlang.org/docs/reference/evolution/components-stability.html) given for the standard library: | ||
| * the behavior of such annotation may be changed or the annotation may be removed completely in any further release. | ||
| * | ||
| * > Beware using the annotated annotation especially if you're developing a library, since your library might become binary incompatible | ||
| * with the future versions of the standard library. | ||
| */ | ||
| @RequiresOptIn | ||
| @Target(AnnotationTarget.ANNOTATION_CLASS) | ||
| @Retention(AnnotationRetention.BINARY) | ||
| @MustBeDocumented | ||
| @SinceKotlin("2.3") | ||
| public annotation class ExperimentalObjCEnum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
native/native.tests/testData/framework/objcexport/nativeEnum.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package nativeEnum | ||
|
|
||
| import kotlin.native.ObjCEnum | ||
| import kotlin.experimental.ExperimentalObjCEnum | ||
|
|
||
| @OptIn(kotlin.experimental.ExperimentalObjCEnum::class) | ||
| @ObjCEnum("OBJCFoo", swiftName="SwiftFoo") | ||
| enum class MyKotlinEnum { | ||
| ALPHA, COPY, BAR_FOO | ||
| } |
21 changes: 21 additions & 0 deletions
21
native/native.tests/testData/framework/objcexport/nativeEnum.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import Kt | ||
|
|
||
|
|
||
| private func testNativeEnumValues() throws { | ||
| let ktEnum = MyKotlinEnum.barFoo | ||
| let nsEnum = ktEnum.nsEnum | ||
|
|
||
| switch(nsEnum) { | ||
| case SwiftFoo.alpha: try fail() | ||
| case .barFoo: try assertEquals(actual: nsEnum, expected: ktEnum.nsEnum) | ||
| case .theCopy: try fail() | ||
| } | ||
| } | ||
|
|
||
| class NativeEnumTests : SimpleTestProvider { | ||
| override init() { | ||
| super.init() | ||
|
|
||
| test("TestNativeEnumValues", testNativeEnumValues) | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
...erator/impl/analysis-api/src/org/jetbrains/kotlin/objcexport/ObjCExportTranslatedClass.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package org.jetbrains.kotlin.objcexport | ||
|
|
||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCClass | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCTopLevel | ||
|
|
||
| class ObjCExportTranslatedClass( | ||
| val auxiliaryDeclarations: List<ObjCTopLevel>, | ||
| val objCClass: ObjCClass, | ||
| ) | ||
|
|
||
| fun ObjCExportTranslatedClass(objCClass: ObjCClass?) = objCClass?.let { ObjCExportTranslatedClass(emptyList(), it) } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ader-generator/impl/analysis-api/src/org/jetbrains/kotlin/objcexport/getNSEnumTypeName.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package org.jetbrains.kotlin.objcexport | ||
|
|
||
| import org.jetbrains.kotlin.analysis.api.symbols.KaClassSymbol | ||
| import org.jetbrains.kotlin.backend.konan.KonanFqNames | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportNSEnumTypeName | ||
| import org.jetbrains.kotlin.name.ClassId | ||
|
|
||
| /** Returns the NSEnum type for the given enum type if the corresponding annotation is set; null otherwise */ | ||
| fun ObjCExportContext.getNSEnumTypeName(symbol: KaClassSymbol): ObjCExportNSEnumTypeName? { | ||
| val classId = ClassId.topLevel(KonanFqNames.objCEnum) | ||
| val annotation = symbol.annotations[classId].firstOrNull() ?: return null | ||
|
|
||
| val name = annotation.findArgument("name")?.resolveStringConstantValue()?.ifEmpty { null } | ||
| ?: (getObjCClassOrProtocolName(symbol).objCName + "NSEnum") | ||
| val swiftName = annotation.findArgument("swiftName")?.resolveStringConstantValue()?.ifEmpty { null } ?: name | ||
| return ObjCExportNSEnumTypeName(swiftName = swiftName, objCName = name) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, we should check that the annotation is not applied to non-
enumclasses.A new frontend checker is needed for that. See an example here:
kotlin/compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeThreadLocalChecker.kt
Line 24 in 360c000
To avoid stalling this PR, this can be done separately.