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
This commit is contained in:
Denis Zharkov
2019-07-12 14:15:56 +03:00
committed by Dmitry Savvinov
parent 579838cb4e
commit ffaca279bc
2 changed files with 14 additions and 10 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.types package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
@@ -44,24 +45,27 @@ fun TypeParameterDescriptor.starProjectionType(): KotlinType {
val classDescriptor = this.containingDeclaration as ClassifierDescriptorWithTypeParameters val classDescriptor = this.containingDeclaration as ClassifierDescriptorWithTypeParameters
val typeParameters = classDescriptor.typeConstructor.parameters.map { it.typeConstructor } val typeParameters = classDescriptor.typeConstructor.parameters.map { it.typeConstructor }
return TypeSubstitutor.create( return TypeSubstitutor.create(
object : TypeConstructorSubstitution() { object : TypeConstructorSubstitution() {
override fun get(key: TypeConstructor) = override fun get(key: TypeConstructor) =
if (key in typeParameters) if (key in typeParameters)
TypeUtils.makeStarProjection(key.declarationDescriptor as TypeParameterDescriptor) TypeUtils.makeStarProjection(key.declarationDescriptor as TypeParameterDescriptor)
else null else null
} }
).substitute(this.upperBounds.first(), Variance.OUT_VARIANCE) ?: builtIns.defaultBound ).substitute(this.upperBounds.first(), Variance.OUT_VARIANCE) ?: builtIns.defaultBound
} }
class TypeBasedStarProjectionImpl( // It should only be used in rare cases when type parameter for the relevant argument is not available
private val _type: KotlinType class StarProjectionForAbsentTypeParameter(
kotlinBuiltIns: KotlinBuiltIns
) : TypeProjectionBase() { ) : TypeProjectionBase() {
private val nullableAnyType: KotlinType = kotlinBuiltIns.nullableAnyType
override fun isStarProjection() = true override fun isStarProjection() = true
override fun getProjectionKind() = Variance.OUT_VARIANCE override fun getProjectionKind() = Variance.OUT_VARIANCE
override fun getType() = _type override fun getType() = nullableAnyType
@TypeRefinement @TypeRefinement
override fun refine(kotlinTypeRefiner: KotlinTypeRefiner): TypeProjection = this override fun refine(kotlinTypeRefiner: KotlinTypeRefiner): TypeProjection = this
@@ -241,7 +241,7 @@ class TypeDeserializer(
private fun typeArgument(parameter: TypeParameterDescriptor?, typeArgumentProto: ProtoBuf.Type.Argument): TypeProjection { private fun typeArgument(parameter: TypeParameterDescriptor?, typeArgumentProto: ProtoBuf.Type.Argument): TypeProjection {
if (typeArgumentProto.projection == ProtoBuf.Type.Argument.Projection.STAR) { if (typeArgumentProto.projection == ProtoBuf.Type.Argument.Projection.STAR) {
return if (parameter == null) return if (parameter == null)
TypeBasedStarProjectionImpl(c.components.moduleDescriptor.builtIns.nullableAnyType) StarProjectionForAbsentTypeParameter(c.components.moduleDescriptor.builtIns)
else else
StarProjectionImpl(parameter) StarProjectionImpl(parameter)
} }