diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/PsiRawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/PsiRawFirBuilder.kt index 1849089bb41..50b751c53e0 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/PsiRawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/PsiRawFirBuilder.kt @@ -50,6 +50,9 @@ import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.runIf import org.jetbrains.kotlin.utils.addToStdlib.shouldNotBeCalled +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment +import org.jetbrains.kotlin.utils.exceptions.requireWithAttachment +import org.jetbrains.kotlin.utils.exceptions.withPsiEntry open class PsiRawFirBuilder( session: FirSession, @@ -1117,17 +1120,19 @@ open class PsiRawFirBuilder( for (declaration in file.declarations) { declarations += when (declaration) { is KtScript -> { - require(file.declarations.size == 1) { "Expect the script to be the only declaration in the file $name" } - convertScript(declaration, this.name) { - for (configurator in baseSession.extensionService.scriptConfigurators) { - with(configurator) { configure(this@buildFile) } - } + requireWithAttachment(file.declarations.size == 1, message = { "Expect the script to be the only declaration in the file" }) { + withEntry("fileName", file.name) + } + convertScript(declaration, this.name) { + for (configurator in baseSession.extensionService.scriptConfigurators) { + with(configurator) { configure(this@buildFile) } } } - is KtDestructuringDeclaration -> buildErrorTopLevelDestructuringDeclaration(declaration.toFirSourceElement()) - else -> declaration.convert() } + is KtDestructuringDeclaration -> buildErrorTopLevelDestructuringDeclaration(declaration.toFirSourceElement()) + else -> declaration.convert() } + } for (danglingModifierList in file.danglingModifierLists) { declarations += buildErrorTopLevelDeclarationForDanglingModifierList(danglingModifierList) @@ -2262,7 +2267,9 @@ open class PsiRawFirBuilder( is KtEscapeStringTemplateEntry -> KtNodeTypes.ESCAPE_STRING_TEMPLATE_ENTRY is KtSimpleNameStringTemplateEntry -> KtNodeTypes.SHORT_STRING_TEMPLATE_ENTRY is KtBlockStringTemplateEntry -> KtNodeTypes.LONG_STRING_TEMPLATE_ENTRY - else -> error("invalid node type $element") + else -> errorWithAttachment("invalid node type ${element::class.java}") { + withPsiEntry("element", element) + } } }, convertTemplateEntry = { diff --git a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/AbstractRawFirBuilder.kt b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/AbstractRawFirBuilder.kt index f2bfd4b3e5c..dadb5de567d 100644 --- a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/AbstractRawFirBuilder.kt +++ b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/AbstractRawFirBuilder.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.fir.builder +import com.intellij.psi.PsiElement import com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.* import org.jetbrains.kotlin.KtNodeTypes.* @@ -38,6 +39,9 @@ import org.jetbrains.kotlin.parsing.* import org.jetbrains.kotlin.psi.KtPsiUtil import org.jetbrains.kotlin.types.ConstantValueKind import org.jetbrains.kotlin.util.OperatorNameConventions +import org.jetbrains.kotlin.utils.exceptions.ExceptionAttachmentBuilder +import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment +import org.jetbrains.kotlin.utils.exceptions.withPsiEntry //T can be either PsiElement, or LighterASTNode abstract class AbstractRawFirBuilder(val baseSession: FirSession, val context: Context = Context()) { @@ -417,10 +421,20 @@ abstract class AbstractRawFirBuilder(val baseSession: FirSession, val context setType = false ) else -> - throw AssertionError("Unknown literal type: $type, $text") + errorWithAttachment("Unknown literal type: $type") { + withSourceElementEntry("literal", expression) + } } } + protected fun ExceptionAttachmentBuilder.withSourceElementEntry(name: String, element: T?) { + when (element) { + is PsiElement -> withPsiEntry(name, element) + else -> withEntry(name, element) { it.asText} + } + } + + fun convertUnaryPlusMinusCallOnIntegerLiteralIfNecessary( source: T, receiver: FirExpression,