JVM IR: allow custom toArray to have any array type

To avoid breaking Java source compatibility. This problem can be fixed
later once JVM IR is stabilized.

 #KT-43111 Fixed
This commit is contained in:
Alexander Udalov
2020-12-02 16:36:46 +01:00
parent e6a3e38c4d
commit 8ce2e4654b
11 changed files with 199 additions and 20 deletions
+7 -4
View File
@@ -1,6 +1,4 @@
// TARGET_BACKEND: JVM
// The old backend thinks `toArray(): Array<Int?>` is the same as `toArray(): Array<Any?>`
// IGNORE_BACKEND: JVM
// WITH_RUNTIME
// FILE: MyListWithCustomToArray.java
@@ -29,7 +27,7 @@ class MyListSubclass<T>(val list: List<T>): MyListWithCustomToArray<T>() {
get() = list.size
}
class MyCollection<T>(val list: List<T>) : Collection<T> by list {
class MyCollectionWithCustomIntToArray<T>(val list: List<T>) : Collection<T> by list {
fun toArray(): Array<Int?> =
arrayOfNulls<Int>(0)
}
@@ -43,8 +41,13 @@ fun box(): String {
list2.toArray().contentToString().let { if (it != "[null]") return "fail 3: $it" }
list2.toArray(arrayOfNulls<Int>(1)).contentToString().let { if (it != "[null]") return "fail 4: $it" }
val list3 = MyCollection(listOf(2, 3, 9)) as java.util.Collection<*>
val list3 = MyCollectionWithCustomIntToArray(listOf(2, 3, 9)) as java.util.Collection<*>
/*
// This fails with AbstractMethodError at the moment because of a bug where the backend doesn't check the array element type
// of the return type when looking for an implementation of the non-generic parameterless `toArray`.
// See `FunctionDescriptor.isNonGenericToArray`, `IrSimpleFunction.isNonGenericToArray` and KT-43111.
list3.toArray().contentToString().let { if (it != "[2, 3, 9]") return "fail 5: $it" }
list3.toArray(arrayOfNulls<Int>(0)).contentToString().let { if (it != "[2, 3, 9]") return "fail 6: $it" }
*/
return "OK"
}