From 2552540f7165571dd2fdefdef4f09d858e4fa92a Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 28 Nov 2019 19:17:30 +0100 Subject: [PATCH] IR: wrap exceptions and add file path in per-file lowerings Also add file path to the wrapped exception in JVM IR codegen --- .../kotlin/backend/common/CodegenUtil.kt | 13 ++++++ .../codegen/CompilationErrorHandler.java | 40 ------------------- .../kotlin/codegen/MultifileClassCodegen.kt | 10 +---- .../kotlin/codegen/PackageCodegenImpl.java | 9 +---- .../jetbrains/kotlin/util/exceptionUtil.kt | 4 +- .../backend/common/phaser/PhaseBuilders.kt | 7 +++- .../kotlin/backend/jvm/JvmBackendFacade.kt | 10 ++--- 7 files changed, 27 insertions(+), 66 deletions(-) delete mode 100644 compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationErrorHandler.java diff --git a/compiler/backend-common/src/org/jetbrains/kotlin/backend/common/CodegenUtil.kt b/compiler/backend-common/src/org/jetbrains/kotlin/backend/common/CodegenUtil.kt index e7eb0413350..2e31900053b 100644 --- a/compiler/backend-common/src/org/jetbrains/kotlin/backend/common/CodegenUtil.kt +++ b/compiler/backend-common/src/org/jetbrains/kotlin/backend/common/CodegenUtil.kt @@ -23,7 +23,9 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.multiplatform.ExpectedActualResolver import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.util.getExceptionMessage import org.jetbrains.kotlin.utils.DFS +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments object CodegenUtil { @JvmStatic @@ -236,4 +238,15 @@ object CodegenUtil { { it.declaresDefaultValue() } ) } + + @JvmStatic + fun reportBackendException(exception: Throwable, phase: String, fileUrl: String?): Nothing { + // CompilationException (the only KotlinExceptionWithAttachments possible here) is already supposed + // to have all information about the context. + if (exception is KotlinExceptionWithAttachments) throw exception + throw IllegalStateException( + getExceptionMessage("Backend", "Exception during $phase", exception, fileUrl), + exception + ) + } } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationErrorHandler.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationErrorHandler.java deleted file mode 100644 index 5cfeb270e6d..00000000000 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationErrorHandler.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.codegen; - -import org.jetbrains.kotlin.util.ExceptionUtilKt; - -import static org.jetbrains.kotlin.utils.ExceptionUtilsKt.rethrow; - -public class CompilationErrorHandler { - public static void reportException(Throwable exception, String fileUrl) { - // CompilationException is already supposed to have all information about the context - if (exception instanceof CompilationException) { - try { - throw exception; - } - catch (Throwable t) { - throw rethrow(t); - } - } - - throw new IllegalStateException( - ExceptionUtilKt.getExceptionMessage("Backend", "Exception during code generation", exception, fileUrl), - exception - ); - } -} diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/MultifileClassCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/MultifileClassCodegen.kt index 691114f6c68..5ad1359b9b5 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/MultifileClassCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/MultifileClassCodegen.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.codegen -import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.progress.ProcessCanceledException import org.jetbrains.kotlin.backend.common.CodegenUtil import org.jetbrains.kotlin.codegen.context.FieldOwnerContext @@ -25,7 +24,6 @@ import org.jetbrains.kotlin.codegen.state.GenerationState import org.jetbrains.kotlin.config.IncrementalCompilation import org.jetbrains.kotlin.config.JvmAnalysisFlags import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.diagnostics.DiagnosticUtils import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader @@ -171,11 +169,7 @@ class MultifileClassCodegenImpl( } catch (e: ProcessCanceledException) { throw e } catch (e: Throwable) { - CompilationErrorHandler.reportException(e, file.virtualFile?.url ?: "no file") - DiagnosticUtils.throwIfRunningOnServer(e) - if (ApplicationManager.getApplication().isInternal) { - e.printStackTrace() - } + CodegenUtil.reportBackendException(e, "multi-file class part code generation", file.virtualFile?.url) } } } @@ -288,7 +282,7 @@ class MultifileClassCodegenImpl( } object DelegateToCompiledMemberGenerationStrategy : FunctionGenerationStrategy() { - override fun skipNotNullAssertionsForParameters(): kotlin.Boolean { + override fun skipNotNullAssertionsForParameters(): Boolean { throw IllegalStateException("shouldn't be called") } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/PackageCodegenImpl.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/PackageCodegenImpl.java index a8fd5901a77..d117b158301 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/PackageCodegenImpl.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/PackageCodegenImpl.java @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.codegen; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.SmartList; @@ -28,7 +27,6 @@ import org.jetbrains.kotlin.codegen.context.PackageContext; import org.jetbrains.kotlin.codegen.state.GenerationState; import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor; -import org.jetbrains.kotlin.diagnostics.DiagnosticUtils; import org.jetbrains.kotlin.fileClasses.JvmFileClassInfo; import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil; import org.jetbrains.kotlin.name.FqName; @@ -73,12 +71,7 @@ public class PackageCodegenImpl implements PackageCodegen { } catch (Throwable e) { VirtualFile vFile = file.getVirtualFile(); - CompilationErrorHandler.reportException(e, vFile == null ? "no file" : vFile.getUrl()); - DiagnosticUtils.throwIfRunningOnServer(e); - if (ApplicationManager.getApplication().isInternal()) { - //noinspection CallToPrintStackTrace - e.printStackTrace(); - } + CodegenUtil.reportBackendException(e, "file facade code generation", vFile == null ? null : vFile.getUrl()); } } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt index 41be20f3439..6c264bdca93 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/util/exceptionUtil.kt @@ -17,9 +17,9 @@ fun getExceptionMessage( append(subsystemName).append(" Internal error: ").appendln(message) if (location != null) { - append("File being compiled at position: ").appendln(location) + append("File being compiled: ").appendln(location) } else { - append("Element is unknown") + appendln("File is unknown") } if (cause != null) { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseBuilders.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseBuilders.kt index 2be814a01a9..27c7c41ed1a 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseBuilders.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseBuilders.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.backend.common.phaser +import org.jetbrains.kotlin.backend.common.CodegenUtil import org.jetbrains.kotlin.backend.common.CommonBackendContext import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.lower @@ -174,7 +175,11 @@ fun performByIrFile( input: IrModuleFragment ): IrModuleFragment { for (irFile in input.files) { - lower.invoke(phaseConfig, phaserState.changeType(), context, irFile) + try { + lower.invoke(phaseConfig, phaserState.changeType(), context, irFile) + } catch (e: Throwable) { + CodegenUtil.reportBackendException(e, "IR lowering", irFile.fileEntry.name) + } } // TODO: no guarantee that module identity is preserved by `lower` diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendFacade.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendFacade.kt index cbddd52b1e8..2c607fba278 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendFacade.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendFacade.kt @@ -5,13 +5,13 @@ package org.jetbrains.kotlin.backend.jvm +import org.jetbrains.kotlin.backend.common.CodegenUtil import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig import org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen import org.jetbrains.kotlin.backend.jvm.lower.MultifileFacadeFileEntry import org.jetbrains.kotlin.backend.jvm.serialization.JvmMangler -import org.jetbrains.kotlin.codegen.CompilationErrorHandler import org.jetbrains.kotlin.codegen.state.GenerationState import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrModuleFragment @@ -71,11 +71,7 @@ object JvmBackendFacade { context.typeMapper.mapType(context.referenceClass(descriptor).defaultType) } - try { - JvmLower(context).lower(irModuleFragment) - } catch (e: Throwable) { - CompilationErrorHandler.reportException(e, null) - } + JvmLower(context).lower(irModuleFragment) for (generateMultifileFacade in listOf(true, false)) { for (irFile in irModuleFragment.files) { @@ -95,7 +91,7 @@ object JvmBackendFacade { } state.afterIndependentPart() } catch (e: Throwable) { - CompilationErrorHandler.reportException(e, null) // TODO ktFile.virtualFile.url + CodegenUtil.reportBackendException(e, "code generation", irFile.fileEntry.name) } } }