Parcelize, JVM IR: Handle star projected and nullable arrays.
This commit is contained in:
committed by
Alexander Udalov
parent
a6d5c02d9b
commit
714d17ac63
@@ -50,9 +50,16 @@ val IrType.isBoxedArray: Boolean
|
||||
get() = classOrNull?.owner?.fqNameWhenAvailable == StandardNames.FqNames.array.toSafe()
|
||||
|
||||
fun IrType.getArrayElementType(irBuiltIns: IrBuiltIns): IrType =
|
||||
if (isBoxedArray)
|
||||
((this as IrSimpleType).arguments.single() as IrTypeProjection).type
|
||||
else {
|
||||
if (isBoxedArray) {
|
||||
when (val argument = (this as IrSimpleType).arguments.singleOrNull()) {
|
||||
is IrTypeProjection ->
|
||||
argument.type
|
||||
is IrStarProjection ->
|
||||
irBuiltIns.anyNType
|
||||
else ->
|
||||
error("Unexpected array argument type: $argument")
|
||||
}
|
||||
} else {
|
||||
val classifier = this.classOrNull!!
|
||||
irBuiltIns.primitiveArrayElementTypes[classifier]
|
||||
?: throw AssertionError("Primitive array expected: $classifier")
|
||||
|
||||
+4
-1
@@ -119,7 +119,10 @@ class IrParcelSerializerFactory(symbols: AndroidSymbols) {
|
||||
|
||||
val arrayType =
|
||||
if (classifier.defaultType.isPrimitiveArray()) classifier.defaultType else irBuiltIns.arrayClass.typeWith(elementType)
|
||||
return IrArrayParcelSerializer(arrayType, elementType, get(elementType, scope, parcelizeType, strict()))
|
||||
return wrapNullableSerializerIfNeeded(
|
||||
irType,
|
||||
IrArrayParcelSerializer(arrayType, elementType, get(elementType, scope, parcelizeType, strict()))
|
||||
)
|
||||
}
|
||||
|
||||
// This will only be hit if we have a custom serializer for booleans
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.parcelize.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import java.util.Arrays
|
||||
|
||||
@Parcelize
|
||||
data class A(val params: @RawValue Array<*>? = null): Parcelable
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val a1 = A(arrayOf<Int>(1,2,3))
|
||||
val b1 = A()
|
||||
a1.writeToParcel(parcel, 0)
|
||||
b1.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
|
||||
val a2 = readFromParcel<A>(parcel)
|
||||
assert(a2.params != null)
|
||||
assert(Arrays.equals(a1.params!!, a2.params!!))
|
||||
val b2 = readFromParcel<A>(parcel)
|
||||
assert(b1 == b2)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
@file:JvmName("TestKt")
|
||||
package test
|
||||
|
||||
import kotlinx.parcelize.*
|
||||
import android.os.Parcel
|
||||
import android.os.Parcelable
|
||||
import java.util.Arrays
|
||||
|
||||
@Parcelize
|
||||
data class A(val params: Array<Int>? = null): Parcelable
|
||||
|
||||
fun box() = parcelTest { parcel ->
|
||||
val a1 = A(arrayOf(1,2,3))
|
||||
val b1 = A()
|
||||
a1.writeToParcel(parcel, 0)
|
||||
b1.writeToParcel(parcel, 0)
|
||||
|
||||
val bytes = parcel.marshall()
|
||||
parcel.unmarshall(bytes, 0, bytes.size)
|
||||
parcel.setDataPosition(0)
|
||||
|
||||
val a2 = readFromParcel<A>(parcel)
|
||||
assert(a2.params != null)
|
||||
assert(Arrays.equals(a1.params!!, a2.params!!))
|
||||
val b2 = readFromParcel<A>(parcel)
|
||||
assert(b1 == b2)
|
||||
}
|
||||
+10
@@ -199,6 +199,16 @@ public class ParcelizeBoxTestGenerated extends AbstractParcelizeBoxTest {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/kt39981.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt41553.kt")
|
||||
public void testKt41553() throws Exception {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/kt41553.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt41553_2.kt")
|
||||
public void testKt41553_2() throws Exception {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/kt41553_2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("listKinds.kt")
|
||||
public void testListKinds() throws Exception {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/listKinds.kt");
|
||||
|
||||
+10
@@ -199,6 +199,16 @@ public class ParcelizeIrBoxTestGenerated extends AbstractParcelizeIrBoxTest {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/kt39981.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt41553.kt")
|
||||
public void testKt41553() throws Exception {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/kt41553.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("kt41553_2.kt")
|
||||
public void testKt41553_2() throws Exception {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/kt41553_2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("listKinds.kt")
|
||||
public void testListKinds() throws Exception {
|
||||
runTest("plugins/parcelize/parcelize-compiler/testData/box/listKinds.kt");
|
||||
|
||||
Reference in New Issue
Block a user