[Analysis API] do not rethrow java.lang.Error

Errors as OutOfMemoryError should not be caught or wrapped
This commit is contained in:
Ilya Kirillov
2022-12-13 13:15:48 +01:00
committed by Space Team
parent 9cf77e19be
commit d7ee312b83
6 changed files with 34 additions and 27 deletions
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.analysis.api.fir.components
import org.jetbrains.kotlin.util.SourceCodeAnalysisException
import org.jetbrains.kotlin.analysis.api.calls.*
import org.jetbrains.kotlin.analysis.api.diagnostics.KtDiagnostic
import org.jetbrains.kotlin.analysis.api.diagnostics.KtNonBoundToPsiErrorDiagnostic
@@ -30,11 +29,16 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFirSafe
import org.jetbrains.kotlin.analysis.low.level.api.fir.resolver.AllCandidatesResolver
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.errorWithFirSpecificEntries
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.withFirEntry
import org.jetbrains.kotlin.analysis.utils.errors.buildErrorWithAttachment
import org.jetbrains.kotlin.analysis.utils.errors.rethrowExceptionWithDetails
import org.jetbrains.kotlin.analysis.utils.errors.withPsiEntry
import org.jetbrains.kotlin.analysis.utils.printer.parentOfType
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.analysis.checkers.toRegularClassSymbol
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
import org.jetbrains.kotlin.fir.declarations.fullyExpandedClass
import org.jetbrains.kotlin.fir.diagnostics.FirDiagnosticHolder
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.builder.buildFunctionCall
import org.jetbrains.kotlin.fir.expressions.impl.FirNoReceiverExpression
@@ -71,11 +75,6 @@ import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.util.OperatorNameConventions.EQUALS
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.analysis.utils.errors.buildErrorWithAttachment
import org.jetbrains.kotlin.analysis.utils.errors.withPsiEntry
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.diagnostics.FirDiagnosticHolder
import org.jetbrains.kotlin.util.shouldIjPlatformExceptionBeRethrown
internal class KtFirCallResolver(
override val analysisSession: KtFirAnalysisSession,
@@ -1236,11 +1235,10 @@ internal class KtFirCallResolver(
private inline fun <R> wrapError(element: KtElement, action: () -> R): R {
return try {
action()
} catch (e: Throwable) {
if (shouldIjPlatformExceptionBeRethrown(e)) throw e
buildErrorWithAttachment(
} catch (e: Exception) {
rethrowExceptionWithDetails(
"Error during resolving call ${element::class.java.name}",
cause = if (e is SourceCodeAnalysisException) e.cause else e,
exception = e,
) {
withPsiEntry("psi", element)
element.getOrBuildFir(firResolveSession)?.let { withFirEntry("fir", it) }
@@ -5,8 +5,6 @@
package org.jetbrains.kotlin.idea.references
import com.intellij.openapi.diagnostic.ControlFlowException
import com.intellij.openapi.project.IndexNotReadyException
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementResolveResult
import com.intellij.psi.ResolveResult
@@ -27,7 +25,7 @@ object KtFirReferenceResolver : ResolveCache.PolyVariantResolver<KtReference> {
return allowAnalysisOnEdt {
val resolveToPsiElements = try {
analyze(ref.expression) { ref.getResolvedToPsi(this) }
} catch (e: Throwable) {
} catch (e: Exception) {
if (shouldIjPlatformExceptionBeRethrown(e)) throw e
throw KtReferenceResolveException(ref, e)
@@ -7,6 +7,8 @@ package org.jetbrains.kotlin.analysis.utils.errors
import org.jetbrains.kotlin.analysis.utils.printer.PrettyPrinter
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
import org.jetbrains.kotlin.util.SourceCodeAnalysisException
import org.jetbrains.kotlin.util.shouldIjPlatformExceptionBeRethrown
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@@ -59,7 +61,7 @@ public inline fun KotlinExceptionWithAttachments.buildAttachment(
public inline fun buildErrorWithAttachment(
message: String,
cause: Throwable? = null,
cause: Exception? = null,
attachmentName: String = "info.txt",
buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {}
): Nothing {
@@ -68,6 +70,18 @@ public inline fun buildErrorWithAttachment(
throw exception
}
public inline fun rethrowExceptionWithDetails(
message: String,
exception: Exception,
attachmentName: String = "info.txt",
buildAttachment: ExceptionAttachmentBuilder.() -> Unit = {}
): Nothing {
if (shouldIjPlatformExceptionBeRethrown(exception)) throw exception
val unwrappedException = if (exception is SourceCodeAnalysisException) exception.cause else exception
if (unwrappedException !is Exception) throw unwrappedException
buildErrorWithAttachment(message, unwrappedException, attachmentName, buildAttachment)
}
@OptIn(ExperimentalContracts::class)
public inline fun checkWithAttachmentBuilder(
@@ -118,7 +118,7 @@ internal class ValueWithPostCompute<KEY, VALUE, DATA>(
_postCompute!!(key, calculated, data)
calculated
} catch (e: Throwable) {
if (exceptionShouldBeSavedInCache(e)) {
if (e is Exception && exceptionShouldBeSavedInCache(e)) {
value = ExceptionWasThrownDuringValueComputation(e)
} else {
value = ValueIsNotComputed
@@ -131,7 +131,7 @@ internal class ValueWithPostCompute<KEY, VALUE, DATA>(
return calculatedValue
}
private fun exceptionShouldBeSavedInCache(exception: Throwable): Boolean =
private fun exceptionShouldBeSavedInCache(exception: Exception): Boolean =
!shouldIjPlatformExceptionBeRethrown(exception)
@@ -19,7 +19,7 @@ import org.jetbrains.kotlin.analysis.low.level.api.fir.transformers.LLFirLazyTra
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.checkCanceled
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.findSourceNonLocalFirDeclaration
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.withFirEntry
import org.jetbrains.kotlin.analysis.utils.errors.buildErrorWithAttachment
import org.jetbrains.kotlin.analysis.utils.errors.rethrowExceptionWithDetails
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirElementWithResolvePhase
import org.jetbrains.kotlin.fir.declarations.*
@@ -33,8 +33,6 @@ import org.jetbrains.kotlin.psi.KtClassBody
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtEnumEntry
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.util.SourceCodeAnalysisException
import org.jetbrains.kotlin.util.shouldIjPlatformExceptionBeRethrown
internal class LLFirModuleLazyDeclarationResolver(val moduleComponents: LLFirModuleResolveComponents) {
@@ -87,8 +85,8 @@ internal class LLFirModuleLazyDeclarationResolver(val moduleComponents: LLFirMod
scopeSession = scopeSession,
)
}
} catch (e: Throwable) {
handleExceptionFromResolve(e,moduleComponents.sessionInvalidator, firFile, fromPhase, toPhase)
} catch (e: Exception) {
handleExceptionFromResolve(e, moduleComponents.sessionInvalidator, firFile, fromPhase, toPhase)
}
}
@@ -184,7 +182,7 @@ internal class LLFirModuleLazyDeclarationResolver(val moduleComponents: LLFirMod
val fromPhase = target.resolvePhase
try {
doLazyResolve(target, scopeSession, toPhase)
} catch (e: Throwable) {
} catch (e: Exception) {
handleExceptionFromResolve(e, moduleComponents.sessionInvalidator, target, fromPhase, toPhase)
}
}
@@ -346,15 +344,14 @@ internal class LLFirModuleLazyDeclarationResolver(val moduleComponents: LLFirMod
}
private fun handleExceptionFromResolve(
e: Throwable,
exception: Exception,
sessionInvalidator: LLFirSessionInvalidator,
firDeclarationToResolve: FirElementWithResolvePhase,
fromPhase: FirResolvePhase,
toPhase: FirResolvePhase?
): Nothing {
sessionInvalidator.invalidate(firDeclarationToResolve.llFirSession)
if (shouldIjPlatformExceptionBeRethrown(e)) throw e
buildErrorWithAttachment(
rethrowExceptionWithDetails(
buildString {
val moduleData = firDeclarationToResolve.llFirModuleData
appendLine("Error while resolving ${firDeclarationToResolve::class.java.name} ")
@@ -366,7 +363,7 @@ private fun handleExceptionFromResolve(
appendLine("KtModule: ${moduleData.ktModule::class}")
appendLine("platform: ${moduleData.ktModule.platform}")
},
cause = if (e is SourceCodeAnalysisException) e.cause else e,
exception = exception,
) {
withEntry("KtModule", firDeclarationToResolve.llFirModuleData.ktModule) { it.moduleDescription }
withEntry("session", firDeclarationToResolve.llFirSession) { it.toString() }
@@ -43,7 +43,7 @@ fun ExceptionAttachmentBuilder.withConeTypeEntry(name: String, coneType: ConeKot
fun errorWithFirSpecificEntries(
message: String,
cause: Throwable? = null,
cause: Exception? = null,
fir: FirElement? = null,
coneType: ConeKotlinType? = null,
psi: PsiElement? = null,