kotlin
kotlin copied to clipboard
[K/N][Tests] Fix crashes when casting to an Obj-C class companion
^KT-65260
The behavior still doesn't seem entirely right:
NSNumber as NSObject.Companionsuch a cast should fail, but it succeeds.
I have a doubt about this case, corresponding to ObjC.
// This result is YES, ObjC class object is instance of meta class.
BOOL result = [NSNumber isKindOfClass:object_getClass(NSObject.class)];
So ThrowTypeCastException branch cannot be reached.
The behavior still doesn't seem entirely right:
NSNumber as NSObject.Companionsuch a cast should fail, but it succeeds.
I have a doubt about this case, corresponding to ObjC.
// This result is YES, ObjC class object is instance of meta class. BOOL result = [NSNumber isKindOfClass:object_getClass(NSObject.class)];
Yes, but semantically NSNumber as NSObject.Companion is not the same.
[NSNumber isKindOfClass:object_getClass(NSObject.class)] is equivalent to NSNumber is NSObjectMeta, which should be true.
But NSNumber is NSObject.Companion has no equivalent in Objective-C. NSObject.Companion is a class that Kotlin synthesizes to reflect the concept of the class object.
NSNumber is NSNumber.Companion == true contradicts general Kotlin rules.
Consider e.g. the following example:
open class A {
companion object : AMeta()
}
open class AMeta
open class B : A() {
companion object : BMeta()
}
open class BMeta : AMeta()
fun main() {
val bAny: Any = B
println(bAny is A.Companion)
}
It prints false.
A.Companion is a class with only one instance -- A. So B can't be an instance of A.Companion.
Same with NSObject.Companion and NSNumber: NSNumber cant be an instance of NSObject.Companion, only NSObject is.
Thank you for the contribution! Merged manually as 450bb1c7c0f90c2a07ceb8208f0f23268decedf0.