Improve exception message when failing to serialize anonymous object metadata

(See KT-20996)

Old message:

    Cannot get FQ name of local class: class <no name provided>

New message:

    Cannot get FQ name of local class: class <no name provided> defined in private fun x(): <no name provided> defined in root package in file 1.kt
This commit is contained in:
Alexander Udalov
2018-06-14 19:12:03 +02:00
parent 29adf09b7e
commit 9bf2d129ee
@@ -8,21 +8,26 @@ package org.jetbrains.kotlin.serialization
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.metadata.serialization.StringTable import org.jetbrains.kotlin.metadata.serialization.StringTable
import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.descriptorUtil.classId import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.ErrorUtils
interface DescriptorAwareStringTable : StringTable { interface DescriptorAwareStringTable : StringTable {
fun getFqNameIndex(descriptor: ClassifierDescriptorWithTypeParameters): Int { fun getFqNameIndex(descriptor: ClassifierDescriptorWithTypeParameters): Int {
if (ErrorUtils.isError(descriptor)) { if (ErrorUtils.isError(descriptor)) {
throw IllegalStateException("Cannot get FQ name of error class: $descriptor") throw IllegalStateException("Cannot get FQ name of error class: ${renderDescriptor(descriptor)}")
} }
val classId = descriptor.classId val classId = descriptor.classId
?: getLocalClassIdReplacement(descriptor) ?: getLocalClassIdReplacement(descriptor)
?: throw IllegalStateException("Cannot get FQ name of local class: $descriptor") ?: throw IllegalStateException("Cannot get FQ name of local class: ${renderDescriptor(descriptor)}")
return getQualifiedClassNameIndex(classId.asString(), classId.isLocal) return getQualifiedClassNameIndex(classId.asString(), classId.isLocal)
} }
fun getLocalClassIdReplacement(descriptor: ClassifierDescriptorWithTypeParameters): ClassId? = null fun getLocalClassIdReplacement(descriptor: ClassifierDescriptorWithTypeParameters): ClassId? = null
private fun renderDescriptor(descriptor: ClassifierDescriptorWithTypeParameters): String =
DescriptorRenderer.COMPACT.render(descriptor) + " defined in " +
DescriptorRenderer.FQ_NAMES_IN_TYPES.render(descriptor.containingDeclaration)
} }