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"
}
@@ -0,0 +1,11 @@
class InternalToArray(d: Collection<Any>): Collection<Any> by d {
internal fun toArray(): Array<Int> = null!!
}
class PrivateToArray(d: Collection<Any>): Collection<Any> by d {
private fun toArray(): Array<Int> = null!!
}
class PublicToArray(d: Collection<Any>): Collection<Any> by d {
public fun toArray(): Array<Int> = null!!
}
@@ -0,0 +1,62 @@
@kotlin.Metadata
public final class InternalToArray {
// source: 'customNonGenericToArray.kt'
private synthetic final field $$delegate_0: java.util.Collection
public method <init>(@org.jetbrains.annotations.NotNull p0: java.util.Collection): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(@org.jetbrains.annotations.NotNull p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray$test_module(): java.lang.Integer[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@kotlin.Metadata
public final class PrivateToArray {
// source: 'customNonGenericToArray.kt'
private synthetic final field $$delegate_0: java.util.Collection
public method <init>(@org.jetbrains.annotations.NotNull p0: java.util.Collection): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(@org.jetbrains.annotations.NotNull p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray(): java.lang.Integer[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@kotlin.Metadata
public final class PublicToArray {
// source: 'customNonGenericToArray.kt'
private synthetic final field $$delegate_0: java.util.Collection
public method <init>(@org.jetbrains.annotations.NotNull p0: java.util.Collection): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(@org.jetbrains.annotations.NotNull p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray(): java.lang.Integer[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@@ -0,0 +1,62 @@
@kotlin.Metadata
public final class InternalToArray {
// source: 'customNonGenericToArray.kt'
private synthetic final field $$delegate_0: java.util.Collection
public method <init>(@org.jetbrains.annotations.NotNull p0: java.util.Collection): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(@org.jetbrains.annotations.Nullable p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray(): java.lang.Integer[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@kotlin.Metadata
public final class PrivateToArray {
// source: 'customNonGenericToArray.kt'
private synthetic final field $$delegate_0: java.util.Collection
public method <init>(@org.jetbrains.annotations.NotNull p0: java.util.Collection): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(@org.jetbrains.annotations.Nullable p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray(): java.lang.Integer[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}
@kotlin.Metadata
public final class PublicToArray {
// source: 'customNonGenericToArray.kt'
private synthetic final field $$delegate_0: java.util.Collection
public method <init>(@org.jetbrains.annotations.NotNull p0: java.util.Collection): void
public method add(p0: java.lang.Object): boolean
public method addAll(p0: java.util.Collection): boolean
public method clear(): void
public method contains(@org.jetbrains.annotations.Nullable p0: java.lang.Object): boolean
public method containsAll(@org.jetbrains.annotations.NotNull p0: java.util.Collection): boolean
public method getSize(): int
public method isEmpty(): boolean
public @org.jetbrains.annotations.NotNull method iterator(): java.util.Iterator
public method remove(p0: java.lang.Object): boolean
public method removeAll(p0: java.util.Collection): boolean
public method retainAll(p0: java.util.Collection): boolean
public bridge final method size(): int
public final @org.jetbrains.annotations.NotNull method toArray(): java.lang.Integer[]
public method toArray(p0: java.lang.Object[]): java.lang.Object[]
}