Files
kotlin/compiler/testData/codegen/box/toArray/toArrayShouldBePublicWithJava.kt
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

70 lines
1.6 KiB
Kotlin
Vendored

// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// IGNORE_LIGHT_ANALYSIS
// 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 fun toArray(): Array<Any?> =
arrayOf<Any?>(value)
protected override fun <E> toArray(a: Array<E>): Array<E> {
a[0] = value as E
return a
}
}
// FILE: JavaSingletonCollection.java
import test.*;
public class JavaSingletonCollection<T> extends SingletonCollection<T> {
public JavaSingletonCollection(T value) {
super(value);
}
}
// FILE: JavaSingletonCollection2.java
import test.*;
public class JavaSingletonCollection2<T> extends SingletonCollection<T> {
public JavaSingletonCollection2(T value) {
super(value);
}
public Object[] toArray() {
return super.toArray();
}
public <E> E[] toArray(E[] arr) {
return super.toArray(arr);
}
}
// FILE: box.kt
import test.*
fun box(): String {
val jsc = JavaSingletonCollection(42) as java.util.Collection<Int>
val test3 = jsc.toArray()
if (test3[0] != 42) return "Failed #3"
val test4 = arrayOf<Any?>(0)
jsc.toArray(test4)
if (test4[0] != 42) return "Failed #4"
val jsc2 = JavaSingletonCollection2(42) as java.util.Collection<Int>
val test5 = jsc2.toArray()
if (test5[0] != 42) return "Failed #5"
val test6 = arrayOf<Any?>(0)
jsc2.toArray(test6)
if (test6[0] != 42) return "Failed #6"
return "OK"
}