[Commonization] Improvements in approx. keys

- Don't print upper bounds if it contains only kotlin/Any?
- Print variance in lowercase
This commit is contained in:
Dmitriy Dolovov
2021-02-12 22:48:24 +03:00
parent 3c6eb8f8f4
commit 0eaea655d0
2 changed files with 24 additions and 28 deletions
@@ -12,22 +12,17 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeParameter import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirTypeParameterImpl import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirTypeParameterImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.filteredUpperBounds
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.typeUtil.isNullableAny
object CirTypeParameterFactory { object CirTypeParameterFactory {
fun create(source: TypeParameterDescriptor): CirTypeParameter { fun create(source: TypeParameterDescriptor): CirTypeParameter = create(
val upperBounds = source.upperBounds annotations = source.annotations.compactMap(CirAnnotationFactory::create),
val filteredUpperBounds = if (upperBounds.singleOrNull()?.isNullableAny() == true) emptyList() else upperBounds name = CirName.create(source.name),
isReified = source.isReified,
return create( variance = source.variance,
annotations = source.annotations.compactMap(CirAnnotationFactory::create), upperBounds = source.filteredUpperBounds.compactMap(CirTypeFactory::create)
name = CirName.create(source.name), )
isReified = source.isReified,
variance = source.variance,
upperBounds = filteredUpperBounds.compactMap(CirTypeFactory::create)
)
}
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun create( inline fun create(
@@ -5,16 +5,14 @@
package org.jetbrains.kotlin.descriptors.commonizer.utils package org.jetbrains.kotlin.descriptors.commonizer.utils
import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature
import org.jetbrains.kotlin.resolve.descriptorUtil.classId import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.isNullableAny
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
internal inline val KotlinType.declarationDescriptor: ClassifierDescriptor internal inline val KotlinType.declarationDescriptor: ClassifierDescriptor
@@ -42,6 +40,9 @@ internal val ClassifierDescriptorWithTypeParameters.classifierId: CirEntityId
else -> error("Unexpected containing declaration type for $this: ${owner::class}, $owner") else -> error("Unexpected containing declaration type for $this: ${owner::class}, $owner")
} }
internal inline val TypeParameterDescriptor.filteredUpperBounds: List<KotlinType>
get() = upperBounds.takeUnless { it.singleOrNull()?.isNullableAny() == true } ?: emptyList()
internal val KotlinType.signature: CirTypeSignature internal val KotlinType.signature: CirTypeSignature
get() { get() {
// use of interner saves up to 95% of duplicates // use of interner saves up to 95% of duplicates
@@ -55,17 +56,17 @@ private fun StringBuilder.buildTypeSignature(type: KotlinType, exploredTypeParam
append(typeParameterDescriptor.name.asString()) append(typeParameterDescriptor.name.asString())
if (exploredTypeParameters.add(type.makeNotNullable())) { // print upper bounds once the first time when type parameter type is met if (exploredTypeParameters.add(type.makeNotNullable())) { // print upper bounds once the first time when type parameter type is met
append(":[") append(':').append('[')
typeParameterDescriptor.upperBounds.forEachIndexed { index, upperBound -> typeParameterDescriptor.filteredUpperBounds.forEachIndexed { index, upperBound ->
if (index > 0) if (index > 0)
append(",") append(',')
buildTypeSignature(upperBound, exploredTypeParameters) buildTypeSignature(upperBound, exploredTypeParameters)
} }
append("]") append(']')
} }
if (type.isMarkedNullable) if (type.isMarkedNullable)
append("?") append('?')
} else { } else {
// N.B. this is classifier type // N.B. this is classifier type
val abbreviation = (type as? AbbreviatedType)?.abbreviation ?: type val abbreviation = (type as? AbbreviatedType)?.abbreviation ?: type
@@ -73,25 +74,25 @@ private fun StringBuilder.buildTypeSignature(type: KotlinType, exploredTypeParam
val arguments = abbreviation.arguments val arguments = abbreviation.arguments
if (arguments.isNotEmpty()) { if (arguments.isNotEmpty()) {
append("<") append('<')
arguments.forEachIndexed { index, argument -> arguments.forEachIndexed { index, argument ->
if (index > 0) if (index > 0)
append(",") append(',')
if (argument.isStarProjection) if (argument.isStarProjection)
append("*") append('*')
else { else {
val variance = argument.projectionKind val variance = argument.projectionKind
if (variance != Variance.INVARIANT) if (variance != Variance.INVARIANT)
append(variance).append(" ") append(variance.label).append(' ')
buildTypeSignature(argument.type, exploredTypeParameters) buildTypeSignature(argument.type, exploredTypeParameters)
} }
} }
append(">") append('>')
} }
if (abbreviation.isMarkedNullable) if (abbreviation.isMarkedNullable)
append("?") append('?')
} }
} }