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<String..String?>
This commit is contained in:
Jinseong Jeon
2021-06-07 23:16:47 -07:00
committed by TeamCityServer
parent 08e486d1a7
commit 4ec745abd7
5 changed files with 21 additions and 23 deletions
@@ -19,7 +19,9 @@ interface TypeMappingContext<Writer : JvmDescriptorTypeWriter<Type>> {
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 <Writer : JvmDescriptorTypeWriter<Type>> TypeSystemCommonBackendContextForTypeMapping.mapType(
context: TypeMappingContext<Writer>,
@@ -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?)
@@ -53,8 +53,8 @@ class FirJvmTypeMapper(val session: FirSession) : TypeMappingContext<JvmSignatur
override fun getScriptInternalName(typeConstructor: TypeConstructorMarker): String =
TODO("Not yet implemented")
override fun JvmSignatureWriter.writeGenericType(type: SimpleTypeMarker, asmType: Type, mode: TypeMappingMode) {
if (type !is ConeClassLikeType) return
override fun JvmSignatureWriter.writeGenericType(type: KotlinTypeMarker, asmType: Type, mode: TypeMappingMode) {
if (type !is ConeKotlinType) return
if (skipGenericSignature() || hasNothingInNonContravariantPosition(type) || type.typeArguments.isEmpty()) {
writeAsmType(asmType)
return
@@ -98,8 +98,10 @@ class FirJvmTypeMapper(val session: FirSession) : TypeMappingContext<JvmSignatur
else -> 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
@@ -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
@@ -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<String>
[!] 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<? extends kotlin.Unit>) : void {...}]
UParameter (name = selectItemFunction) [var selectItemFunction: kotlin.jvm.functions.Function0<? extends kotlin.Unit>]
UBlockExpression [{...}] : PsiType:NonExistentClass
@@ -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<String> = setOf(
// TODO: not able to map Stream<String?..String>
"plugins/uast-kotlin/testData/Lambdas.kt",
)
override fun isExpectedToFail(filePath: String): Boolean {
return filePath in whitelist
}
}
abstract class AbstractFirLegacyUastTypesTest : AbstractFirUastTypesTest(), FirLegacyUastTypesTestBase