Skip to content

Commit

Permalink
[FE 1.0] Refactor error utils: split error entities and introduce err…
Browse files Browse the repository at this point in the history
…or type and error scope kinds
  • Loading branch information
petukhovv authored and teamcity committed Mar 23, 2022
1 parent 8c1fcdd commit b5933c7
Show file tree
Hide file tree
Showing 139 changed files with 1,061 additions and 1,204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -82,23 +83,25 @@ 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)
}

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)
}

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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.jetbrains.kotlin.resolve.source.KotlinSourceElementKt;
import org.jetbrains.kotlin.storage.LockBasedStorageManager;
import org.jetbrains.kotlin.storage.NotNullLazyValue;
import org.jetbrains.kotlin.types.ErrorUtils;
import org.jetbrains.kotlin.types.error.ErrorUtils;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.org.objectweb.asm.Label;
import org.jetbrains.org.objectweb.asm.MethodVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature;
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature;
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor;
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.KotlinType;
import org.jetbrains.org.objectweb.asm.FieldVisitor;
import org.jetbrains.org.objectweb.asm.MethodVisitor;
Expand Down Expand Up @@ -483,7 +484,7 @@ else if (delegateExpression != null) {

if (delegateType == null) {
// Delegation convention is unresolved
delegateType = ErrorUtils.createErrorType("Delegate type");
delegateType = ErrorUtils.createErrorType(ErrorTypeKind.TYPE_FOR_DELEGATION, delegateExpression.getText());
}
return delegateType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
import org.jetbrains.kotlin.resolve.jvm.diagnostics.Synthetic
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature
import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.TypeUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.resolve.jvm.AsmTypes.OBJECT_TYPE
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeFactory
import org.jetbrains.kotlin.types.TypeConstructorSubstitution
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.error.ErrorTypeKind
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
import org.jetbrains.kotlin.util.OperatorNameConventions
Expand Down Expand Up @@ -277,7 +276,7 @@ fun ModuleDescriptor.getResult(kotlinType: KotlinType) =
it,
arguments = listOf(kotlinType.asTypeProjection())
)
} ?: ErrorUtils.createErrorType("For Result")
} ?: ErrorUtils.createErrorType(ErrorTypeKind.TYPE_FOR_RESULT)

fun FunctionDescriptor.isBuiltInSuspendCoroutineUninterceptedOrReturnInJvm() =
getUserData(INITIAL_DESCRIPTOR_FOR_SUSPEND_FUNCTION)?.isBuiltInSuspendCoroutineUninterceptedOrReturn() == true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.jetbrains.kotlin.serialization.AnnotationSerializer
import org.jetbrains.kotlin.serialization.KotlinSerializerExtensionBase
import org.jetbrains.kotlin.serialization.deserialization.builtins.BuiltInSerializerProtocol
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.UnresolvedType
import org.jetbrains.kotlin.types.typeUtil.isUnresolvedType

class BuiltInsSerializerExtension : KotlinSerializerExtensionBase(BuiltInSerializerProtocol) {
private val shortNameToClassId = mapOf(
Expand Down Expand Up @@ -52,9 +52,9 @@ class BuiltInsSerializerExtension : KotlinSerializerExtensionBase(BuiltInSeriali
private val KotlinType.presentableName: String
get() {
val unwrapped = unwrap()
if (unwrapped !is UnresolvedType) {
throw UnsupportedOperationException("Error types which are not UnresolvedType instances are not supported here: $unwrapped")
if (!isUnresolvedType(unwrapped)) {
throw UnsupportedOperationException("Error types which are not unresolved type instances are not supported here: $unwrapped")
}
return unwrapped.presentableName
return unwrapped.debugMessage.removePrefix("Unresolved type for ")
}
}
Loading

0 comments on commit b5933c7

Please sign in to comment.