From 4ec745abd7402867bfdc4a8c02358d05905ea912 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Mon, 7 Jun 2021 23:16:47 -0700 Subject: [PATCH] FIR: relax simple type requirement in AbstractTypeMapper The counterparts in old FE don't have such restriction, so it can map a type that has a flexible type as a type argument to JVM type, e.g., Stream --- .../jetbrains/kotlin/types/AbstractTypeMapper.kt | 14 +++++++------- .../kotlin/fir/backend/jvm/FirJvmTypeMapper.kt | 10 ++++++---- .../kotlin/backend/jvm/codegen/IrTypeMapper.kt | 4 ++-- .../testData/legacyTypes/Lambdas.types.fir.txt | 6 +++++- .../test/kotlin/AbstractFirLegacyUastTypesTest.kt | 10 +--------- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/compiler/backend.common.jvm/src/org/jetbrains/kotlin/types/AbstractTypeMapper.kt b/compiler/backend.common.jvm/src/org/jetbrains/kotlin/types/AbstractTypeMapper.kt index e2e658b23f0..b301c7d6f03 100644 --- a/compiler/backend.common.jvm/src/org/jetbrains/kotlin/types/AbstractTypeMapper.kt +++ b/compiler/backend.common.jvm/src/org/jetbrains/kotlin/types/AbstractTypeMapper.kt @@ -19,7 +19,9 @@ interface TypeMappingContext> { fun getClassInternalName(typeConstructor: TypeConstructorMarker): String fun getScriptInternalName(typeConstructor: TypeConstructorMarker): String - fun Writer.writeGenericType(type: SimpleTypeMarker, asmType: Type, mode: TypeMappingMode) + + // NB: The counterpart, [KotlinTypeMapper#writeGenericType], doesn't have restriction on [type] + fun Writer.writeGenericType(type: KotlinTypeMarker, asmType: Type, mode: TypeMappingMode) } object AbstractTypeMapper { @@ -44,6 +46,7 @@ object AbstractTypeMapper { sw: Writer? = null ): Type = context.typeContext.mapType(context, type, mode, sw) + // NB: The counterpart, [descriptorBasedTypeSignatureMapping#mapType] doesn't have restriction on [type]. @OptIn(ExperimentalStdlibApi::class) private fun > TypeSystemCommonBackendContextForTypeMapping.mapType( context: TypeMappingContext, @@ -51,10 +54,7 @@ object AbstractTypeMapper { mode: TypeMappingMode = TypeMappingMode.DEFAULT, sw: Writer? = null ): Type { - if (type !is SimpleTypeMarker) { - error("Unexpected type: $type (original Kotlin type=$type of ${type.let { it::class }})") - } - if (type.isSuspendFunction()) { + if (type is SimpleTypeMarker && type.isSuspendFunction()) { val argumentsCount = type.argumentsCount() val argumentsList = type.asArgumentList() @@ -79,7 +79,7 @@ object AbstractTypeMapper { val typeConstructor = type.typeConstructor() when { - type.isArrayOrNullableArray() -> { + type is SimpleTypeMarker && type.isArrayOrNullableArray() -> { val typeArgument = type.asArgumentList()[0] val (variance, memberType) = when { typeArgument.isStarProjection() -> Variance.OUT_VARIANCE to nullableAnyType() @@ -99,7 +99,7 @@ object AbstractTypeMapper { return AsmUtil.getArrayType(arrayElementType) } - typeConstructor.isClassTypeConstructor() -> { + type is SimpleTypeMarker && typeConstructor.isClassTypeConstructor() -> { if (typeConstructor.isInlineClass() && !mode.needInlineClassWrapping) { val expandedType = computeExpandedTypeForInlineClass(type) require(expandedType is SimpleTypeMarker?) diff --git a/compiler/fir/fir2ir/jvm-backend/src/org/jetbrains/kotlin/fir/backend/jvm/FirJvmTypeMapper.kt b/compiler/fir/fir2ir/jvm-backend/src/org/jetbrains/kotlin/fir/backend/jvm/FirJvmTypeMapper.kt index 7aa400a6b94..2056119ad25 100644 --- a/compiler/fir/fir2ir/jvm-backend/src/org/jetbrains/kotlin/fir/backend/jvm/FirJvmTypeMapper.kt +++ b/compiler/fir/fir2ir/jvm-backend/src/org/jetbrains/kotlin/fir/backend/jvm/FirJvmTypeMapper.kt @@ -53,8 +53,8 @@ class FirJvmTypeMapper(val session: FirSession) : TypeMappingContext null } - private fun ConeClassLikeType.buildPossiblyInnerType(): PossiblyInnerConeType? = - buildPossiblyInnerType(lookupTag.toSymbol(session)?.toRegularClassSymbol(), 0) + private fun ConeKotlinType.buildPossiblyInnerType(): PossiblyInnerConeType? { + if (this !is ConeClassLikeType) return null + return buildPossiblyInnerType(lookupTag.toSymbol(session)?.toRegularClassSymbol(), 0) + } private fun ConeClassLikeType.parentClassOrNull(): FirRegularClassSymbol? { val parentClassId = classId?.outerClassId ?: return null diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrTypeMapper.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrTypeMapper.kt index 2f3b0a9622f..0bf7f1d677b 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrTypeMapper.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrTypeMapper.kt @@ -133,8 +133,8 @@ class IrTypeMapper(private val context: JvmBackendContext) : KotlinTypeMapperBas sw: JvmSignatureWriter? = null ): Type = AbstractTypeMapper.mapType(this, type, mode, sw) - override fun JvmSignatureWriter.writeGenericType(type: SimpleTypeMarker, asmType: Type, mode: TypeMappingMode) { - require(type is IrSimpleType) + override fun JvmSignatureWriter.writeGenericType(type: KotlinTypeMarker, asmType: Type, mode: TypeMappingMode) { + if (type !is IrSimpleType) return if (skipGenericSignature() || hasNothingInNonContravariantPosition(type) || type.arguments.isEmpty() || type.isRawTypeImpl()) { writeAsmType(asmType) return diff --git a/plugins/uast-kotlin-fir/testData/legacyTypes/Lambdas.types.fir.txt b/plugins/uast-kotlin-fir/testData/legacyTypes/Lambdas.types.fir.txt index 97cb5248dcd..102de597f09 100644 --- a/plugins/uast-kotlin-fir/testData/legacyTypes/Lambdas.types.fir.txt +++ b/plugins/uast-kotlin-fir/testData/legacyTypes/Lambdas.types.fir.txt @@ -3,7 +3,11 @@ UFile (package = ) [import java.util.stream.Stream...] UClass (name = LambdasKt) [public final class LambdasKt {...}] UMethod (name = foo) [public static final fun foo() : void {...}] UBlockExpression [{...}] : PsiType:NonExistentClass - [!] UnknownKotlinExpression (DOT_QUALIFIED_EXPRESSION) [[!] UnknownKotlinExpression (DOT_QUALIFIED_EXPRESSION)] + UQualifiedReferenceExpression [[!] UnknownKotlinExpression (REFERENCE_EXPRESSION).[!] UnknownKotlinExpression (CALL_EXPRESSION).[!] UnknownKotlinExpression (CALL_EXPRESSION)] : PsiType:NonExistentClass + UQualifiedReferenceExpression [[!] UnknownKotlinExpression (REFERENCE_EXPRESSION).[!] UnknownKotlinExpression (CALL_EXPRESSION)] : PsiType:Stream + [!] UnknownKotlinExpression (REFERENCE_EXPRESSION) [[!] UnknownKotlinExpression (REFERENCE_EXPRESSION)] + [!] UnknownKotlinExpression (CALL_EXPRESSION) [[!] UnknownKotlinExpression (CALL_EXPRESSION)] + [!] UnknownKotlinExpression (CALL_EXPRESSION) [[!] UnknownKotlinExpression (CALL_EXPRESSION)] UMethod (name = doSelectItem) [public static final fun doSelectItem(selectItemFunction: kotlin.jvm.functions.Function0) : void {...}] UParameter (name = selectItemFunction) [var selectItemFunction: kotlin.jvm.functions.Function0] UBlockExpression [{...}] : PsiType:NonExistentClass diff --git a/plugins/uast-kotlin-fir/tests/org/jetbrains/uast/test/kotlin/AbstractFirLegacyUastTypesTest.kt b/plugins/uast-kotlin-fir/tests/org/jetbrains/uast/test/kotlin/AbstractFirLegacyUastTypesTest.kt index 351f07ac472..62ec62855f4 100644 --- a/plugins/uast-kotlin-fir/tests/org/jetbrains/uast/test/kotlin/AbstractFirLegacyUastTypesTest.kt +++ b/plugins/uast-kotlin-fir/tests/org/jetbrains/uast/test/kotlin/AbstractFirLegacyUastTypesTest.kt @@ -7,12 +7,4 @@ package org.jetbrains.uast.test.kotlin import org.jetbrains.uast.test.common.kotlin.FirLegacyUastTypesTestBase -abstract class AbstractFirLegacyUastTypesTest : AbstractFirUastTypesTest(), FirLegacyUastTypesTestBase { - private val whitelist : Set = setOf( - // TODO: not able to map Stream - "plugins/uast-kotlin/testData/Lambdas.kt", - ) - override fun isExpectedToFail(filePath: String): Boolean { - return filePath in whitelist - } -} +abstract class AbstractFirLegacyUastTypesTest : AbstractFirUastTypesTest(), FirLegacyUastTypesTestBase