Minor: reformat CapturedTypeApproximation.kt according to code style
This commit is contained in:
@@ -29,14 +29,14 @@ import org.jetbrains.kotlin.types.typeUtil.builtIns
|
||||
import java.util.*
|
||||
|
||||
data class ApproximationBounds<out T>(
|
||||
val lower: T,
|
||||
val upper: T
|
||||
val lower: T,
|
||||
val upper: T
|
||||
)
|
||||
|
||||
private class TypeArgument(
|
||||
val typeParameter: TypeParameterDescriptor,
|
||||
val inProjection: KotlinType,
|
||||
val outProjection: KotlinType
|
||||
val typeParameter: TypeParameterDescriptor,
|
||||
val inProjection: KotlinType,
|
||||
val outProjection: KotlinType
|
||||
) {
|
||||
val isConsistent: Boolean
|
||||
get() = KotlinTypeChecker.DEFAULT.isSubtypeOf(inProjection, outProjection)
|
||||
@@ -48,8 +48,10 @@ private fun TypeArgument.toTypeProjection(): TypeProjection {
|
||||
classifierNamePolicy = ClassifierNamePolicy.FULLY_QUALIFIED
|
||||
}
|
||||
"Only consistent enhanced type projection can be converted to type projection, but " +
|
||||
"[${descriptorRenderer.render(typeParameter)}: <${descriptorRenderer.renderType(inProjection)}, ${descriptorRenderer.renderType(outProjection)}>]" +
|
||||
" was found"
|
||||
"[${descriptorRenderer.render(typeParameter)}: <${descriptorRenderer.renderType(inProjection)}, ${descriptorRenderer.renderType(
|
||||
outProjection
|
||||
)}>]" +
|
||||
" was found"
|
||||
}
|
||||
fun removeProjectionIfRedundant(variance: Variance) = if (variance == typeParameter.variance) Variance.INVARIANT else variance
|
||||
return when {
|
||||
@@ -62,11 +64,11 @@ private fun TypeArgument.toTypeProjection(): TypeProjection {
|
||||
}
|
||||
|
||||
private fun TypeProjection.toTypeArgument(typeParameter: TypeParameterDescriptor) =
|
||||
when (TypeSubstitutor.combine(typeParameter.variance, this)) {
|
||||
Variance.INVARIANT -> TypeArgument(typeParameter, type, type)
|
||||
Variance.IN_VARIANCE -> TypeArgument(typeParameter, type, typeParameter.builtIns.nullableAnyType)
|
||||
Variance.OUT_VARIANCE -> TypeArgument(typeParameter, typeParameter.builtIns.nothingType, type)
|
||||
}
|
||||
when (TypeSubstitutor.combine(typeParameter.variance, this)) {
|
||||
Variance.INVARIANT -> TypeArgument(typeParameter, type, type)
|
||||
Variance.IN_VARIANCE -> TypeArgument(typeParameter, type, typeParameter.builtIns.nullableAnyType)
|
||||
Variance.OUT_VARIANCE -> TypeArgument(typeParameter, typeParameter.builtIns.nothingType, type)
|
||||
}
|
||||
|
||||
fun approximateCapturedTypesIfNecessary(typeProjection: TypeProjection?, approximateContravariant: Boolean): TypeProjection? {
|
||||
if (typeProjection == null) return null
|
||||
@@ -112,14 +114,15 @@ fun approximateCapturedTypes(type: KotlinType): ApproximationBounds<KotlinType>
|
||||
val boundsForFlexibleUpper = approximateCapturedTypes(type.upperIfFlexible())
|
||||
|
||||
return ApproximationBounds(
|
||||
KotlinTypeFactory.flexibleType(
|
||||
boundsForFlexibleLower.lower.lowerIfFlexible(),
|
||||
boundsForFlexibleUpper.lower.upperIfFlexible()
|
||||
).inheritEnhancement(type),
|
||||
KotlinTypeFactory.flexibleType(
|
||||
boundsForFlexibleLower.upper.lowerIfFlexible(),
|
||||
boundsForFlexibleUpper.upper.upperIfFlexible()
|
||||
).inheritEnhancement(type))
|
||||
KotlinTypeFactory.flexibleType(
|
||||
boundsForFlexibleLower.lower.lowerIfFlexible(),
|
||||
boundsForFlexibleUpper.lower.upperIfFlexible()
|
||||
).inheritEnhancement(type),
|
||||
KotlinTypeFactory.flexibleType(
|
||||
boundsForFlexibleLower.upper.lowerIfFlexible(),
|
||||
boundsForFlexibleUpper.upper.upperIfFlexible()
|
||||
).inheritEnhancement(type)
|
||||
)
|
||||
}
|
||||
|
||||
val typeConstructor = type.constructor
|
||||
@@ -146,8 +149,7 @@ fun approximateCapturedTypes(type: KotlinType): ApproximationBounds<KotlinType>
|
||||
if (typeProjection.isStarProjection) {
|
||||
lowerBoundArguments.add(typeArgument)
|
||||
upperBoundArguments.add(typeArgument)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
val (lower, upper) = approximateProjection(typeArgument)
|
||||
lowerBoundArguments.add(lower)
|
||||
upperBoundArguments.add(upper)
|
||||
@@ -155,8 +157,9 @@ fun approximateCapturedTypes(type: KotlinType): ApproximationBounds<KotlinType>
|
||||
}
|
||||
val lowerBoundIsTrivial = lowerBoundArguments.any { !it.isConsistent }
|
||||
return ApproximationBounds(
|
||||
if (lowerBoundIsTrivial) type.builtIns.nothingType else type.replaceTypeArguments(lowerBoundArguments),
|
||||
type.replaceTypeArguments(upperBoundArguments))
|
||||
if (lowerBoundIsTrivial) type.builtIns.nothingType else type.replaceTypeArguments(lowerBoundArguments),
|
||||
type.replaceTypeArguments(upperBoundArguments)
|
||||
)
|
||||
}
|
||||
|
||||
private fun KotlinType.replaceTypeArguments(newTypeArguments: List<TypeArgument>): KotlinType {
|
||||
@@ -168,6 +171,7 @@ private fun approximateProjection(typeArgument: TypeArgument): ApproximationBoun
|
||||
val (inLower, inUpper) = approximateCapturedTypes(typeArgument.inProjection)
|
||||
val (outLower, outUpper) = approximateCapturedTypes(typeArgument.outProjection)
|
||||
return ApproximationBounds(
|
||||
lower = TypeArgument(typeArgument.typeParameter, inUpper, outLower),
|
||||
upper = TypeArgument(typeArgument.typeParameter, inLower, outUpper))
|
||||
lower = TypeArgument(typeArgument.typeParameter, inUpper, outLower),
|
||||
upper = TypeArgument(typeArgument.typeParameter, inLower, outUpper)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user