From 0eaea655d0d5801023657b8206624b85005e8f5f Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Fri, 12 Feb 2021 22:48:24 +0300 Subject: [PATCH] [Commonization] Improvements in approx. keys - Don't print upper bounds if it contains only kotlin/Any? - Print variance in lowercase --- .../cir/factory/CirTypeParameterFactory.kt | 21 +++++-------- .../descriptors/commonizer/utils/type.kt | 31 ++++++++++--------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeParameterFactory.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeParameterFactory.kt index aede18423eb..824627b9fcb 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeParameterFactory.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeParameterFactory.kt @@ -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.impl.CirTypeParameterImpl 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.typeUtil.isNullableAny object CirTypeParameterFactory { - fun create(source: TypeParameterDescriptor): CirTypeParameter { - val upperBounds = source.upperBounds - val filteredUpperBounds = if (upperBounds.singleOrNull()?.isNullableAny() == true) emptyList() else upperBounds - - return create( - annotations = source.annotations.compactMap(CirAnnotationFactory::create), - name = CirName.create(source.name), - isReified = source.isReified, - variance = source.variance, - upperBounds = filteredUpperBounds.compactMap(CirTypeFactory::create) - ) - } + fun create(source: TypeParameterDescriptor): CirTypeParameter = create( + annotations = source.annotations.compactMap(CirAnnotationFactory::create), + name = CirName.create(source.name), + isReified = source.isReified, + variance = source.variance, + upperBounds = source.filteredUpperBounds.compactMap(CirTypeFactory::create) + ) @Suppress("NOTHING_TO_INLINE") inline fun create( diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/utils/type.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/utils/type.kt index d56c1f07fb0..10cc5211801 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/utils/type.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/utils/type.kt @@ -5,16 +5,14 @@ package org.jetbrains.kotlin.descriptors.commonizer.utils -import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.descriptors.ClassifierDescriptor -import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters -import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature import org.jetbrains.kotlin.resolve.descriptorUtil.classId import org.jetbrains.kotlin.types.* +import org.jetbrains.kotlin.types.typeUtil.isNullableAny import org.jetbrains.kotlin.types.typeUtil.makeNotNullable 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") } +internal inline val TypeParameterDescriptor.filteredUpperBounds: List + get() = upperBounds.takeUnless { it.singleOrNull()?.isNullableAny() == true } ?: emptyList() + internal val KotlinType.signature: CirTypeSignature get() { // use of interner saves up to 95% of duplicates @@ -55,17 +56,17 @@ private fun StringBuilder.buildTypeSignature(type: KotlinType, exploredTypeParam append(typeParameterDescriptor.name.asString()) if (exploredTypeParameters.add(type.makeNotNullable())) { // print upper bounds once the first time when type parameter type is met - append(":[") - typeParameterDescriptor.upperBounds.forEachIndexed { index, upperBound -> + append(':').append('[') + typeParameterDescriptor.filteredUpperBounds.forEachIndexed { index, upperBound -> if (index > 0) - append(",") + append(',') buildTypeSignature(upperBound, exploredTypeParameters) } - append("]") + append(']') } if (type.isMarkedNullable) - append("?") + append('?') } else { // N.B. this is classifier type val abbreviation = (type as? AbbreviatedType)?.abbreviation ?: type @@ -73,25 +74,25 @@ private fun StringBuilder.buildTypeSignature(type: KotlinType, exploredTypeParam val arguments = abbreviation.arguments if (arguments.isNotEmpty()) { - append("<") + append('<') arguments.forEachIndexed { index, argument -> if (index > 0) - append(",") + append(',') if (argument.isStarProjection) - append("*") + append('*') else { val variance = argument.projectionKind if (variance != Variance.INVARIANT) - append(variance).append(" ") + append(variance.label).append(' ') buildTypeSignature(argument.type, exploredTypeParameters) } } - append(">") + append('>') } if (abbreviation.isMarkedNullable) - append("?") + append('?') } }