[IR] Improve error messages for constant evaluation error

This commit is contained in:
Alejandro Serrano Mena
2023-11-02 14:55:40 +01:00
committed by Space Team
parent 1bb5e97b62
commit 312187dbe6
3 changed files with 19 additions and 6 deletions
@@ -9,6 +9,7 @@ import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.backend.common.BackendDiagnosticRenderers.EXPECT_ACTUAL_ANNOTATION_INCOMPATIBILITY
import org.jetbrains.kotlin.backend.common.BackendDiagnosticRenderers.INCOMPATIBILITY
import org.jetbrains.kotlin.backend.common.BackendDiagnosticRenderers.DECLARATION_NAME
import org.jetbrains.kotlin.backend.common.BackendDiagnosticRenderers.EVALUATION_ERROR_EXPLANATION
import org.jetbrains.kotlin.backend.common.BackendDiagnosticRenderers.SYMBOL_OWNER_DECLARATION_FQ_NAME
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.diagnostics.*
@@ -64,7 +65,7 @@ object KtDefaultCommonBackendErrorMessages : BaseDiagnosticRendererFactory() {
map.put(
CommonBackendErrors.EVALUATION_ERROR,
"Cannot evaluate constant expression: {0}",
STRING,
EVALUATION_ERROR_EXPLANATION,
)
map.put(
CommonBackendErrors.ACTUAL_ANNOTATION_CONFLICTING_DEFAULT_ARGUMENT_VALUE,
@@ -91,4 +92,16 @@ object BackendDiagnosticRenderers {
"Annotation `$expectAnnotationFqName` $reason"
}
val DECLARATION_NAME = Renderer<IrDeclarationWithName> { it.name.asString() }
val EVALUATION_ERROR_EXPLANATION = Renderer<String> {
val result = Regex("Exception (\\S+)(: (.*))?").matchEntire(it)?.groupValues
val exceptionName = result?.getOrNull(1)
val message = result?.getOrNull(3)
when {
!message.isNullOrBlank() -> message
exceptionName == StackOverflowError::class.java.name -> "stack overflow (potentially due to infinite recursion)"
exceptionName == NullPointerException::class.java.name -> "null reference access"
exceptionName != null -> exceptionName.split('.').last()
else -> it
}
}
}
@@ -1,5 +1,5 @@
/exceptionFromInterpreter.kt:(195,200): error: Cannot evaluate constant expression: Exception java.lang.ArithmeticException: / by zero
/exceptionFromInterpreter.kt:(195,200): error: Cannot evaluate constant expression: / by zero
/exceptionFromInterpreter.kt:(239,254): error: Cannot evaluate constant expression: Exception java.lang.IllegalArgumentException: marginPrefix must be non-blank string.
/exceptionFromInterpreter.kt:(239,254): error: Cannot evaluate constant expression: marginPrefix must be non-blank string.
/exceptionFromInterpreter.kt:(303,308): error: Cannot evaluate constant expression: Exception java.lang.ArithmeticException: / by zero
/exceptionFromInterpreter.kt:(303,308): error: Cannot evaluate constant expression: / by zero
@@ -1,3 +1,3 @@
/recursionAccess.kt:(178,194): error: Cannot evaluate constant expression: Exception java.lang.StackOverflowError
/recursionAccess.kt:(178,194): error: Cannot evaluate constant expression: stack overflow (potentially due to infinite recursion)
/recursionAccess.kt:(267,283): error: Cannot evaluate constant expression: Exception java.lang.NullPointerException
/recursionAccess.kt:(267,283): error: Cannot evaluate constant expression: null reference access