Files
kotlin-fork/compiler/testData/codegen/box/toArray/toArrayShouldBePublic.kt
T
pyos 20ea77d55d JVM_IR: refactor ToArrayLowering and make matching more precise
* If `toArray` is inherited from Java, it may take an argument or
    return a value of a flexible type, which looks nullable in IR;
  * The returned array may also have `out` variance of the type
    argument;
  * `Array<T>` is not the same as `Array<Any?>` if `T` is neither `Any`
    nor `in Something`, so presence of `toArray(): Array<T>` does not
    mean we don't need to generate a new `toArray`.
2020-03-06 18:33:25 +01:00

51 lines
1.3 KiB
Kotlin
Vendored

// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// FILE: SingletonCollection.kt
package test
open class SingletonCollection<T>(val value: T) : AbstractCollection<T>() {
override val size = 1
override fun iterator(): Iterator<T> = listOf(value).iterator()
protected override final fun toArray(): Array<Any?> =
arrayOf<Any?>(value)
protected override final fun <E> toArray(a: Array<E>): Array<E> {
a[0] = value as E
return a
}
}
// FILE: DerivedSingletonCollection.kt
package test2
import test.*
class DerivedSingletonCollection<T>(value: T) : SingletonCollection<T>(value)
// FILE: box.kt
import test.*
import test2.*
fun box(): String {
val sc = SingletonCollection(42)
val test1 = (sc as java.util.Collection<Int>).toArray()
if (test1[0] != 42) return "Failed #1"
val test2 = arrayOf<Any?>(0)
(sc as java.util.Collection<Int>).toArray(test2)
if (test2[0] != 42) return "Failed #2"
val dsc = DerivedSingletonCollection(42)
val test3 = (dsc as java.util.Collection<Int>).toArray()
if (test3[0] != 42) return "Failed #3"
val test4 = arrayOf<Any?>(0)
(dsc as java.util.Collection<Int>).toArray(test4)
if (test4[0] != 42) return "Failed #4"
return "OK"
}