Skip to content

Conversation

@stefanhaustein
Copy link

Basically implementing the feature suggested here: https://youtrack.jetbrains.com/issue/KT-48068

@SvyatoslavScherbina
Copy link
Contributor

/test-private

@kotlin-safe-merge
Copy link

Failed to process command due to an unexpected exception.

@SvyatoslavScherbina
Copy link
Contributor

The K2 (Analysis API, aka AA) implementation is missing, which makes the new test fail in this mode.
Run

./gradlew :native:objcexport-header-generator:testAnalysisApi --tests "org.jetbrains.kotlin.backend.konan.tests.ObjCExportHeaderGeneratorTest"

to reproduce.

Related code:

@SvyatoslavScherbina
Copy link
Contributor

Another thing to fix: adding a declaration to stdlib requires updating the ABI dump.
Run the following command to do that:

./gradlew :tools:binary-compatibility-validator:cleanTest :tools:binary-compatibility-validator:test --tests "*" -Poverwrite.output=true

@stefanhaustein stefanhaustein changed the title Add opt-in support for NSEnum for Kotlin Native iOS via an annotation [KT-48068] Add opt-in support for NSEnum for Kotlin Native iOS via an annotation Nov 4, 2025
@stefanhaustein stefanhaustein requested a review from a team as a code owner November 4, 2025 13:21
@stefanhaustein
Copy link
Author

The K2 (Analysis API, aka AA) implementation is missing, which makes the new test fail in this mode. Run

I have added this, but AA emits the a comment about the annotation whereas K1 does not... Any suggestions how to resolve this? Current state is the AA output

enum class Foo {
// Note that the order is not alphabetic on purpose, ensuring that tests fail if we can't rely on the
// order to be preserved for the ordinal value.
ALPHA, COPY, BAR_FOO,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no test checking how having @ObjCName on enum class or its entries affects the generated NS_ENUM.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a few more cases here -- let me know if I should add any additional specific one...

I tried adding @ObjCName on the literal itself, but that's broken for AA in general, even for "regular" enum literals, and the output won't match, so I can't even document the error easily.

I'll file a bug for this separately

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KT-82838 @ObjCName for enum literals inconsistent in AnalysisApi

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried adding @ObjCName on the literal itself, but that's broken for AA in general, even for "regular" enum literals, and the output won't match, so I can't even document the error easily.

Ah, I see. Thanks!
What can be done: a separate @Test fun can be added to ObjCExportHeaderGeneratorTest for this case and marked with @TodoAnalysisApi. This way, the test will be muted when checking the AA implementation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added annotations to the smaller test and disabled aa there -- I think for the meantime AA is covered by the larger test with enum annotations sufficiently?

Copy link
Contributor

@SvyatoslavScherbina SvyatoslavScherbina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think the implementation is good now. But there are a few more things to be done to actually get it merged:

  1. Rebase it to master, squash the commits and write a commit message as recommended here.
  2. Because of KT-82670, an unrelated test has to be adjusted:
--- a/analysis/low-level-api-fir/testData/lazyResolveStdlibSources/wrappedInt.txt
+++ b/analysis/low-level-api-fir/testData/lazyResolveStdlibSources/wrappedInt.txt
@@ -824,7 +824,7 @@ FILE: [ResolvedTo(BODY_RESOLVE)] TypeReference.kt
         }
 
         public open override [ResolvedTo(BODY_RESOLVE)] fun hashCode(): R|kotlin/Int| {
-            ^hashCode this@R|kotlin/jvm/internal/TypeReference|.R|kotlin/jvm/internal/TypeReference.classifier|.R|kotlin/Any.hashCode|().R|kotlin/Int.times|(Int(31)).R|kotlin/Int.plus|(this@R|kotlin/jvm/internal/TypeReference|.R|kotlin/jvm/internal/TypeReference.arguments|.R|kotlin/Any.hashCode|()).R|kotlin/Int.times|(Int(31)).R|kotlin/Int.plus|(this@R|kotlin/jvm/internal/TypeReference|.R|kotlin/jvm/internal/TypeReference.flags|.R|kotlin/Int.hashCode|())
+            ^hashCode this@R|kotlin/jvm/internal/TypeReference|.R|kotlin/jvm/internal/TypeReference.classifier|.R|kotlin/Any.hashCode|().R|kotlin/Int.times|(Int(31)).R|kotlin/Int.plus|(this@R|kotlin/jvm/internal/TypeReference|.R|kotlin/jvm/internal/TypeReference.arguments|.R|kotlin/Any.hashCode|()).R|kotlin/Int.times|(Int(31)).R|kotlin/Int.plus|(this@R|kotlin/jvm/internal/TypeReference|.R|kotlin/jvm/internal/TypeReference.flags|.R|kotlin/Any.hashCode|())
         }
 
         public open override [ResolvedTo(BODY_RESOLVE)] fun toString(): R|kotlin/String| {
  1. I'll run the required tests after that. Hopefully, nothing more will require fixing.
  2. We'll also need some approvals from the codeowners for the changes in stdlib. I'll request them.

@stefanhaustein
Copy link
Author

stefanhaustein commented Dec 2, 2025

Ok, I think the implementation is good now. But there are a few more things to be done to actually get it merged:

  1. Rebase it to master, squash the commits and write a commit message as recommended here.

I think I have rebased the fork successfully but would like to confirm that I won't screw up the branch with the wrong git command.... (I mainly use perforce for work... O:)

Can you confirm that a valid option to squash is to run git rebase -i HEAD~29 in the fork and then replace /every/ "pick" there with "squash" -- including commits from previous times I synced the fork to head? Would you recommend something else?

@SvyatoslavScherbina
Copy link
Contributor

I think I have rebased the fork successfully but would like to confirm that I won't screw up the branch with the wrong git command.... (I mainly use perforce for work... O:)

It seems that you actually merged the master branch, not rebased to master.

Can you confirm that a valid option to squash is to run git rebase -i HEAD~29 in the fork and then replace /every/ "pick" there with "squash" -- including commits from previous times I synced the fork to head? Would you recommend something else?

That won't work because of the merge.
First, you need to get rid of the merge commits. Luckily, git rebase helps:

git rebase -i 5952e77c40174e23afb9b2606d3ee3dded3b4b9f

And then you can replace "pick" with "squash" (or "fixup").

…Native iOS via an annotation

Addresses KT-48068

If not specified otherwise via @ObjCEnum, the NSEnum type name will match the Kotlin Enum
ObjC / Swift name with "NSEnum" appended.

The new type is accesible via the "nsEnum" property (the same way as the ordinal value via "ordinal")
dimonchik0036
dimonchik0036 previously approved these changes Dec 4, 2025
package kotlin.experimental

/**
* This annotation marks the experimental [ObjCEnum][kotlin.native.ObjCEnum] annotation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a description of what this status entails.

Also, is there a path to stabilization?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have expanded the explanation based on the documentation for ExperimentalNativeApi

Not sure about the stabilization path, I'd expect something along the lines that this would lose experimental status if no issues are observed for a certain amount of time and there is significant usage, but I don't see something similar elsewere...

stefanhaustein and others added 2 commits December 5, 2025 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants