From 96198a1cd3e39dca1aafa5a8ba09d04f973509ea Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Tue, 15 Oct 2019 13:58:54 +0200 Subject: [PATCH] Report no parent KtNamedDeclaration for declaration instead of NPE in BindingContextUtils.getEnclosingDescriptor #EA-210816 Fixed --- .../{ => bindingContextUtil}/BindingContextUtils.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) rename compiler/frontend/src/org/jetbrains/kotlin/resolve/{ => bindingContextUtil}/BindingContextUtils.kt (92%) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/BindingContextUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/bindingContextUtil/BindingContextUtils.kt similarity index 92% rename from compiler/frontend/src/org/jetbrains/kotlin/resolve/BindingContextUtils.kt rename to compiler/frontend/src/org/jetbrains/kotlin/resolve/bindingContextUtil/BindingContextUtils.kt index 0bf5c04988e..dc14cbbf48f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/BindingContextUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/bindingContextUtil/BindingContextUtils.kt @@ -37,6 +37,7 @@ import org.jetbrains.kotlin.resolve.scopes.utils.takeSnapshot import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.expressions.typeInfoFactory.noTypeInfo import org.jetbrains.kotlin.types.typeUtil.makeNotNullable +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments fun KtReturnExpression.getTargetFunctionDescriptor(context: BindingContext): FunctionDescriptor? { val targetLabel = getTargetLabel() @@ -133,12 +134,16 @@ fun KtElement.getParentOfTypeCodeFragmentAware(vararg parentCla } fun getEnclosingDescriptor(context: BindingContext, element: KtElement): DeclarationDescriptor { - val declaration = element.getParentOfTypeCodeFragmentAware(KtNamedDeclaration::class.java) + val declaration = + element.getParentOfTypeCodeFragmentAware(KtNamedDeclaration::class.java) + ?: throw KotlinExceptionWithAttachments("No parent KtNamedDeclaration for of type ${element.javaClass}") + .withAttachment("element.kt", element.text) return if (declaration is KtFunctionLiteral) { getEnclosingDescriptor(context, declaration) } else { context.get(DECLARATION_TO_DESCRIPTOR, declaration) - ?: error("No descriptor for named declaration: " + declaration?.text + "\n(of type " + declaration?.javaClass + ")") + ?: throw KotlinExceptionWithAttachments("No descriptor for named declaration of type ${declaration?.javaClass}") + .withAttachment("declaration.kt", declaration.text) } }