From ffaca279bcb56e938b6118ec4d9e2dc60e41c40b Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Fri, 12 Jul 2019 14:15:56 +0300 Subject: [PATCH] Minor. Turn TypeBasedStarProjectionImpl into StarProjectionForAbsentTypeParameter It's needed to avoid abusing of the class Star projections should be created based on the relevant type parameter whenever it's possible --- .../kotlin/types/StarProjectionImpl.kt | 22 +++++++++++-------- .../deserialization/TypeDeserializer.kt | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/StarProjectionImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/types/StarProjectionImpl.kt index fa3a2957542..0353d4019c2 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/StarProjectionImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/StarProjectionImpl.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.types +import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns @@ -44,24 +45,27 @@ fun TypeParameterDescriptor.starProjectionType(): KotlinType { val classDescriptor = this.containingDeclaration as ClassifierDescriptorWithTypeParameters val typeParameters = classDescriptor.typeConstructor.parameters.map { it.typeConstructor } return TypeSubstitutor.create( - object : TypeConstructorSubstitution() { - override fun get(key: TypeConstructor) = - if (key in typeParameters) - TypeUtils.makeStarProjection(key.declarationDescriptor as TypeParameterDescriptor) - else null + object : TypeConstructorSubstitution() { + override fun get(key: TypeConstructor) = + if (key in typeParameters) + TypeUtils.makeStarProjection(key.declarationDescriptor as TypeParameterDescriptor) + else null - } + } ).substitute(this.upperBounds.first(), Variance.OUT_VARIANCE) ?: builtIns.defaultBound } -class TypeBasedStarProjectionImpl( - private val _type: KotlinType +// It should only be used in rare cases when type parameter for the relevant argument is not available +class StarProjectionForAbsentTypeParameter( + kotlinBuiltIns: KotlinBuiltIns ) : TypeProjectionBase() { + private val nullableAnyType: KotlinType = kotlinBuiltIns.nullableAnyType + override fun isStarProjection() = true override fun getProjectionKind() = Variance.OUT_VARIANCE - override fun getType() = _type + override fun getType() = nullableAnyType @TypeRefinement override fun refine(kotlinTypeRefiner: KotlinTypeRefiner): TypeProjection = this diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt index 9f83e051b07..08725f00569 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt @@ -241,7 +241,7 @@ class TypeDeserializer( private fun typeArgument(parameter: TypeParameterDescriptor?, typeArgumentProto: ProtoBuf.Type.Argument): TypeProjection { if (typeArgumentProto.projection == ProtoBuf.Type.Argument.Projection.STAR) { return if (parameter == null) - TypeBasedStarProjectionImpl(c.components.moduleDescriptor.builtIns.nullableAnyType) + StarProjectionForAbsentTypeParameter(c.components.moduleDescriptor.builtIns) else StarProjectionImpl(parameter) }