Skip unresolved JvmStatic/JvmField annotations in builtins

This is needed to implement KT-30084 and KT-30083 after bootstrap.
This commit is contained in:
Alexander Udalov
2020-06-19 17:02:03 +02:00
parent abfc74c8b2
commit 05e8546bdb
5 changed files with 51 additions and 21 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.serialization.builtins
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.builtins.BuiltInsBinaryVersion
import org.jetbrains.kotlin.metadata.deserialization.BinaryVersion
import org.jetbrains.kotlin.serialization.AnnotationSerializer
import org.jetbrains.kotlin.serialization.KotlinSerializerExtensionBase
import org.jetbrains.kotlin.serialization.deserialization.builtins.BuiltInSerializerProtocol
import org.jetbrains.kotlin.types.KotlinType
@@ -31,20 +32,29 @@ class BuiltInsSerializerExtension : KotlinSerializerExtensionBase(BuiltInSeriali
"CharRange" to "kotlin/ranges/CharRange"
)
override fun createAnnotationSerializer(): AnnotationSerializer = object : AnnotationSerializer(stringTable) {
override fun ignoreAnnotation(type: KotlinType): Boolean =
type.presentableName == "JvmStatic" || type.presentableName == "JvmField" || super.ignoreAnnotation(type)
}
override val metadataVersion: BinaryVersion
get() = BuiltInsBinaryVersion.INSTANCE
override fun shouldUseTypeTable(): Boolean = true
override fun serializeErrorType(type: KotlinType, builder: ProtoBuf.Type.Builder) {
val unwrapped = type.unwrap()
if (unwrapped !is UnresolvedType) {
throw UnsupportedOperationException("Error types which are not UnresolvedType instances are not supported here: $unwrapped")
}
val className = shortNameToClassId[unwrapped.presentableName]
?: throw UnsupportedOperationException("Unsupported unresolved type: $unwrapped")
val className = shortNameToClassId[type.presentableName]
?: throw UnsupportedOperationException("Unsupported unresolved type: ${type.unwrap()}")
builder.className = stringTable.getQualifiedClassNameIndex(className, false)
}
private val KotlinType.presentableName: String
get() {
val unwrapped = unwrap()
if (unwrapped !is UnresolvedType) {
throw UnsupportedOperationException("Error types which are not UnresolvedType instances are not supported here: $unwrapped")
}
return unwrapped.presentableName
}
}
@@ -27,11 +27,13 @@ import org.jetbrains.kotlin.metadata.deserialization.Flags
import org.jetbrains.kotlin.resolve.constants.*
import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
class AnnotationSerializer(private val stringTable: DescriptorAwareStringTable) {
fun serializeAnnotation(annotation: AnnotationDescriptor): ProtoBuf.Annotation = ProtoBuf.Annotation.newBuilder().apply {
open class AnnotationSerializer(private val stringTable: DescriptorAwareStringTable) {
fun serializeAnnotation(annotation: AnnotationDescriptor): ProtoBuf.Annotation? = ProtoBuf.Annotation.newBuilder().apply {
val annotationClass = annotation.annotationClass ?: error("Annotation type is not a class: ${annotation.type}")
if (ErrorUtils.isError(annotationClass)) {
if (ignoreAnnotation(annotation.type)) return null
error("Unresolved annotation type: ${annotation.type} at ${annotation.source.containingFile}")
}
@@ -173,4 +175,6 @@ class AnnotationSerializer(private val stringTable: DescriptorAwareStringTable)
}
}, Unit)
}
protected open fun ignoreAnnotation(type: KotlinType): Boolean = false
}
@@ -452,7 +452,7 @@ class DescriptorSerializer private constructor(
}
for (annotation in descriptor.nonSourceAnnotations) {
builder.addAnnotation(extension.annotationSerializer.serializeAnnotation(annotation))
builder.addAnnotation(extension.annotationSerializer.serializeAnnotation(annotation)!!)
}
extension.serializeTypeAlias(descriptor, builder)
@@ -18,7 +18,9 @@ abstract class SerializerExtension {
abstract val metadataVersion: BinaryVersion
val annotationSerializer by lazy { AnnotationSerializer(stringTable) }
val annotationSerializer by lazy { createAnnotationSerializer() }
protected open fun createAnnotationSerializer(): AnnotationSerializer = AnnotationSerializer(stringTable)
open fun shouldUseTypeTable(): Boolean = false
open fun shouldUseNormalizedVisibility(): Boolean = false
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.serialization.MutableVersionRequirementTable
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.protobuf.GeneratedMessageLite
import org.jetbrains.kotlin.resolve.constants.NullValue
import org.jetbrains.kotlin.resolve.descriptorUtil.nonSourceAnnotations
import org.jetbrains.kotlin.types.KotlinType
@@ -34,7 +35,7 @@ abstract class KotlinSerializerExtensionBase(private val protocol: SerializerExt
childSerializer: DescriptorSerializer
) {
for (annotation in descriptor.nonSourceAnnotations) {
proto.addExtension(protocol.classAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.classAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
}
@@ -48,7 +49,7 @@ abstract class KotlinSerializerExtensionBase(private val protocol: SerializerExt
childSerializer: DescriptorSerializer
) {
for (annotation in descriptor.nonSourceAnnotations) {
proto.addExtension(protocol.constructorAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.constructorAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
}
@@ -59,7 +60,7 @@ abstract class KotlinSerializerExtensionBase(private val protocol: SerializerExt
childSerializer: DescriptorSerializer
) {
for (annotation in descriptor.nonSourceAnnotations) {
proto.addExtension(protocol.functionAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.functionAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
}
@@ -70,13 +71,13 @@ abstract class KotlinSerializerExtensionBase(private val protocol: SerializerExt
childSerializer: DescriptorSerializer
) {
for (annotation in descriptor.nonSourceAnnotations) {
proto.addExtension(protocol.propertyAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.propertyAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
for (annotation in descriptor.getter?.nonSourceAnnotations.orEmpty()) {
proto.addExtension(protocol.propertyGetterAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.propertyGetterAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
for (annotation in descriptor.setter?.nonSourceAnnotations.orEmpty()) {
proto.addExtension(protocol.propertySetterAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.propertySetterAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
val constantInitializer = descriptor.compileTimeInitializer ?: return
if (constantInitializer !is NullValue) {
@@ -86,25 +87,25 @@ abstract class KotlinSerializerExtensionBase(private val protocol: SerializerExt
override fun serializeEnumEntry(descriptor: ClassDescriptor, proto: ProtoBuf.EnumEntry.Builder) {
for (annotation in descriptor.nonSourceAnnotations) {
proto.addExtension(protocol.enumEntryAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.enumEntryAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
}
override fun serializeValueParameter(descriptor: ValueParameterDescriptor, proto: ProtoBuf.ValueParameter.Builder) {
for (annotation in descriptor.nonSourceAnnotations) {
proto.addExtension(protocol.parameterAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.parameterAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
}
override fun serializeType(type: KotlinType, proto: ProtoBuf.Type.Builder) {
for (annotation in type.nonSourceAnnotations) {
proto.addExtension(protocol.typeAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.typeAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
}
override fun serializeTypeParameter(typeParameter: TypeParameterDescriptor, proto: ProtoBuf.TypeParameter.Builder) {
for (annotation in typeParameter.nonSourceAnnotations) {
proto.addExtension(protocol.typeParameterAnnotation, annotationSerializer.serializeAnnotation(annotation))
proto.addExtensionOrNull(protocol.typeParameterAnnotation, annotationSerializer.serializeAnnotation(annotation))
}
}
@@ -113,4 +114,17 @@ abstract class KotlinSerializerExtensionBase(private val protocol: SerializerExt
// (this requires more extensive protobuf scheme modifications)
}
@Suppress("Reformat")
private fun <
MessageType : GeneratedMessageLite.ExtendableMessage<MessageType>,
BuilderType : GeneratedMessageLite.ExtendableBuilder<MessageType, BuilderType>,
Type
> GeneratedMessageLite.ExtendableBuilder<MessageType, BuilderType>.addExtensionOrNull(
extension: GeneratedMessageLite.GeneratedExtension<MessageType, List<Type>>,
value: Type?
) {
if (value != null) {
addExtension(extension, value)
}
}
}