Support mutable collection types in typeOf

flexibleTypes_1_6.kt is fixed for JVM IR in a subsequent commit.

 #KT-35877 Fixed
This commit is contained in:
Alexander Udalov
2021-05-21 20:39:29 +02:00
parent 6e975b3498
commit 0cb905a4b1
19 changed files with 327 additions and 15 deletions
@@ -16,6 +16,7 @@
package kotlin.reflect.jvm.internal;
import kotlin.SinceKotlin;
import kotlin.jvm.internal.*;
import kotlin.reflect.*;
import kotlin.reflect.full.KClassifiers;
@@ -151,6 +152,11 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
return TypeOfImplKt.createPlatformKType(lowerBound, upperBound);
}
// @Override // JPS
public KType mutableCollectionType(KType type) {
return TypeOfImplKt.createMutableCollectionKType(type);
}
// Misc
public static void clearCaches() {
@@ -5,6 +5,10 @@
package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.types.KotlinTypeFactory
import org.jetbrains.kotlin.types.SimpleType
import kotlin.reflect.KType
@@ -19,3 +23,19 @@ internal fun createPlatformKType(lowerBound: KType, upperBound: KType): KType {
)
)
}
internal fun createMutableCollectionKType(type: KType): KType {
val kotlinType = (type as KTypeImpl).type
require(kotlinType is SimpleType) { "Non-simple type cannot be a mutable collection type: $type" }
val classifier = kotlinType.constructor.declarationDescriptor as? ClassDescriptor
?: throw IllegalArgumentException("Non-class type cannot be a mutable collection type: $type")
return KTypeImpl(
KotlinTypeFactory.simpleType(kotlinType, constructor = classifier.readOnlyToMutable().typeConstructor)
)
}
private fun ClassDescriptor.readOnlyToMutable(): ClassDescriptor {
val fqName = JavaToKotlinClassMap.readOnlyToMutable(fqNameUnsafe)
?: throw IllegalArgumentException("Not a readonly collection: $this")
return builtIns.getBuiltInClassByFqName(fqName)
}