FIR IDE: simplify KtType hierarchy

- get rid of Kt(Non)DenotatbleType super classes as unused
- make KtErrorType to inherit KtClassType to be consistent with FIR
This commit is contained in:
Ilya Kirillov
2021-06-11 11:39:06 +02:00
committed by TeamCityServer
parent f6078b24df
commit 0a6c96492a
14 changed files with 51 additions and 54 deletions
@@ -110,20 +110,30 @@ fun KtTypeArgument.toTypeProjection(context: FE10BindingContext): TypeProjection
}
fun KtType.toKotlinType(context: FE10BindingContext, annotations: Annotations = Annotations.EMPTY): UnwrappedType {
if (this is KtNonDenotableType) return toKotlinType(context, annotations)
val typeConstructor = when (this) {
val typeConstructor: TypeConstructor = when (this) {
is KtTypeParameterType -> KtSymbolBasedTypeParameterDescriptor(this.symbol, context).typeConstructor
is KtClassType -> when (val classLikeSymbol = classSymbol) {
is KtNonErrorClassType -> when (val classLikeSymbol = classSymbol) {
is KtTypeAliasSymbol -> context.typeAliasImplementationPlanned()
is KtNamedClassOrObjectSymbol -> KtSymbolBasedClassDescriptor(classLikeSymbol, context).typeConstructor
is KtAnonymousObjectSymbol -> context.implementationPostponed()
}
is KtErrorType -> ErrorUtils.createErrorTypeConstructorWithCustomDebugName(error)
is KtClassErrorType -> ErrorUtils.createErrorTypeConstructorWithCustomDebugName(error)
is KtFlexibleType -> {
return KotlinTypeFactory.flexibleType(
lowerBound.toKotlinType(context, annotations) as SimpleType,
upperBound.toKotlinType(context, annotations) as SimpleType
)
}
is KtIntersectionType -> {
// most likely it isn't correct and intersectTypes(List<UnwrappedType>) should be used,
// but I don't think that we will have the real problem with that implementation
return IntersectionTypeConstructor(conjuncts.map { it.toKotlinType(context) }).createType()
}
else -> error("Unexpected subclass: ${this.javaClass}")
}
val ktTypeArguments = this.safeAs<KtClassType>()?.typeArguments ?: emptyList()
val ktTypeArguments = this.safeAs<KtNonErrorClassType>()?.typeArguments ?: emptyList()
val markedAsNullable = this.safeAs<KtTypeWithNullability>()?.nullability == KtTypeNullability.NULLABLE
@@ -131,15 +141,4 @@ fun KtType.toKotlinType(context: FE10BindingContext, annotations: Annotations =
annotations, typeConstructor, ktTypeArguments.map { it.toTypeProjection(context) }, markedAsNullable,
MemberScopeForKtSymbolBasedDescriptors { this.asStringForDebugging() }
)
}
fun KtNonDenotableType.toKotlinType(context: FE10BindingContext, annotations: Annotations = Annotations.EMPTY): UnwrappedType =
when (this) {
is KtFlexibleType -> KotlinTypeFactory.flexibleType(
lowerBound.toKotlinType(context, annotations) as SimpleType,
upperBound.toKotlinType(context, annotations) as SimpleType
)
// most likely it isn't correct and intersectTypes(List<UnwrappedType>) should be used,
// but I don't think that we will have the real problem with that implementation
is KtIntersectionType -> IntersectionTypeConstructor(conjuncts.map { it.toKotlinType(context) }).createType()
}
}
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.idea.frontend.api.scopes.KtCompositeScope
import org.jetbrains.kotlin.idea.frontend.api.scopes.KtScope
import org.jetbrains.kotlin.idea.frontend.api.symbols.*
import org.jetbrains.kotlin.idea.frontend.api.types.KtClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtNonErrorClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
import org.jetbrains.kotlin.psi.KtExpression
@@ -169,7 +170,7 @@ internal class FirCallableCompletionContributor(
private class TypeNamesProvider(private val indexHelper: IndexHelper) {
fun KtAnalysisSession.findAllNames(type: KtType): Set<String> {
if (type !is KtClassType) return emptySet()
if (type !is KtNonErrorClassType) return emptySet()
val typeName = type.classId.shortClassName.let {
if (it.isSpecial) return emptySet()
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.idea.frontend.api.symbols.*
import org.jetbrains.kotlin.idea.frontend.api.types.KtClassType
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.idea.completion.contributors.helpers.FirClassifierProvider.getAvailableClassifiersCurrentScope
import org.jetbrains.kotlin.idea.frontend.api.types.KtNonErrorClassType
internal open class FirClassifierCompletionContributor(
basicContext: FirBasicCompletionContext,
@@ -82,7 +83,7 @@ internal class FirAnnotationCompletionContributor(
}
}
is KtTypeAliasSymbol -> {
val expendedClass = (classifierSymbol.expandedType as? KtClassType)?.classSymbol
val expendedClass = (classifierSymbol.expandedType as? KtNonErrorClassType)?.classSymbol
expendedClass?.let { filterClassifiers(it) } == true
}
}
@@ -72,7 +72,7 @@ internal class FirWhenWithSubjectConditionContributor(
}
private fun KtAnalysisSession.getClassSymbol(subjectType: KtType): KtNamedClassOrObjectSymbol? {
val classType = subjectType as? KtClassType
val classType = subjectType as? KtNonErrorClassType
return classType?.classSymbol as? KtNamedClassOrObjectSymbol
}
@@ -107,7 +107,7 @@ internal class FirWhenWithSubjectConditionContributor(
return when (classifier) {
is KtAnonymousObjectSymbol -> return false
is KtNamedClassOrObjectSymbol -> !classifier.classKind.isObject
is KtTypeAliasSymbol -> (classifier.expandedType as? KtClassType)?.classSymbol?.let { isPrefixNeeded(it) } == true
is KtTypeAliasSymbol -> (classifier.expandedType as? KtNonErrorClassType)?.classSymbol?.let { isPrefixNeeded(it) } == true
is KtTypeParameterSymbol -> true
}
}
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.idea.completion.lookups.shortenReferencesForFirCompl
import org.jetbrains.kotlin.idea.frontend.api.KtAnalysisSession
import org.jetbrains.kotlin.idea.frontend.api.symbols.KtNamedClassOrObjectSymbol
import org.jetbrains.kotlin.idea.frontend.api.types.KtClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtNonErrorClassType
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
@@ -23,7 +24,7 @@ internal object FirSuperEntriesProvider {
val containingClass = context.getStrictParentOfType<KtClassOrObject>() ?: return emptyList()
val containingClassSymbol = containingClass.getClassOrObjectSymbol()
return containingClassSymbol.superTypes.mapNotNull { superType ->
val classType = superType.type as? KtClassType ?: return@mapNotNull null
val classType = superType.type as? KtNonErrorClassType ?: return@mapNotNull null
classType.classSymbol as? KtNamedClassOrObjectSymbol
}
}
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.idea.frontend.api.symbols.KtPropertySymbol
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.KtSymbolWithMembers
import org.jetbrains.kotlin.idea.frontend.api.symbols.psiSafe
import org.jetbrains.kotlin.idea.frontend.api.types.KtClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtNonErrorClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
import org.jetbrains.kotlin.idea.quickfix.ChangeTypeFixUtils
import org.jetbrains.kotlin.name.Name
@@ -134,7 +135,7 @@ object ChangeTypeQuickFixFactories {
?: return@diagnosticFixFactory emptyList()
buildList<HLApplicatorTargetWithInput<KtCallableDeclaration, Input>> {
add(entryWithWrongType withInput Input(TargetType.VARIABLE, createTypeInfo(diagnostic.destructingType)))
val classSymbol = (diagnostic.psi.getKtType() as? KtClassType)?.classSymbol as? KtSymbolWithMembers ?: return@buildList
val classSymbol = (diagnostic.psi.getKtType() as? KtNonErrorClassType)?.classSymbol as? KtSymbolWithMembers ?: return@buildList
val componentFunction = classSymbol.getMemberScope()
.getCallableSymbols { it == diagnostic.componentFunctionName }
.firstOrNull()?.psi as? KtCallableDeclaration
@@ -27,7 +27,7 @@ interface KtExpressionTypeProviderMixIn : KtAnalysisSessionMixIn {
/**
* Returns the expected [KtType] of this [PsiElement] if it is an expression. The returned value should not be a
* [org.jetbrains.kotlin.idea.frontend.api.types.KtErrorType].
* [org.jetbrains.kotlin.idea.frontend.api.types.KtClassErrorType].
*/
fun PsiElement.getExpectedType(): KtType? =
analysisSession.expressionTypeProvider.getExpectedType(this)
@@ -6,10 +6,7 @@
package org.jetbrains.kotlin.idea.frontend.api.components
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.idea.frontend.api.types.KtClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
import org.jetbrains.kotlin.idea.frontend.api.types.KtTypeNullability
import org.jetbrains.kotlin.idea.frontend.api.types.KtTypeWithNullability
import org.jetbrains.kotlin.idea.frontend.api.types.*
import org.jetbrains.kotlin.name.ClassId
abstract class KtTypeInfoProvider : KtAnalysisSessionComponent() {
@@ -47,13 +44,13 @@ interface KtTypeInfoProviderMixIn : KtAnalysisSessionMixIn {
val KtType.isUByte: Boolean get() = isClassTypeWithClassId(StandardNames.FqNames.uByte)
fun KtType.isClassTypeWithClassId(classId: ClassId): Boolean {
if (this !is KtClassType) return false
if (this !is KtNonErrorClassType) return false
return this.classId == classId
}
val KtType.isPrimitive: Boolean
get() {
if (this !is KtClassType) return false
if (this !is KtNonErrorClassType) return false
return this.classId in DefaultTypeClassIds.PRIMITIVES
}
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.idea.frontend.api.types
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.idea.frontend.api.KtTypeArgument
import org.jetbrains.kotlin.idea.frontend.api.ValidityTokenOwner
import org.jetbrains.kotlin.idea.frontend.api.symbols.KtClassLikeSymbol
@@ -13,7 +12,7 @@ import org.jetbrains.kotlin.idea.frontend.api.symbols.KtTypeParameterSymbol
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
interface KtType : ValidityTokenOwner {
sealed interface KtType : ValidityTokenOwner {
fun asStringForDebugging(): String
}
@@ -21,25 +20,23 @@ interface KtTypeWithNullability : KtType {
val nullability: KtTypeNullability
}
enum class KtTypeNullability {
NULLABLE, NON_NULLABLE;
enum class KtTypeNullability(val isNullable: Boolean) {
NULLABLE(true), NON_NULLABLE(false);
companion object {
fun create(isNullable: Boolean) = if (isNullable) NULLABLE else NON_NULLABLE
}
}
sealed class KtDenotableType : KtType {
abstract fun asString(): String
}
sealed class KtClassType : KtType
sealed class KtClassType : KtDenotableType(), KtTypeWithNullability {
sealed class KtNonErrorClassType: KtClassType(), KtTypeWithNullability {
abstract val classId: ClassId
abstract val classSymbol: KtClassLikeSymbol
abstract val typeArguments: List<KtTypeArgument>
}
abstract class KtFunctionalType : KtClassType() {
abstract class KtFunctionalType : KtNonErrorClassType() {
abstract val isSuspend: Boolean
abstract val arity: Int
abstract val receiverType: KtType?
@@ -47,24 +44,22 @@ abstract class KtFunctionalType : KtClassType() {
abstract val returnType: KtType
}
abstract class KtUsualClassType : KtClassType()
abstract class KtUsualClassType : KtNonErrorClassType()
abstract class KtErrorType : KtType {
abstract class KtClassErrorType : KtClassType() {
abstract val error: String
}
abstract class KtTypeParameterType : KtDenotableType(), KtTypeWithNullability {
abstract class KtTypeParameterType : KtTypeWithNullability {
abstract val name: Name
abstract val symbol: KtTypeParameterSymbol
}
sealed class KtNonDenotableType : KtType
abstract class KtFlexibleType : KtNonDenotableType() {
abstract class KtFlexibleType : KtType {
abstract val lowerBound: KtType
abstract val upperBound: KtType
}
abstract class KtIntersectionType : KtNonDenotableType() {
abstract class KtIntersectionType : KtType{
abstract val conjuncts: List<KtType>
}
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.frontend.api.tokens.hackyAllowRunningOnEdt
import org.jetbrains.kotlin.idea.frontend.api.symbols.*
import org.jetbrains.kotlin.idea.frontend.api.symbols.markers.*
import org.jetbrains.kotlin.idea.frontend.api.types.KtClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtNonErrorClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
@@ -289,7 +290,7 @@ internal fun FirLightClassBase.createInheritanceList(forExtendsList: Boolean, su
)
fun KtType.needToAddTypeIntoList(): Boolean {
if (this !is KtClassType) return false
if (this !is KtNonErrorClassType) return false
// Do not add redundant "extends java.lang.Object" anywhere
if (this.classId == StandardClassIds.Any) return false
@@ -165,7 +165,7 @@ internal fun KtType.mapSupertype(
kotlinCollectionAsIs: Boolean = false,
annotations: List<KtAnnotationCall>
): PsiClassType? {
if (this !is KtClassType) return null
if (this !is KtNonErrorClassType) return null
require(this is KtFirType)
val contextSymbol = classSymbol
require(contextSymbol is KtFirSymbol<*>)
@@ -313,7 +313,7 @@ internal fun KtType.getTypeNullability(context: KtSymbol, phase: FirResolvePhase
internal val KtType.isUnit get() = isClassTypeWithClassId(DefaultTypeClassIds.UNIT)
internal fun KtType.isClassTypeWithClassId(classId: ClassId): Boolean {
if (this !is KtClassType) return false
if (this !is KtNonErrorClassType) return false
return this.classId == classId
}
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.idea.asJava.mapSupertype
import org.jetbrains.kotlin.idea.frontend.api.isValid
import org.jetbrains.kotlin.idea.frontend.api.symbols.KtTypeParameterSymbol
import org.jetbrains.kotlin.idea.frontend.api.types.KtClassType
import org.jetbrains.kotlin.idea.frontend.api.types.KtNonErrorClassType
import org.jetbrains.kotlin.psi.KtTypeParameter
import org.jetbrains.kotlin.psi.psiUtil.startOffset
@@ -63,7 +64,7 @@ internal class FirLightTypeParameter(
)
typeParameterSymbol.upperBounds
.filterIsInstance<KtClassType>()
.filterIsInstance<KtNonErrorClassType>()
.filter { it.classId != StandardClassIds.Any }
.mapNotNull {
it.mapSupertype(
@@ -316,7 +316,7 @@ internal class KtSymbolByFirBuilder private constructor(
else KtFirUsualClassType(coneType, token, this@KtSymbolByFirBuilder)
}
is ConeTypeParameterType -> KtFirTypeParameterType(coneType, token, this@KtSymbolByFirBuilder)
is ConeClassErrorType -> KtFirErrorType(coneType, token)
is ConeClassErrorType -> KtFirClassErrorType(coneType, token)
is ConeFlexibleType -> KtFirFlexibleType(coneType, token, this@KtSymbolByFirBuilder)
is ConeIntersectionType -> KtFirIntersectionType(coneType, token, this@KtSymbolByFirBuilder)
is ConeDefinitelyNotNullType -> buildKtType(coneType.original)
@@ -19,7 +19,7 @@ import org.jetbrains.kotlin.idea.frontend.api.components.KtExpressionTypeProvide
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.idea.frontend.api.fir.utils.getReferencedElementType
import org.jetbrains.kotlin.idea.frontend.api.tokens.ValidityToken
import org.jetbrains.kotlin.idea.frontend.api.types.KtErrorType
import org.jetbrains.kotlin.idea.frontend.api.types.KtClassErrorType
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
import org.jetbrains.kotlin.idea.frontend.api.withValidityAssertion
import org.jetbrains.kotlin.lexer.KtTokens
@@ -51,7 +51,7 @@ internal class KtFirExpressionTypeProvider(
?: getExpectedTypeByVariableAssignment(expression)
?: getExpectedTypeByPropertyDeclaration(expression)
?: getExpectedTypeByFunctionExpressionBody(expression)
return expectedType.takeIf { it !is KtErrorType }
return expectedType.takeIf { it !is KtClassErrorType }
}
private fun getExpectedTypeOfFunctionParameter(expression: PsiElement): KtType? {