From 43d9c6cc906eda0fbd6209bb2f2ebbcbf9c76357 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Tue, 2 Mar 2021 02:05:43 +0300 Subject: [PATCH] [Commonizer] Reworked functions for making nullable CIR types --- .../commonizer/cir/factory/CirTypeFactory.kt | 53 ++++++++++++------- .../commonizer/core/TypeCommonizer.kt | 6 +-- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt index 32903d02543..94150895f41 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt @@ -67,7 +67,7 @@ object CirTypeFactory { val expandedType = extractExpandedType(abbreviatedType) val cirExpandedType = create(expandedType) as CirClassOrTypeAliasType - val cirExpandedTypeWithProperNullability = if (source.isMarkedNullable) makeNullable(cirExpandedType) else cirExpandedType + val cirExpandedTypeWithProperNullability = makeNullableIfNecessary(cirExpandedType, source.isMarkedNullable) createTypeAliasType( typeAliasId = classifierDescriptor.classifierId, @@ -128,25 +128,38 @@ object CirTypeFactory { ) } - fun makeNullable(classOrTypeAliasType: CirClassOrTypeAliasType): CirClassOrTypeAliasType = - if (classOrTypeAliasType.isMarkedNullable) - classOrTypeAliasType - else - when (classOrTypeAliasType) { - is CirClassType -> createClassType( - classId = classOrTypeAliasType.classifierId, - outerType = classOrTypeAliasType.outerType, - visibility = classOrTypeAliasType.visibility, - arguments = classOrTypeAliasType.arguments, - isMarkedNullable = true - ) - is CirTypeAliasType -> createTypeAliasType( - typeAliasId = classOrTypeAliasType.classifierId, - underlyingType = makeNullable(classOrTypeAliasType.underlyingType), - arguments = classOrTypeAliasType.arguments, - isMarkedNullable = true - ) - } + fun makeNullable(type: T): T { + if (type.isMarkedNullable) + return type + + val result = when (type) { + is CirClassType -> createClassType( + classId = type.classifierId, + outerType = type.outerType, + visibility = type.visibility, + arguments = type.arguments, + isMarkedNullable = true + ) + is CirTypeAliasType -> createTypeAliasType( + typeAliasId = type.classifierId, + underlyingType = makeNullable(type.underlyingType), + arguments = type.arguments, + isMarkedNullable = true + ) + is CirTypeParameterType -> createTypeParameterType( + index = type.index, + isMarkedNullable = true + ) + else -> error("Unsupported type: $type") + } + + @Suppress("UNCHECKED_CAST") + return result as T + } + + @Suppress("NOTHING_TO_INLINE") + inline fun makeNullableIfNecessary(type: T, necessary: Boolean): T = + if (!necessary) type else makeNullable(type) fun unabbreviate(type: CirClassOrTypeAliasType): CirClassType = when (type) { is CirClassType -> { diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt index efea745b149..090020aab45 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt @@ -143,11 +143,7 @@ private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifie // type alias don't needs to be commonized because it is from the standard library fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder { override fun build(typeAliasId: CirEntityId, arguments: List, isMarkedNullable: Boolean): CirTypeAliasType { - val underlyingTypeWithProperNullability = if (isMarkedNullable && !underlyingType.isMarkedNullable) - CirTypeFactory.makeNullable(underlyingType) - else - underlyingType - + val underlyingTypeWithProperNullability = CirTypeFactory.makeNullableIfNecessary(underlyingType, isMarkedNullable) return CirTypeFactory.createTypeAliasType( typeAliasId = typeAliasId, underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments???