Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Improve support for -Xobjc-generics and enable it by default #3778

Merged
merged 8 commits into from Jan 27, 2020

Conversation

SvyatoslavScherbina
Copy link
Collaborator

No description provided.

OBJC_INTEROP.md Outdated
@@ -229,26 +229,26 @@ foo {
Objective-C supports "lightweight generics" defined on classes, with a relatively limited feature set. Swift can import
generics defined on classes to help provide additional type information to the compiler.

Generic feature support for Objc and Swift differ from Kotlin, so the translation will inevitably lose some information,
Generic feature support for Obj-C and Swift differ from Kotlin, so the translation will inevitably lose some information,
Copy link
Contributor

Choose a reason for hiding this comment

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

Objective-C

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.


fun isTypeParameterNameReserved(name: String): Boolean = name in reservedTypeParameterNames

private val reservedTypeParameterNames = setOf("id", "NSObject", "NSArray", "NSCopying", "NSNumber", "NSInteger",
Copy link
Contributor

Choose a reason for hiding this comment

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

Where this list comes from?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Generally from here: https://github.com/JetBrains/kotlin-native/pull/3778/files#diff-d13ae8cff625fe7530e3a86b4102ca0fL666

I have some plans to improve this part of code, but it requires more deep changes.

@@ -216,10 +216,10 @@ class StdlibTests : TestProvider {
try applyVoid { $0.add("bar") }
try applyVoid { $0.remove("baz") }
try applyVoid { $0.add("baz") }
try applyVoid { $0.add(TripleVals(first: 1, second: 2, third: 3)) }
try apply { $0.member(TripleVals(first: 1, second: 2, third: 3)) as! TripleVals }
try applyVoid { $0.add(TripleVals<NSNumber>(first: 1, second: 2, third: 3)) }
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, why it is needed to specify NSNumber?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Swift compiler infers Int as type argument, but Objective-C classes can have only "class" (e.g. reference) types as type arguments.

elements.set(index: 0, value: nil)
elements.set(index: 1, value: 42)
elements.set(index: 1, value: 42 as NSNumber)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it always required now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not exactly. Swift doesn't actually have implicit conversion from its integer types to NSNumber. There is ad hoc support for literals, e.g.

let x: NSNumber = 42

works.
But in the discussed line of test the expected type is not NSNumber, so such a literal conversion doesn't apply.

OBJC_INTEROP.md Outdated
@@ -327,6 +312,20 @@ In Kotlin you can provide upper bounds for a generic type. Objective-C also supp
in more complex cases, and is currently not supported in the Kotlin - Objective-C interop. The exception here being a non-null
upper bound will make Objective-C methods/properties non-null.

### To disable

To have the framework header written without generics, add a flag to the compiler config:
Copy link
Contributor

Choose a reason for hiding this comment

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

add the flag? It's precisely that flag.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed. Btw, this sense was originally written by a native speaker ¯\_(ツ)_/¯

as it seems to be referenced from code generated by Swift compiler
under certain circumstances with -Xobjc-generics involved.

Also
 #KT-31665 Fixed
* Avoid emitting accidentally nested protocols
* Unify PSI and descriptors implementation
  (thus fix it with generics involved in ObjCExportLazy)
* Avoid class name translation dependency on its contents
* Simplify the policy
current heuristic is wierd with generics enabled and less important
in this case.
@SvyatoslavScherbina SvyatoslavScherbina merged commit efdd425 into master Jan 27, 2020
@SvyatoslavScherbina SvyatoslavScherbina deleted the objcexport-generics-improvements branch January 27, 2020 06:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants