diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationException.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationException.java index d3765564c74..6b1dd7814e4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationException.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/CompilationException.java @@ -1,17 +1,6 @@ /* - * 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. + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.codegen; @@ -19,14 +8,22 @@ package org.jetbrains.kotlin.codegen; import com.intellij.psi.PsiElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.diagnostics.DiagnosticUtils; import org.jetbrains.kotlin.util.ExceptionUtilKt; +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments; -public class CompilationException extends RuntimeException { +public class CompilationException extends KotlinExceptionWithAttachments { private final PsiElement element; public CompilationException(@NotNull String message, @Nullable Throwable cause, @Nullable PsiElement element) { - super(ExceptionUtilKt.getExceptionMessage("Back-end (JVM)", message, cause, element), cause); + super(ExceptionUtilKt.getExceptionMessage("Back-end (JVM)", message, cause, + element == null ? null : DiagnosticUtils.atLocation(element)), + cause); this.element = element; + + if (element != null) { + withAttachment("element.kt", element.getText()); + } } @Nullable diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExceptionLogger.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExceptionLogger.kt index 946732271cb..56e12b3cd43 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExceptionLogger.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExceptionLogger.kt @@ -1,21 +1,11 @@ /* - * Copyright 2010-2016 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. + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.codegen +import com.intellij.openapi.diagnostic.Attachment import com.intellij.openapi.diagnostic.Logger import com.intellij.psi.PsiElement import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext @@ -23,7 +13,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext object ExceptionLogger { @JvmStatic fun logDescriptorNotFound(problemDescription: String, psi: PsiElement): AssertionError { - LOG.error(problemDescription, psi.getElementTextWithContext()) + LOG.error(problemDescription, Attachment("psi.kt", psi.getElementTextWithContext())) throw AssertionError(problemDescription) } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenBinding.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenBinding.java index 44cfada189c..17734da594c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenBinding.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/binding/CodegenBinding.java @@ -1,17 +1,6 @@ /* - * Copyright 2010-2016 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. + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.codegen.binding; @@ -33,6 +22,7 @@ import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; import org.jetbrains.kotlin.util.slicedMap.BasicWritableSlice; import org.jetbrains.kotlin.util.slicedMap.Slices; import org.jetbrains.kotlin.util.slicedMap.WritableSlice; +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments; import org.jetbrains.org.objectweb.asm.Type; import java.util.*; @@ -124,7 +114,8 @@ public class CodegenBinding { return asmTypeForAnonymousClass(bindingContext, variableDescriptor); } - throw new IllegalStateException("Couldn't compute ASM type for " + PsiUtilsKt.getElementTextWithContext(expression)); + throw new KotlinExceptionWithAttachments("Couldn't compute ASM type for expression") + .withAttachment("expression.kt", PsiUtilsKt.getElementTextWithContext(expression)); } @NotNull diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/AbsentDescriptorHandler.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/AbsentDescriptorHandler.kt index 61495d6adc5..eff680bfacd 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/AbsentDescriptorHandler.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/AbsentDescriptorHandler.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2016 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. + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.resolve.lazy @@ -19,6 +8,7 @@ package org.jetbrains.kotlin.resolve.lazy import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments interface AbsentDescriptorHandler { fun diagnoseDescriptorNotFound(declaration: KtDeclaration): DeclarationDescriptor @@ -29,5 +19,9 @@ class BasicAbsentDescriptorHandler : AbsentDescriptorHandler { } class NoDescriptorForDeclarationException @JvmOverloads constructor(declaration: KtDeclaration, additionalDetails: String? = null) : - IllegalStateException("Descriptor wasn't found for declaration $declaration\n${declaration.getElementTextWithContext()}" - + (additionalDetails?.let { "\n---------------------------------------------------\n$it" } ?: "")) + KotlinExceptionWithAttachments("Descriptor wasn't found for declaration $declaration" + + (additionalDetails?.let { "\n---------------------------------------------------\n$it" } ?: "")) { + init { + withAttachment("declaration.kt", declaration.getElementTextWithContext()) + } +} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorDispatcher.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorDispatcher.java index 346d53b669a..719a106230f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorDispatcher.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/ExpressionTypingVisitorDispatcher.java @@ -1,17 +1,6 @@ /* - * Copyright 2010-2016 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. + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.types.expressions; @@ -41,6 +30,7 @@ import org.jetbrains.kotlin.util.KotlinFrontEndException; import org.jetbrains.kotlin.util.LookupTrackerUtilKt; import org.jetbrains.kotlin.util.PerformanceCounter; import org.jetbrains.kotlin.util.ReenteringLazyValueComputationException; +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments; import static org.jetbrains.kotlin.diagnostics.Errors.TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM; @@ -240,8 +230,8 @@ public abstract class ExpressionTypingVisitorDispatcher extends KtVisitor { val result = StringBuilder(subsystemName + " Internal error: ").append(message).append("\n") if (cause != null) { @@ -32,9 +19,8 @@ fun getExceptionMessage( result.append("Cause: ").append(causeMessage ?: cause.toString()).append("\n") } - if (element != null) { - result.append("File being compiled and position: ").append(DiagnosticUtils.atLocation(element)).append("\n") - result.append("PsiElement: ").append(element.text).append("\n") + if (location != null) { + result.append("File being compiled and position: ").append(location).append("\n") } else { result.append("Element is unknown") diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/builder/LightClassDataHolder.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/builder/LightClassDataHolder.kt index 004011c0082..03efd1b583a 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/builder/LightClassDataHolder.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/builder/LightClassDataHolder.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2016 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. + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.asJava.builder @@ -34,6 +23,7 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.debugText.getDebugText import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments interface LightClassDataHolder { val javaFileStub: PsiJavaFileStub @@ -107,7 +97,10 @@ fun PsiJavaFileStub.findDelegate(classOrObject: KtClassOrObject): PsiClass { } val stubFileText = DebugUtil.stubTreeToString(this) - throw IllegalStateException("Couldn't get delegate for ${classOrObject.getDebugText()}\nin $ktFileText\nstub: \n$stubFileText") + throw KotlinExceptionWithAttachments("Couldn't get delegate for class") + .withAttachment(classOrObject.name ?: "unnamed class or object", classOrObject.getDebugText()) + .withAttachment("file.kt", ktFileText) + .withAttachment("stub text", stubFileText) } fun PsiJavaFileStub.findDelegate(classFqName: FqName): PsiClass { diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt b/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt new file mode 100644 index 00000000000..4f41b808a70 --- /dev/null +++ b/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.utils + +import com.intellij.openapi.diagnostic.Attachment +import com.intellij.openapi.diagnostic.ExceptionWithAttachments + +open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttachments { + private val attachments = mutableListOf() + + constructor(message: String) : super(message) + + constructor(message: String?, cause: Throwable?) : super(message, cause) + + override fun getAttachments(): Array = attachments.toTypedArray() + + fun withAttachment(name: String, content: String?): KotlinExceptionWithAttachments { + attachments.add(Attachment(name, content ?: "")) + return this + } +} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/lightClasses/LazyLightClassDataHolder.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/lightClasses/LazyLightClassDataHolder.kt index caffceb413a..9088ac88e01 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/lightClasses/LazyLightClassDataHolder.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/lightClasses/LazyLightClassDataHolder.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 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. + * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.idea.caches.resolve.lightClasses @@ -34,7 +23,10 @@ import org.jetbrains.kotlin.asJava.elements.KtLightFieldImpl import org.jetbrains.kotlin.asJava.elements.KtLightMethod import org.jetbrains.kotlin.asJava.elements.KtLightMethodImpl import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.NotNullableUserDataProperty +import org.jetbrains.kotlin.psi.debugText.getDebugText +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments typealias ExactLightClassContextProvider = () -> LightClassConstructionContext typealias DummyLightClassContextProvider = (() -> LightClassConstructionContext?)? @@ -151,18 +143,19 @@ sealed class LazyLightClassDataHolder( } private sealed class LazyLightClassMemberMatchingError(message: String, containingClass: KtLightClass) - : AssertionError(message) { + : KotlinExceptionWithAttachments(message) { init { containingClass.kotlinOrigin?.hasLightClassMatchingErrors = true + withAttachment("class.kt", (containingClass as KtElement).getDebugText()) } class NoMatch(dummyMember: PsiMember, containingClass: KtLightClass) : LazyLightClassMemberMatchingError( - "Couldn't match ${dummyMember.debugName} in $containingClass", containingClass + "Couldn't match ${dummyMember.debugName}", containingClass ) class WrongMatch(realMember: PsiMember, dummyMember: PsiMember, containingClass: KtLightClass) : LazyLightClassMemberMatchingError( - "Matched ${dummyMember.debugName} to ${realMember.debugName} in $containingClass", containingClass + "Matched ${dummyMember.debugName} to ${realMember.debugName}", containingClass ) }