From e710cbb0bb5bc3a4aba632354dbb9e65dbc5c44f Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 29 Dec 2017 17:34:30 +0300 Subject: [PATCH] Don't fail during error reporting on invalid elements --- .../kotlin/diagnostics/DiagnosticUtils.java | 24 +++++++++++++++---- .../kotlin/util/KotlinFrontEndException.kt | 9 ++++++- .../jetbrains/kotlin/util/exceptionUtil.kt | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/DiagnosticUtils.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/DiagnosticUtils.java index 35df5a61af1..d15c852e1cb 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/DiagnosticUtils.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/DiagnosticUtils.java @@ -24,6 +24,7 @@ import com.intellij.openapi.util.TextRange; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiInvalidElementAccessException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; @@ -68,16 +69,31 @@ public class DiagnosticUtils { } public static String atLocation(@NotNull PsiElement element) { - return atLocation(element.getNode()); + if (element.isValid()) { + return atLocation(element.getContainingFile(), element.getTextRange()); + } + + PsiFile file = null; + int offset = -1; + try { + file = element.getContainingFile(); + offset = element.getTextOffset(); + } + catch (PsiInvalidElementAccessException invalidException) { + // ignore + } + + return "at offset: " + (offset != -1 ? offset : "") + " file: " + (file != null ? file : ""); } public static String atLocation(@NotNull ASTNode node) { int startOffset = node.getStartOffset(); PsiElement element = getClosestPsiElement(node); - if (element != null && element.isValid()) { - return atLocation(element.getContainingFile(), element.getTextRange()); + if (element != null) { + return atLocation(element); } - return "' at offset " + startOffset + " (line and file unknown: no PSI element)"; + + return "at offset " + startOffset + " (line and file unknown: no PSI element)"; } @Nullable diff --git a/compiler/frontend/src/org/jetbrains/kotlin/util/KotlinFrontEndException.kt b/compiler/frontend/src/org/jetbrains/kotlin/util/KotlinFrontEndException.kt index 3fa4632f984..297eeb4440c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/util/KotlinFrontEndException.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/util/KotlinFrontEndException.kt @@ -15,6 +15,13 @@ class KotlinFrontEndException(message: String, cause: Throwable) : KotlinExcepti cause: Throwable, element: PsiElement ) : this(getExceptionMessage("Front-end", message, cause, DiagnosticUtils.atLocation(element)), cause) { - withAttachment("element.kt", element.text) + withAttachment( + "element.kt", + if (element.isValid) { + element.text + } else { + "PsiElement (invalid): " + element.toString() + } + ) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt index a5eba68fa73..d1179f0d96f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt @@ -20,7 +20,7 @@ fun getExceptionMessage( } if (location != null) { - result.append("File being compiled and position: ").append(location).append("\n") + result.append("File being compiled at position: ").append(location).append("\n") } else { result.append("Element is unknown")