Do not serialize SOURCE-retained annotations
Also, fix the value of "hasAnnotations" flag to reflect if there are any _non-source_ annotations on a declaration. Unfortunately, after this change IncrementalJsCompilerRunnerTestGenerated$PureKotlin.testAnnotations starts to fail because of the following problem. The problem is that annotations on property accessors are not serialized yet on JS (see KT-14529), yet property proto message has setterFlags field which has the hasAnnotations flag. Upon the full rebuild of the code in that test, we correctly write hasAnnotations = true, but annotations themselves are not serialized. After an incremental build, we deserialize property setter descriptor, observe its Annotations object which happens to be an instance of NonEmptyDeserializedAnnotationsWithPossibleTargets. Now, because annotations itself are not serialized, that Annotations object has no annotations, yet its isEmpty always returns false (see the code). Everything worked correctly before the change because in DescriptorSerializer.hasAnnotations, we used Annotations.isEmpty and the result was the same in the full rebuild and in the incremental scenario. But now we're actually loading annotations, to determine their retention, and that's why the setterFlags are becoming different here and the test fails #KT-23360 Fixed
This commit is contained in:
+10
-2
@@ -33,6 +33,8 @@ import org.jetbrains.kotlin.resolve.constants.EnumValue
|
||||
import org.jetbrains.kotlin.resolve.constants.IntValue
|
||||
import org.jetbrains.kotlin.resolve.constants.NullValue
|
||||
import org.jetbrains.kotlin.resolve.constants.StringValue
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isSourceAnnotation
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.nonSourceAnnotations
|
||||
import org.jetbrains.kotlin.serialization.deserialization.ProtoEnumFlags
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.contains
|
||||
@@ -396,7 +398,9 @@ class DescriptorSerializer private constructor(
|
||||
builder.versionRequirement = requirement
|
||||
}
|
||||
|
||||
builder.addAllAnnotation(descriptor.annotations.map { extension.annotationSerializer.serializeAnnotation(it) })
|
||||
for (annotation in descriptor.nonSourceAnnotations) {
|
||||
builder.addAnnotation(extension.annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
|
||||
return builder
|
||||
}
|
||||
@@ -775,7 +779,11 @@ class DescriptorSerializer private constructor(
|
||||
Variance.OUT_VARIANCE -> ProtoBuf.Type.Argument.Projection.OUT
|
||||
}
|
||||
|
||||
private fun hasAnnotations(descriptor: Annotated): Boolean = !descriptor.annotations.isEmpty()
|
||||
private fun hasAnnotations(descriptor: Annotated): Boolean {
|
||||
// Sadly, we can't just return `descriptor.nonSourceAnnotations.isNotEmpty()` because that would not
|
||||
// consider use-site-targeted annotations
|
||||
return descriptor.annotations.getAllAnnotations().filterNot { it.annotation.isSourceAnnotation }.isNotEmpty()
|
||||
}
|
||||
|
||||
fun <T : DeclarationDescriptor> sort(descriptors: Collection<T>): List<T> =
|
||||
ArrayList(descriptors).apply {
|
||||
|
||||
+9
-8
@@ -20,13 +20,14 @@ import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.metadata.ProtoBuf
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.resolve.constants.NullValue
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.nonSourceAnnotations
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
open class KotlinSerializerExtensionBase(private val protocol: SerializerExtensionProtocol) : SerializerExtension() {
|
||||
override val stringTable = StringTableImpl()
|
||||
|
||||
override fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder) {
|
||||
for (annotation in descriptor.annotations) {
|
||||
for (annotation in descriptor.nonSourceAnnotations) {
|
||||
proto.addExtension(protocol.classAnnotation, annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
}
|
||||
@@ -36,19 +37,19 @@ open class KotlinSerializerExtensionBase(private val protocol: SerializerExtensi
|
||||
}
|
||||
|
||||
override fun serializeConstructor(descriptor: ConstructorDescriptor, proto: ProtoBuf.Constructor.Builder) {
|
||||
for (annotation in descriptor.annotations) {
|
||||
for (annotation in descriptor.nonSourceAnnotations) {
|
||||
proto.addExtension(protocol.constructorAnnotation, annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
}
|
||||
|
||||
override fun serializeFunction(descriptor: FunctionDescriptor, proto: ProtoBuf.Function.Builder) {
|
||||
for (annotation in descriptor.annotations) {
|
||||
for (annotation in descriptor.nonSourceAnnotations) {
|
||||
proto.addExtension(protocol.functionAnnotation, annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
}
|
||||
|
||||
override fun serializeProperty(descriptor: PropertyDescriptor, proto: ProtoBuf.Property.Builder) {
|
||||
for (annotation in descriptor.annotations) {
|
||||
for (annotation in descriptor.nonSourceAnnotations) {
|
||||
proto.addExtension(protocol.propertyAnnotation, annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
val constantInitializer = descriptor.compileTimeInitializer ?: return
|
||||
@@ -58,25 +59,25 @@ open class KotlinSerializerExtensionBase(private val protocol: SerializerExtensi
|
||||
}
|
||||
|
||||
override fun serializeEnumEntry(descriptor: ClassDescriptor, proto: ProtoBuf.EnumEntry.Builder) {
|
||||
for (annotation in descriptor.annotations) {
|
||||
for (annotation in descriptor.nonSourceAnnotations) {
|
||||
proto.addExtension(protocol.enumEntryAnnotation, annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
}
|
||||
|
||||
override fun serializeValueParameter(descriptor: ValueParameterDescriptor, proto: ProtoBuf.ValueParameter.Builder) {
|
||||
for (annotation in descriptor.annotations) {
|
||||
for (annotation in descriptor.nonSourceAnnotations) {
|
||||
proto.addExtension(protocol.parameterAnnotation, annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
}
|
||||
|
||||
override fun serializeType(type: KotlinType, proto: ProtoBuf.Type.Builder) {
|
||||
for (annotation in type.annotations) {
|
||||
for (annotation in type.nonSourceAnnotations) {
|
||||
proto.addExtension(protocol.typeAnnotation, annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
}
|
||||
|
||||
override fun serializeTypeParameter(typeParameter: TypeParameterDescriptor, proto: ProtoBuf.TypeParameter.Builder) {
|
||||
for (annotation in typeParameter.annotations) {
|
||||
for (annotation in typeParameter.nonSourceAnnotations) {
|
||||
proto.addExtension(protocol.typeParameterAnnotation, annotationSerializer.serializeAnnotation(annotation))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user