diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/references/KtFirReferenceResolver.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/references/KtFirReferenceResolver.kt index c74ecd4d191..bc6bb83764b 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/references/KtFirReferenceResolver.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/references/KtFirReferenceResolver.kt @@ -12,9 +12,9 @@ import com.intellij.psi.impl.source.resolve.ResolveCache import org.jetbrains.kotlin.analysis.api.KtAllowAnalysisOnEdt import org.jetbrains.kotlin.analysis.api.analyze import org.jetbrains.kotlin.analysis.api.lifetime.allowAnalysisOnEdt -import org.jetbrains.kotlin.diagnostics.PsiDiagnosticUtils -import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment import org.jetbrains.kotlin.utils.exceptions.shouldIjPlatformExceptionBeRethrown +import org.jetbrains.kotlin.utils.exceptions.withPsiEntry object KtFirReferenceResolver : ResolveCache.PolyVariantResolver { class KotlinResolveResult(element: PsiElement) : PsiElementResolveResult(element) @@ -29,18 +29,11 @@ object KtFirReferenceResolver : ResolveCache.PolyVariantResolver { } catch (e: Exception) { if (shouldIjPlatformExceptionBeRethrown(e)) throw e - throw KtReferenceResolveException(ref, e) + errorWithAttachment("Unable to resolve reference ${ref.element::class.java}", cause = e) { + withPsiEntry("reference", ref.element) + } } resolveToPsiElements.map { KotlinResolveResult(it) }.toTypedArray() } } -} - -class KtReferenceResolveException( - reference: KtReference, - cause: Throwable -) : KotlinExceptionWithAttachments("Unable to resolve reference at: ${PsiDiagnosticUtils.atLocation(reference.element)}", cause) { - init { - withPsiAttachment("element.kt", reference.element) - } } \ No newline at end of file diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/api/InvalidFirElementTypeException.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/api/InvalidFirElementTypeException.kt index a2bb0f827de..07c9c1650f0 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/api/InvalidFirElementTypeException.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/api/InvalidFirElementTypeException.kt @@ -5,14 +5,14 @@ package org.jetbrains.kotlin.analysis.low.level.api.fir.api -import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry -import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry -import org.jetbrains.kotlin.fir.utils.exceptions.withFirSymbolEntry import org.jetbrains.kotlin.fir.FirElement import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType +import org.jetbrains.kotlin.fir.utils.exceptions.withConeTypeEntry +import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry +import org.jetbrains.kotlin.fir.utils.exceptions.withFirSymbolEntry import org.jetbrains.kotlin.psi.KtElement -import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments +import org.jetbrains.kotlin.utils.exceptions.KotlinIllegalArgumentExceptionWithAttachments import org.jetbrains.kotlin.utils.exceptions.buildAttachment import java.util.* import kotlin.reflect.KClass @@ -21,7 +21,7 @@ class InvalidFirElementTypeException( actualFirElement: Any?, ktElement: KtElement?, expectedFirClasses: List>, -) : KotlinExceptionWithAttachments("") { +) : KotlinIllegalArgumentExceptionWithAttachments("") { init { buildAttachment { when (actualFirElement) { diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index 9e8f450ff5e..c246d3d8bd6 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope import org.jetbrains.kotlin.fir.symbols.* 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.ir.ObsoleteDescriptorBasedAPI import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.builders.declarations.UNDEFINED_PARAMETER_INDEX @@ -58,8 +59,8 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.NameUtils import org.jetbrains.kotlin.name.SpecialNames -import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments import org.jetbrains.kotlin.utils.addToStdlib.runUnless +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment import org.jetbrains.kotlin.utils.threadLocal import java.util.concurrent.ConcurrentHashMap @@ -1783,8 +1784,10 @@ class Fir2IrDeclarationStorage( private inline fun convertCatching(element: FirElement, block: () -> R): R { try { return block() - } catch (e: Throwable) { - throw KotlinExceptionWithAttachments("Exception was thrown during transformation of ${element.render()}", e) + } catch (e: Exception) { + errorWithAttachment("Exception was thrown during transformation of ${element::class.java}", cause = e) { + withFirEntry("element", element) + } } } } diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/impl/Utils.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/impl/Utils.kt index 2ba583abdd8..98d2f9eb879 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/impl/Utils.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/impl/Utils.kt @@ -5,33 +5,20 @@ package org.jetbrains.kotlin.fir.symbols.impl -import kotlin.reflect.KClass import org.jetbrains.kotlin.fir.declarations.FirMemberDeclaration import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.declarations.FirResolvedDeclarationStatus -import org.jetbrains.kotlin.fir.psi -import org.jetbrains.kotlin.fir.renderer.FirDeclarationRendererWithAttributes -import org.jetbrains.kotlin.fir.renderer.FirFileAnnotationsContainerRenderer -import org.jetbrains.kotlin.fir.renderer.FirRenderer -import org.jetbrains.kotlin.fir.renderer.FirResolvePhaseRenderer import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase -import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments +import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry +import org.jetbrains.kotlin.fir.utils.exceptions.withFirSymbolIdEntry +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment +import kotlin.reflect.KClass internal fun FirBasedSymbol<*>.errorInLazyResolve(name: String, actualClass: KClass<*>, expected: KClass<*>): Nothing { - throw KotlinExceptionWithAttachments("Unexpected $name. Expected is ${expected.simpleName}, but was ${actualClass.simpleName}").apply { - withAttachment( - "FirElement.txt", - FirRenderer( - resolvePhaseRenderer = FirResolvePhaseRenderer(), - declarationRenderer = FirDeclarationRendererWithAttributes(), - fileAnnotationsContainerRenderer = FirFileAnnotationsContainerRenderer(), - ).renderElementAsString(fir), - ) - - withAttachment("FirBasedSymbol.txt", this@errorInLazyResolve::class.simpleName) - withAttachment("KtSourceElementKind.txt", fir.source?.kind?.let { it::class.simpleName }) - withPsiAttachment("PsiElement.txt", fir.psi) + errorWithAttachment("Unexpected $name. Expected is ${expected.simpleName}, but was ${actualClass.simpleName}") { + withFirEntry("firElement", fir) + withFirSymbolIdEntry("firSymbol", this@errorInLazyResolve) } } diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtDotQualifiedExpression.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtDotQualifiedExpression.kt index 75c25031331..0cc211dcbc6 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtDotQualifiedExpression.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtDotQualifiedExpression.kt @@ -21,7 +21,8 @@ import com.intellij.openapi.diagnostic.Logger import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes import org.jetbrains.kotlin.psi.stubs.elements.KtTokenSets.INSIDE_DIRECTIVE_EXPRESSIONS -import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments +import org.jetbrains.kotlin.utils.exceptions.logErrorWithAttachment +import org.jetbrains.kotlin.utils.exceptions.withPsiEntry class KtDotQualifiedExpression : KtExpressionImplStub>, KtQualifiedExpression { constructor(node: ASTNode) : super(node) @@ -69,11 +70,9 @@ class KtDotQualifiedExpression : KtExpressionImplStub Unit = {}, -): Nothing { - val exception = KotlinIllegalArgumentExceptionWithAttachments(message, cause) - exception.buildAttachment(attachmentName) { buildAttachment() } - throw exception -} inline fun Logger.logErrorWithAttachment( message: String, @@ -75,8 +64,27 @@ inline fun Logger.logErrorWithAttachment( attachmentName: String = "info.txt", buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {}, ) { - val attachment = Attachment(attachmentName, ExceptionAttachmentBuilder().apply(buildAttachment).buildString()) - this.error(message, cause, attachment) + this.error(buildErrorWithAttachment(message, cause, attachmentName, buildAttachment)) +} + +inline fun buildErrorWithAttachment( + message: String, + cause: Exception? = null, + attachmentName: String = "info.txt", + buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {}, +): Throwable { + val exception = KotlinIllegalArgumentExceptionWithAttachments(message, cause) + exception.buildAttachment(attachmentName) { buildAttachment() } + return exception +} + +inline fun errorWithAttachment( + message: String, + cause: Exception? = null, + attachmentName: String = "info.txt", + buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {}, +): Nothing { + throw buildErrorWithAttachment(message, cause, attachmentName, buildAttachment) } inline fun rethrowExceptionWithDetails(