diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/FirVisibilityChecker.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/FirVisibilityChecker.kt index b6c8bef3b43..619bef97d34 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/FirVisibilityChecker.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/FirVisibilityChecker.kt @@ -25,8 +25,10 @@ import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.types.AbstractTypeChecker +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment abstract class FirModuleVisibilityChecker : FirSessionComponent { abstract fun isInFriendModule(declaration: FirMemberDeclaration): Boolean @@ -463,7 +465,9 @@ fun FirBasedSymbol<*>.getOwnerLookupTag(): ConeClassLikeLookupTag? { is FirClassLikeSymbol<*> -> getContainingClassLookupTag() is FirCallableSymbol<*> -> containingClassLookupTag() is FirScriptSymbol -> null - else -> error("Unsupported owner search for ${fir.javaClass}: ${fir.render()}") + else -> errorWithAttachment("Unsupported owner search for ${fir::class.java}") { + withFirEntry("ownerDeclaration", fir) + } } } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/LookupTagUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/LookupTagUtils.kt index ed936bf0a77..8941d2d0e27 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/LookupTagUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/LookupTagUtils.kt @@ -22,10 +22,12 @@ import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl +import org.jetbrains.kotlin.fir.utils.exceptions.withFirLookupTagEntry import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.ConstantValueKind import org.jetbrains.kotlin.util.WeakPair +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment fun ConeClassifierLookupTag.toSymbol(useSiteSession: FirSession): FirClassifierSymbol<*>? = when (this) { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt index 693fe7c0865..efe153d49d6 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt @@ -15,12 +15,14 @@ import org.jetbrains.kotlin.fir.resolve.withCombinedAttributesFrom import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl +import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.model.TypeConstructorMarker import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker import org.jetbrains.kotlin.types.model.TypeVariableMarker import org.jetbrains.kotlin.types.model.typeConstructor import org.jetbrains.kotlin.utils.addToStdlib.runIf +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment abstract class AbstractConeSubstitutor(protected val typeContext: ConeTypeContext) : ConeSubstitutor() { protected fun wrapProjection(old: ConeTypeProjection, newType: ConeKotlinType): ConeTypeProjection { @@ -160,7 +162,9 @@ abstract class AbstractConeSubstitutor(protected val typeContext: ConeTypeContex newArguments as Array, attributes ) - else -> error("Unknown class-like type to substitute: $this, ${this::class}") + else -> errorWithAttachment("Unknown class-like type to substitute, ${this::class}") { + withConeTypeEntry("type", this@substituteArguments) + } } } return null diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/AbstractFirOverrideScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/AbstractFirOverrideScope.kt index 9a45fd41783..0a5cc66626c 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/AbstractFirOverrideScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/AbstractFirOverrideScope.kt @@ -13,6 +13,8 @@ import org.jetbrains.kotlin.fir.scopes.FirOverrideChecker import org.jetbrains.kotlin.fir.scopes.FirTypeScope import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol +import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment abstract class AbstractFirOverrideScope( val session: FirSession, @@ -54,7 +56,10 @@ internal fun FirOverrideChecker.similarFunctionsOrBothProperties( overrideCandidate is FirConstructor -> false overrideCandidate is FirProperty -> baseDeclaration is FirProperty && isOverriddenProperty(overrideCandidate, baseDeclaration) overrideCandidate is FirField -> baseDeclaration is FirField - else -> error("Unknown fir callable type: $overrideCandidate, $baseDeclaration") + else -> errorWithAttachment("Unknown fir callable type") { + withFirEntry("overrideCandidate", overrideCandidate) + withFirEntry("baseDeclaration", baseDeclaration) + } } } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt index 41a411269ea..23de849479a 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt @@ -24,8 +24,10 @@ import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.utils.addToStdlib.runIf +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment class FirClassSubstitutionScope( private val session: FirSession, @@ -365,7 +367,9 @@ class FirSubstitutionOverrideStorage(val session: FirSession) : FirSessionCompon when (original) { is FirPropertySymbol -> scope.createSubstitutionOverrideProperty(original) is FirFieldSymbol -> scope.createSubstitutionOverrideField(original) - else -> error("symbol $original is not overridable") + else -> errorWithAttachment("symbol ${original::class.java} is not overridable") { + withFirEntry("original", original.fir) + } } } } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirDelegatedMemberScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirDelegatedMemberScope.kt index a0231100fb9..6d4ede7db35 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirDelegatedMemberScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirDelegatedMemberScope.kt @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.name.CallableId import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.StandardClassIds +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment class FirDelegatedMemberScope( private val session: FirSession, @@ -221,7 +222,9 @@ fun FirSimpleFunction.isPublicInAny(): Boolean { return when (name.asString()) { "hashCode", "toString" -> valueParameters.isEmpty() "equals" -> valueParameters.singleOrNull()?.hasTypeOf(StandardClassIds.Any, allowNullable = true) == true - else -> error("Unexpected method name: $name") + else -> errorWithAttachment("Unexpected method name") { + withEntry("methodName", name) { name.asString() } + } } } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirIntegerConstantOperatorScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirIntegerConstantOperatorScope.kt index e2b8514e0d5..bd103c4f722 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirIntegerConstantOperatorScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirIntegerConstantOperatorScope.kt @@ -21,7 +21,9 @@ import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef +import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract @@ -41,7 +43,9 @@ class FirIntegerConstantOperatorScope( scopeSession, FakeOverrideTypeCalculator.DoNothing, requiredMembersPhase = FirResolvePhase.STATUS, - ) ?: error("Scope for $baseType not found") + ) ?: errorWithAttachment("Scope for ${baseType::class.java} not found") { + withConeTypeEntry("type", baseType) + } } private val mappedFunctions = mutableMapOf() diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt index 2dd73eb6d48..88b0aac8298 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeInferenceContext.kt @@ -24,12 +24,14 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirAnonymousObjectSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl +import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry import org.jetbrains.kotlin.name.StandardClassIds import org.jetbrains.kotlin.types.AbstractTypeChecker import org.jetbrains.kotlin.types.AbstractTypeRefiner import org.jetbrains.kotlin.types.TypeCheckerState import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.utils.DFS +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeContext { @@ -142,7 +144,9 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo override fun KotlinTypeMarker.typeDepth() = when (this) { is ConeSimpleKotlinType -> typeDepth() is ConeFlexibleType -> maxOf(lowerBound().typeDepth(), upperBound().typeDepth()) - else -> error("Type should be simple or flexible: $this") + else -> errorWithAttachment("Type should be simple or flexible: ${this::class.java}") { + withConeTypeEntry("type", this@typeDepth as? ConeKotlinType) + } } override fun SimpleTypeMarker.typeDepth(): Int { @@ -526,7 +530,10 @@ interface ConeInferenceContext : TypeSystemInferenceExtensionContext, ConeTypeCo isFunction } ?: false } - functionalSupertype ?: error("Failed to find functional supertype for $simpleType") + functionalSupertype + ?: errorWithAttachment("Failed to find functional supertype for ${simpleType::class.java}") { + withConeTypeEntry("type", simpleType) + } } } } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt index 5054517942d..0b89bb0eba8 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt @@ -21,15 +21,20 @@ import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap import org.jetbrains.kotlin.fir.resolve.toSymbol import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag +import org.jetbrains.kotlin.fir.symbols.ConeClassifierLookupTag import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase +import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry +import org.jetbrains.kotlin.fir.utils.exceptions.withFirLookupTagEntry import org.jetbrains.kotlin.name.* import org.jetbrains.kotlin.types.TypeCheckerState import org.jetbrains.kotlin.types.TypeCheckerState.SupertypesPolicy.DoCustomTransform import org.jetbrains.kotlin.types.TypeCheckerState.SupertypesPolicy.LowerIfFlexible import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext import org.jetbrains.kotlin.types.model.* +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment +import org.jetbrains.kotlin.utils.exceptions.withPsiEntry class ErrorTypeConstructor(val reason: String) : TypeConstructorMarker { override fun toString(): String = reason @@ -85,7 +90,9 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty is ConeClassLikeType -> fullyExpandedType(session) is ConeSimpleKotlinType -> this is ConeFlexibleType -> null - else -> error("Unknown simpleType: $this") + else -> errorWithAttachment("Unknown simpleType: ${this::class.java}") { + withConeTypeEntry("type", this@asSimpleType as? ConeKotlinType) + } } } @@ -154,7 +161,9 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty is ConeStubType -> constructor is ConeDefinitelyNotNullType -> original.typeConstructor() is ConeIntegerLiteralType -> this - else -> error("?: $this") + else -> errorWithAttachment("Unknown simpleType: ${this::class.java}") { + withConeTypeEntry("type", this@typeConstructor as? ConeKotlinType) + } } } @@ -261,7 +270,9 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty is FirAnonymousObjectSymbol -> symbol.fir.typeParameters[index].symbol.toLookupTag() is FirRegularClassSymbol -> symbol.fir.typeParameters[index].symbol.toLookupTag() is FirTypeAliasSymbol -> symbol.fir.typeParameters[index].symbol.toLookupTag() - else -> error("Unexpected FirClassLikeSymbol $symbol for ${this::class}, with classId ${(this as? ConeClassLikeLookupTag)?.classId}") + else -> errorWithAttachment("Unexpected FirClassLikeSymbol $symbol for ${this::class}") { + withFirLookupTagEntry("lookupTag", this@getParameter as? ConeClassLikeLookupTag) + } } } @@ -632,7 +643,10 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty } private fun TypeConstructorMarker.unknownConstructorError(): Nothing { - error("Unknown type constructor: ${this::class}") + errorWithAttachment("Unknown type constructor: ${this::class.java}") { + withEntry("constructor", this@unknownConstructorError) { it.toString() } + withFirLookupTagEntry("constructorAsLookupTag", this@unknownConstructorError as? ConeClassifierLookupTag) + } } override fun substitutionSupertypePolicy(type: SimpleTypeMarker): TypeCheckerState.SupertypesPolicy { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypePreparator.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypePreparator.kt index b60446f1d7e..f37b8ae360f 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypePreparator.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypePreparator.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.types.AbstractTypePreparator import org.jetbrains.kotlin.types.model.KotlinTypeMarker +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment class ConeTypePreparator(val session: FirSession) : AbstractTypePreparator() { private fun prepareType(type: ConeSimpleKotlinType): ConeSimpleKotlinType { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/FunctionalTypeUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/FunctionalTypeUtils.kt index 5b3bb135935..3c1875bf95e 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/FunctionalTypeUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/FunctionalTypeUtils.kt @@ -23,10 +23,12 @@ import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol +import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.types.AbstractTypeChecker import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.utils.addToStdlib.runUnless +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment // ---------------------------------------------- is type is a function type ---------------------------------------------- @@ -46,7 +48,9 @@ private inline fun ConeKotlinType.isFunctionTypeWithPredicate( predicate: (FunctionTypeKind) -> Boolean ): Boolean { val kind = functionTypeKind(session) - ?: if (errorOnNotFunctionType) error("$this is not a function type") else return false + ?: if (errorOnNotFunctionType) errorWithAttachment("${this::class.java} is not a function type") { + withConeTypeEntry("type", this@isFunctionTypeWithPredicate) + } else return false return predicate(kind) } diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt index 3c6a57910e0..632cfdc7db2 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/TypeUtils.kt @@ -30,10 +30,12 @@ import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl import org.jetbrains.kotlin.fir.types.lowerBoundIfFlexible +import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry import org.jetbrains.kotlin.resolve.calls.NewCommonSuperTypeCalculator import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.utils.addToStdlib.runIf +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment import org.jetbrains.kotlin.fir.types.lowerBoundIfFlexible as coneLowerBoundIfFlexible import org.jetbrains.kotlin.fir.types.upperBoundIfFlexible as coneUpperBoundIfFlexible @@ -122,7 +124,9 @@ fun T.withArguments(arguments: Array ConeErrorType(diagnostic, isUninferredParameter, arguments, attributes) as T is ConeClassLikeTypeImpl -> ConeClassLikeTypeImpl(lookupTag, arguments, nullability.isNullable, attributes) as T is ConeDefinitelyNotNullType -> ConeDefinitelyNotNullType(original.withArguments(arguments)) as T - else -> error("Not supported: $this: ${this.renderForDebugging()}") + else -> errorWithAttachment("Not supported: ${this::class}") { + withConeTypeEntry("type", this@withArguments) + } } } @@ -154,7 +158,9 @@ fun T.withAttributes(attributes: ConeAttributes): T { // Attributes for stub types are not supported, and it's not obvious if it should is ConeStubType -> this is ConeIntegerLiteralType -> this - else -> error("Not supported: $this: ${this.renderForDebugging()}") + else -> errorWithAttachment("Not supported: ${this::class}") { + withConeTypeEntry("type", this@withAttributes) + } } as T } @@ -729,7 +735,9 @@ private fun ConeKotlinType.eraseAsUpperBound( original.eraseAsUpperBound(session, cache, mode) .makeConeTypeDefinitelyNotNullOrNotNull(session.typeContext) - else -> error("unexpected Java type parameter upper bound kind: $this") + else -> errorWithAttachment("unexpected Java type parameter upper bound kind: ${this::class}") { + withConeTypeEntry("type", this@eraseAsUpperBound) + } } fun ConeKotlinType.isRaw(): Boolean = lowerBoundIfFlexible().attributes.contains(CompilerConeAttributes.RawType)