Support Nothing type in typeOf

The proper support will come in KT-15518, but that would be a breaking
change even for stable Kotlin without kotlin-reflect. Before that issue
is fixed, represent Nothing in types with the Void class, and use a flag
in the no-reflect implementation to remember that it's not actually the
Void class itself.

 #KT-39166 Fixed
This commit is contained in:
Alexander Udalov
2021-07-07 13:04:59 +02:00
parent 02774fae0c
commit ddfa94e7e9
15 changed files with 338 additions and 15 deletions
@@ -157,6 +157,11 @@ public class ReflectionFactoryImpl extends ReflectionFactory {
return TypeOfImplKt.createMutableCollectionKType(type);
}
// @Override // JPS
public KType nothingType(KType type) {
return TypeOfImplKt.createNothingType(type);
}
// Misc
public static void clearCaches() {
@@ -11,6 +11,7 @@ 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 org.jetbrains.kotlin.types.typeUtil.builtIns
import kotlin.reflect.KType
internal fun createPlatformKType(lowerBound: KType, upperBound: KType): KType {
@@ -39,3 +40,11 @@ private fun ClassDescriptor.readOnlyToMutable(): ClassDescriptor {
?: throw IllegalArgumentException("Not a readonly collection: $this")
return builtIns.getBuiltInClassByFqName(fqName)
}
internal fun createNothingType(type: KType): KType {
val kotlinType = (type as KTypeImpl).type
require(kotlinType is SimpleType) { "Non-simple type cannot be a Nothing type: $type" }
return KTypeImpl(
KotlinTypeFactory.simpleType(kotlinType, constructor = kotlinType.builtIns.nothing.typeConstructor)
)
}