Do not leak primitiveness of an array wrapped with asList

A primitive array wrapped in a List with asList had incorrect implementation of toArray method:
while it declares that an object array is returned, it returned a primitive array.
Therefore the methods such as `Collection.toTypedArray()` and its dependents
`ArrayList(collection)`, `Collection + Iterable` might behave incorrectly
having relied on `toTypedArray` returned an object array.

#KT-21828 Fixed
This commit is contained in:
Ilya Gorbunov
2017-12-15 19:26:17 +03:00
parent c55f08a166
commit d8cd926a8c
2 changed files with 8 additions and 1 deletions
@@ -36,6 +36,13 @@ class JsCollectionsTest {
snapshotDoesNotCreateView(arrayOf<Any>("first", "last"), { arrayListOf(*it) })
}
@Suppress("USELESS_CAST")
@Test fun asListHidesPrimitivenessOfArray() {
assertTrue(intArrayOf(1).asList().toTypedArray() as Any is Array<*>, "IntArray primitiveness leaks")
assertTrue(longArrayOf(1).asList().toTypedArray() as Any is Array<*>, "LongArray primitiveness leaks")
assertTrue(charArrayOf(' ').asList().toTypedArray() as Any is Array<*>, "CharArray primitiveness leaks")
}
@Test fun arrayListCapacity() {
val list = ArrayList<Any>(20)
list.ensureCapacity(100)