[FE 1.0] Refactor error utils: split error entities and introduce error type and error scope kinds

This commit is contained in:
Victor Petukhov
2022-03-17 13:32:36 +04:00
committed by teamcity
parent 8c1fcddea3
commit b5933c70e2
139 changed files with 1061 additions and 1204 deletions
@@ -28,7 +28,8 @@ import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.util.getType
import org.jetbrains.kotlin.resolve.sam.SamConstructorDescriptor
import org.jetbrains.kotlin.resolve.sam.getFunctionTypeForAbstractMethod
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.checker.intersectWrappedTypes
import org.jetbrains.kotlin.types.typeUtil.makeNullable
@@ -73,7 +74,7 @@ class KtFe10ExpressionTypeProvider(
if (typeReference != null) {
val bindingContext = analysisContext.analyze(typeReference, AnalysisMode.PARTIAL)
val kotlinType = bindingContext[BindingContext.TYPE, typeReference]
?: ErrorUtils.createErrorType("Return type \"${typeReference.text}\" cannot be resolved")
?: ErrorUtils.createErrorType(ErrorTypeKind.RETURN_TYPE, typeReference.text)
return kotlinType.toKtType(analysisContext)
}
@@ -82,7 +83,7 @@ class KtFe10ExpressionTypeProvider(
if (declaration is KtFunction && declaration !is KtConstructor<*> && declaration.equalsToken != null) {
val bindingContext = analysisContext.analyze(declaration)
val kotlinType = bindingContext[BindingContext.FUNCTION, declaration]?.returnType
?: ErrorUtils.createErrorType("Implicit return type for function \"${declaration.name}\" cannot be resolved")
?: ErrorUtils.createErrorType(ErrorTypeKind.IMPLICIT_RETURN_TYPE_FOR_FUNCTION, declaration.name ?: "<unknown>")
return kotlinType.toKtType(analysisContext)
}
@@ -90,7 +91,7 @@ class KtFe10ExpressionTypeProvider(
if (declaration is KtProperty) {
val bindingContext = analysisContext.analyze(declaration)
val kotlinType = bindingContext[BindingContext.VARIABLE, declaration]?.returnType
?: ErrorUtils.createErrorType("Implicit return type for property \"${declaration.name}\" cannot be resolved")
?: ErrorUtils.createErrorType(ErrorTypeKind.IMPLICIT_RETURN_TYPE_FOR_PROPERTY, declaration.name ?: "<unknown>")
return kotlinType.toKtType(analysisContext)
}
@@ -98,7 +99,9 @@ class KtFe10ExpressionTypeProvider(
if (declaration is KtPropertyAccessor) {
val bindingContext = analysisContext.analyze(declaration)
val kotlinType = bindingContext[BindingContext.PROPERTY_ACCESSOR, declaration]?.returnType
?: ErrorUtils.createErrorType("Return type for property accessor \"${declaration.property.name}\" cannot be resolved")
?: ErrorUtils.createErrorType(
ErrorTypeKind.IMPLICIT_RETURN_TYPE_FOR_PROPERTY_ACCESSOR, declaration.property.name ?: "<unknown>"
)
return kotlinType.toKtType(analysisContext)
}
@@ -112,7 +115,7 @@ class KtFe10ExpressionTypeProvider(
if (property != null && property.setter == propertyAccessor) {
val bindingContext = analysisContext.analyze(property)
val kotlinType = bindingContext[BindingContext.VARIABLE, property]?.returnType
?: ErrorUtils.createErrorType("Return type for property \"${declaration.name}\" cannot be resolved")
?: ErrorUtils.createErrorType(ErrorTypeKind.RETURN_TYPE_FOR_PROPERTY, declaration.name ?: "<unknown>")
return kotlinType.toKtType(analysisContext)
}
@@ -122,7 +125,9 @@ class KtFe10ExpressionTypeProvider(
if (declaration is KtConstructor<*>) {
val bindingContext = analysisContext.analyze(declaration)
val kotlinType = bindingContext[BindingContext.CONSTRUCTOR, declaration]?.returnType
?: ErrorUtils.createErrorType("Return type for constructor \"${declaration.containingClass()?.name}\" cannot be resolved")
?: ErrorUtils.createErrorType(
ErrorTypeKind.RETURN_TYPE_FOR_CONSTRUCTOR, declaration.containingClass()?.name ?: "<unknown>"
)
return kotlinType.toKtType(analysisContext)
}
@@ -146,7 +151,8 @@ class KtFe10ExpressionTypeProvider(
}
val errorMessage = "Descriptor not found for function \"${declaration.name}\""
return ErrorUtils.createErrorTypeWithCustomConstructor(errorMessage, function.typeConstructor).toKtType(analysisContext)
return ErrorUtils.createErrorType(ErrorTypeKind.NOT_FOUND_DESCRIPTOR_FOR_FUNCTION, function.typeConstructor, errorMessage)
.toKtType(analysisContext)
}
override fun getExpectedType(expression: PsiElement): KtType? = withValidityAssertion {
@@ -26,6 +26,9 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.error.ErrorType
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
internal class KtFe10TypeCreator(
@@ -48,8 +51,8 @@ internal class KtFe10TypeCreator(
}
if (descriptor == null) {
val kotlinType = ErrorUtils.createErrorType("Cannot build class type, descriptor not found for builder $builder")
return KtFe10ClassErrorType(kotlinType as ErrorType, analysisContext)
val kotlinType = ErrorUtils.createErrorType(ErrorTypeKind.NOT_FOUND_DESCRIPTOR_FOR_CLASS, builder.toString())
return KtFe10ClassErrorType(kotlinType, analysisContext)
}
val typeParameters = descriptor.typeConstructor.parameters
@@ -77,7 +80,7 @@ internal class KtFe10TypeCreator(
}
}
val kotlinType = descriptor?.defaultType
?: ErrorUtils.createErrorType("Cannot build type parameter type, descriptor not found for builder $builder")
?: ErrorUtils.createErrorType(ErrorTypeKind.NOT_FOUND_DESCRIPTOR_FOR_TYPE_PARAMETER, builder.toString())
return kotlinType.toKtType(analysisContext) as KtTypeParameterType
}
}
@@ -45,6 +45,9 @@ import org.jetbrains.kotlin.resolve.scopes.utils.getImplicitReceiversHierarchy
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.NewCapturedType
import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor
import org.jetbrains.kotlin.types.error.ErrorType
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.util.containingNonLocalDeclaration
@@ -71,7 +74,7 @@ internal class KtFe10TypeProvider(
override fun buildSelfClassType(symbol: KtNamedClassOrObjectSymbol): KtType = withValidityAssertion {
val kotlinType = (getSymbolDescriptor(symbol) as? ClassDescriptor)?.defaultType
?: ErrorUtils.createErrorType("Cannot get class type for unresolved class ${symbol.nameOrAnonymous}")
?: ErrorUtils.createErrorType(ErrorTypeKind.UNRESOLVED_CLASS_TYPE, symbol.nameOrAnonymous.toString())
return kotlinType.toKtType(analysisContext)
}
@@ -84,7 +87,7 @@ internal class KtFe10TypeProvider(
override fun getKtType(ktTypeReference: KtTypeReference): KtType = withValidityAssertion {
val bindingContext = analysisContext.analyze(ktTypeReference, AnalysisMode.PARTIAL)
val kotlinType = bindingContext[BindingContext.TYPE, ktTypeReference]
?: ErrorUtils.createErrorType("Cannot resolve type reference ${ktTypeReference.text}")
?: ErrorUtils.createErrorType(ErrorTypeKind.UNRESOLVED_TYPE, ktTypeReference.text)
return kotlinType.toKtType(analysisContext)
}
@@ -50,6 +50,9 @@ import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.NewCapturedType
import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor
import org.jetbrains.kotlin.types.error.ErrorType
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.error.ErrorUtils
internal val MemberDescriptor.ktSymbolKind: KtSymbolKind
get() {
@@ -229,7 +232,7 @@ internal fun KotlinType.toKtType(analysisContext: Fe10AnalysisContext): KtType {
return if (newTypeParameterDescriptor != null) {
KtFe10TypeParameterType(unwrappedType, newTypeParameterDescriptor, analysisContext)
} else {
KtFe10ClassErrorType(ErrorUtils.createErrorType("Unresolved type parameter type") as ErrorType, analysisContext)
KtFe10ClassErrorType(ErrorUtils.createErrorType(ErrorTypeKind.UNRESOLVED_TYPE_PARAMETER_TYPE), analysisContext)
}
}
@@ -241,8 +244,8 @@ internal fun KotlinType.toKtType(analysisContext: Fe10AnalysisContext): KtType {
is FunctionClassDescriptor -> KtFe10FunctionalType(unwrappedType, typeDeclaration, analysisContext)
is ClassDescriptor -> KtFe10UsualClassType(unwrappedType, typeDeclaration, analysisContext)
else -> {
val errorType = ErrorUtils.createErrorTypeWithCustomConstructor("Unresolved class type", typeConstructor)
KtFe10ClassErrorType(errorType as ErrorType, analysisContext)
val errorType = ErrorUtils.createErrorType(ErrorTypeKind.UNRESOLVED_CLASS_TYPE, typeConstructor, typeDeclaration.toString())
KtFe10ClassErrorType(errorType, analysisContext)
}
}
@@ -26,12 +26,11 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.hasBody
import org.jetbrains.kotlin.psi.psiUtil.isTopLevelInFileOrScript
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.types.ErrorType
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.error.ErrorUtils
internal val KtDeclaration.ktVisibility: Visibility?
get() = when {
@@ -149,6 +148,6 @@ internal fun PsiElement.getResolutionScope(bindingContext: BindingContext): Lexi
internal fun KtFe10Symbol.createErrorType(): KtType {
val type = ErrorUtils.createErrorType("Type is unavailable for declaration $psi") as ErrorType
val type = ErrorUtils.createErrorType(ErrorTypeKind.UNAVAILABLE_TYPE_FOR_DECLARATION, psi.toString())
return KtFe10ClassErrorType(type, analysisContext)
}
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
import org.jetbrains.kotlin.analysis.api.types.KtClassErrorType
import org.jetbrains.kotlin.analysis.api.types.KtTypeNullability
import org.jetbrains.kotlin.analysis.api.withValidityAssertion
import org.jetbrains.kotlin.types.ErrorType
import org.jetbrains.kotlin.types.error.ErrorType
internal class KtFe10ClassErrorType(
override val type: ErrorType,
@@ -23,7 +23,7 @@ internal class KtFe10ClassErrorType(
override fun asStringForDebugging(): String = withValidityAssertion { type.asStringForDebugging() }
override val error: String
get() = withValidityAssertion { "Type \"${type.presentableName}\" is unresolved" }
get() = withValidityAssertion { "Type \"${type.debugMessage}\" is unresolved" }
override val candidateClassSymbols: Collection<KtClassLikeSymbol>
get() = withValidityAssertion { emptyList() }
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
import org.jetbrains.org.objectweb.asm.Type
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.NewCapturedType
import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor
import org.jetbrains.kotlin.types.error.ErrorType
import org.jetbrains.kotlin.types.typeUtil.builtIns
internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, private val isDebugText: Boolean = false) {
@@ -14,6 +14,8 @@ import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext
import org.jetbrains.kotlin.types.checker.NewTypeVariableConstructor
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
@@ -43,7 +45,8 @@ internal class KtFe10TypeSystemCommonBackendContextForTypeMapping(
override fun TypeConstructorMarker.defaultType(): KotlinTypeMarker {
require(this is TypeConstructor)
val declaration = declarationDescriptor ?: return ErrorUtils.createErrorType("Unresolved declaration descriptor ($this)")
val declaration = declarationDescriptor
?: return ErrorUtils.createErrorType(ErrorTypeKind.UNRESOLVED_DECLARATION, this.toString())
return declaration.defaultType
}
@@ -78,7 +81,7 @@ internal class KtFe10TypeSystemCommonBackendContextForTypeMapping(
val declaration = declarationDescriptor
if (declaration == null) {
val errorArguments = arguments.map { TypeProjectionImpl(it as KotlinType) }
return ErrorUtils.createErrorTypeWithArguments("Unresolved type constructor $this", errorArguments)
return ErrorUtils.createErrorTypeWithArguments(ErrorTypeKind.UNRESOLVED_TYPE, errorArguments, this.toString())
}
val substitutions = LinkedHashMap<TypeConstructor, TypeProjection>(parameters.size)
@@ -106,7 +109,8 @@ internal class KtFe10TypeSystemCommonBackendContextForTypeMapping(
override fun continuationTypeConstructor(): TypeConstructorMarker {
val continuationFqName = StandardClassIds.Continuation.asSingleFqName()
val foundClasses = resolveSession.getTopLevelClassifierDescriptors(continuationFqName, NoLookupLocation.FROM_IDE)
return foundClasses.firstOrNull()?.typeConstructor ?: ErrorUtils.createErrorTypeConstructor("Cannot find $continuationFqName")
return foundClasses.firstOrNull()?.typeConstructor
?: ErrorUtils.createErrorTypeConstructor(ErrorTypeKind.NOT_FOUND_FQNAME, continuationFqName.toString())
}
override fun functionNTypeConstructor(n: Int): TypeConstructorMarker {