Skip to content

Commit 35162db

Browse files
udalovSpace Team
authored andcommitted
kotlinx-metadata: remove builtinsAnnotations
Similarly to klib-specific annotations, use the common annotations fields added in KT-57919. Also fix the missing logic for reading/writing property accessor annotations. Note that kotlinp output for optional annotations in case of K1 has changed. This is because optional annotations are not normal classes, their metadata is stored inside the JVM module metadata. So when JvmKotlinp rendered them, it tried to lookup annotations according to JVM metadata rules, which is only in the new place (the `KmClass.annotations` field). However, K1 writes them only to the old place (builtins annotations extension), so JvmKotlinp could not find any annotations, in particular `@OptionalExpectation`. Now all annotations are always present in `KmClass.annotations` and there are no platform-specific ways to load annotations, so `@OptionalExpectation` is found and rendered.
1 parent f2dfcb6 commit 35162db

File tree

8 files changed

+41
-84
lines changed

8 files changed

+41
-84
lines changed

libraries/kotlinx-metadata/src/kotlin/metadata/internal/common/BuiltInExtensionNodes.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ internal val KmType.builtins: BuiltInTypeExtension
3535
get() = getExtension(BuiltInTypeExtension.TYPE) as BuiltInTypeExtension
3636

3737
internal class BuiltInClassExtension : KmClassExtension {
38-
val annotations: MutableList<KmAnnotation> = mutableListOf()
39-
4038
override val type: KmExtensionType
4139
get() = TYPE
4240

@@ -57,8 +55,6 @@ internal class BuiltInPackageExtension : KmPackageExtension {
5755
}
5856

5957
internal class BuiltInFunctionExtension : KmFunctionExtension {
60-
val annotations: MutableList<KmAnnotation> = mutableListOf()
61-
6258
override val type: KmExtensionType
6359
get() = TYPE
6460

@@ -68,9 +64,6 @@ internal class BuiltInFunctionExtension : KmFunctionExtension {
6864
}
6965

7066
internal class BuiltInPropertyExtension : KmPropertyExtension {
71-
val annotations: MutableList<KmAnnotation> = mutableListOf()
72-
val getterAnnotations: MutableList<KmAnnotation> = mutableListOf()
73-
val setterAnnotations: MutableList<KmAnnotation> = mutableListOf()
7467
var compileTimeValue: KmAnnotationArgument? = null
7568

7669
override val type: KmExtensionType
@@ -82,8 +75,6 @@ internal class BuiltInPropertyExtension : KmPropertyExtension {
8275
}
8376

8477
internal class BuiltInConstructorExtension : KmConstructorExtension {
85-
val annotations: MutableList<KmAnnotation> = mutableListOf()
86-
8778
override val type: KmExtensionType
8879
get() = TYPE
8980

@@ -93,8 +84,6 @@ internal class BuiltInConstructorExtension : KmConstructorExtension {
9384
}
9485

9586
internal class BuiltInValueParameterExtension : KmValueParameterExtension {
96-
val annotations: MutableList<KmAnnotation> = mutableListOf()
97-
9887
override val type: KmExtensionType
9988
get() = TYPE
10089

libraries/kotlinx-metadata/src/kotlin/metadata/internal/common/BuiltInMetadataExtensions.kt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import kotlin.metadata.internal.writeAnnotation
1818

1919
public class BuiltInsMetadataExtensions : MetadataExtensions {
2020
override fun readClassExtensions(kmClass: KmClass, proto: ProtoBuf.Class, c: ReadContext) {
21-
val ext = kmClass.builtins
2221
proto.getExtension(BuiltInsProtoBuf.classAnnotation).forEach { annotation ->
23-
ext.annotations.add(annotation.readAnnotation(c.strings))
22+
kmClass.annotations.add(annotation.readAnnotation(c.strings))
2423
}
2524
}
2625

@@ -35,23 +34,28 @@ public class BuiltInsMetadataExtensions : MetadataExtensions {
3534
}
3635

3736
override fun readFunctionExtensions(kmFunction: KmFunction, proto: ProtoBuf.Function, c: ReadContext) {
38-
val ext = kmFunction.builtins
3937
proto.getExtension(BuiltInsProtoBuf.functionAnnotation).forEach { annotation ->
40-
ext.annotations.add(annotation.readAnnotation(c.strings))
38+
kmFunction.annotations.add(annotation.readAnnotation(c.strings))
4139
}
4240
}
4341

4442
override fun readPropertyExtensions(kmProperty: KmProperty, proto: ProtoBuf.Property, c: ReadContext) {
45-
val ext = kmProperty.builtins
4643
proto.getExtension(BuiltInsProtoBuf.propertyAnnotation).forEach { annotation ->
47-
ext.annotations.add(annotation.readAnnotation(c.strings))
44+
kmProperty.annotations.add(annotation.readAnnotation(c.strings))
45+
}
46+
proto.getExtension(BuiltInsProtoBuf.propertyGetterAnnotation).forEach { annotation ->
47+
kmProperty.getter.annotations.add(annotation.readAnnotation(c.strings))
48+
}
49+
kmProperty.setter?.let { setter ->
50+
proto.getExtension(BuiltInsProtoBuf.propertySetterAnnotation).forEach { annotation ->
51+
setter.annotations.add(annotation.readAnnotation(c.strings))
52+
}
4853
}
4954
}
5055

5156
override fun readConstructorExtensions(kmConstructor: KmConstructor, proto: ProtoBuf.Constructor, c: ReadContext) {
52-
val ext = kmConstructor.builtins
5357
proto.getExtension(BuiltInsProtoBuf.constructorAnnotation).forEach { annotation ->
54-
ext.annotations.add(annotation.readAnnotation(c.strings))
58+
kmConstructor.annotations.add(annotation.readAnnotation(c.strings))
5559
}
5660
}
5761

@@ -73,14 +77,13 @@ public class BuiltInsMetadataExtensions : MetadataExtensions {
7377
}
7478

7579
override fun readValueParameterExtensions(kmValueParameter: KmValueParameter, proto: ProtoBuf.ValueParameter, c: ReadContext) {
76-
val ext = kmValueParameter.builtins
7780
proto.getExtension(BuiltInsProtoBuf.parameterAnnotation).forEach { annotation ->
78-
ext.annotations.add(annotation.readAnnotation(c.strings))
81+
kmValueParameter.annotations.add(annotation.readAnnotation(c.strings))
7982
}
8083
}
8184

8285
override fun writeClassExtensions(kmClass: KmClass, proto: ProtoBuf.Class.Builder, c: WriteContext) {
83-
for (annotation in extension { kmClass.builtinsAnnotations }) {
86+
for (annotation in kmClass.annotations) {
8487
proto.addExtension(BuiltInsProtoBuf.classAnnotation, annotation.writeAnnotation(c.strings).build())
8588
}
8689
}
@@ -97,19 +100,25 @@ public class BuiltInsMetadataExtensions : MetadataExtensions {
97100
}
98101

99102
override fun writeFunctionExtensions(kmFunction: KmFunction, proto: ProtoBuf.Function.Builder, c: WriteContext) {
100-
for (annotation in extension { kmFunction.builtinsAnnotations }) {
103+
for (annotation in kmFunction.annotations) {
101104
proto.addExtension(BuiltInsProtoBuf.functionAnnotation, annotation.writeAnnotation(c.strings).build())
102105
}
103106
}
104107

105108
override fun writePropertyExtensions(kmProperty: KmProperty, proto: ProtoBuf.Property.Builder, c: WriteContext) {
106-
for (annotation in extension { kmProperty.builtinsAnnotations }) {
109+
for (annotation in kmProperty.annotations) {
107110
proto.addExtension(BuiltInsProtoBuf.propertyAnnotation, annotation.writeAnnotation(c.strings).build())
108111
}
112+
for (annotation in kmProperty.getter.annotations) {
113+
proto.addExtension(BuiltInsProtoBuf.propertyGetterAnnotation, annotation.writeAnnotation(c.strings).build())
114+
}
115+
for (annotation in kmProperty.setter?.annotations.orEmpty()) {
116+
proto.addExtension(BuiltInsProtoBuf.propertySetterAnnotation, annotation.writeAnnotation(c.strings).build())
117+
}
109118
}
110119

111120
override fun writeConstructorExtensions(kmConstructor: KmConstructor, proto: ProtoBuf.Constructor.Builder, c: WriteContext) {
112-
for (annotation in extension { kmConstructor.builtinsAnnotations }) {
121+
for (annotation in kmConstructor.annotations) {
113122
proto.addExtension(BuiltInsProtoBuf.constructorAnnotation, annotation.writeAnnotation(c.strings).build())
114123
}
115124
}
@@ -130,7 +139,7 @@ public class BuiltInsMetadataExtensions : MetadataExtensions {
130139
}
131140

132141
override fun writeValueParameterExtensions(valueParameter: KmValueParameter, proto: ProtoBuf.ValueParameter.Builder, c: WriteContext) {
133-
for (annotation in extension { valueParameter.builtinsAnnotations }) {
142+
for (annotation in valueParameter.annotations) {
134143
proto.addExtension(BuiltInsProtoBuf.parameterAnnotation, annotation.writeAnnotation(c.strings).build())
135144
}
136145
}

libraries/kotlinx-metadata/src/kotlin/metadata/internal/common/builtInsExtensions.kt

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,18 @@ import kotlin.metadata.*
1111
* This object serves as an internal accessor for extensions that are available only when metadata is loaded from .kotlin_builtins file.
1212
*/
1313
public object BuiltInExtensionsAccessor {
14-
public val KmClass.builtinsAnnotations: MutableList<KmAnnotation>
15-
get() = builtins.annotations
16-
1714
public var KmPackage.fqName: String?
1815
get() = builtins.fqName
1916
set(value) {
2017
builtins.fqName = value
2118
}
2219

23-
public val KmFunction.builtinsAnnotations: MutableList<KmAnnotation>
24-
get() = builtins.annotations
25-
26-
public val KmProperty.builtinsAnnotations: MutableList<KmAnnotation>
27-
get() = builtins.annotations
28-
29-
public val KmProperty.builtinsSetterAnnotations: MutableList<KmAnnotation>
30-
get() = builtins.setterAnnotations
31-
32-
public val KmProperty.builtinsGetterAnnotations: MutableList<KmAnnotation>
33-
get() = builtins.getterAnnotations
34-
3520
public var KmProperty.compileTimeValue: KmAnnotationArgument?
3621
get() = builtins.compileTimeValue
3722
set(value) {
3823
builtins.compileTimeValue = value
3924
}
4025

41-
public val KmConstructor.builtinsAnnotations: MutableList<KmAnnotation>
42-
get() = builtins.annotations
43-
44-
public val KmValueParameter.builtinsAnnotations: MutableList<KmAnnotation>
45-
get() = builtins.annotations
46-
4726
public val KmTypeParameter.annotations: MutableList<KmAnnotation>
4827
get() = builtins.annotations
4928

libraries/tools/kotlinp/jvm/src/org/jetbrains/kotlin/kotlinp/jvm/JvmKotlinp.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
package org.jetbrains.kotlin.kotlinp.jvm
77

8-
import org.jetbrains.kotlin.kotlinp.*
8+
import org.jetbrains.kotlin.kotlinp.Kotlinp
9+
import org.jetbrains.kotlin.kotlinp.Printer
10+
import org.jetbrains.kotlin.kotlinp.Settings
11+
import org.jetbrains.kotlin.kotlinp.printString
912
import kotlin.metadata.*
1013
import kotlin.metadata.jvm.*
1114

@@ -87,15 +90,8 @@ class JvmKotlinp(settings: Settings) : Kotlinp(settings) {
8790
appendLine("}")
8891
}
8992

90-
override fun getAnnotations(clazz: KmClass): List<KmAnnotation> = clazz.annotations
91-
override fun getAnnotations(constructor: KmConstructor): List<KmAnnotation> = constructor.annotations
92-
override fun getAnnotations(function: KmFunction): List<KmAnnotation> = function.annotations
93-
override fun getAnnotations(property: KmProperty): List<KmAnnotation> = property.annotations
94-
override fun getGetterAnnotations(property: KmProperty): List<KmAnnotation> = property.getter.annotations
95-
override fun getSetterAnnotations(property: KmProperty): List<KmAnnotation> = property.setter!!.annotations
9693
override fun getAnnotations(typeParameter: KmTypeParameter): List<KmAnnotation> = typeParameter.annotations
9794
override fun getAnnotations(type: KmType): List<KmAnnotation> = type.annotations
98-
override fun getAnnotations(valueParameter: KmValueParameter): List<KmAnnotation> = valueParameter.annotations
9995

10096
override fun sortConstructors(constructors: List<KmConstructor>) = constructors.sortedBy { it.signature.toString() }
10197
override fun sortFunctions(functions: List<KmFunction>) = functions.sortedBy { it.signature.toString() }

libraries/tools/kotlinp/jvm/testData/OptionalAnnotation.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module {
2525

2626
// Optional annotations
2727

28+
@kotlin/OptionalExpectation
2829
public final expect annotation class test/A : kotlin/Annotation {
2930

3031
// signature: <init>(I)V
@@ -35,6 +36,7 @@ module {
3536

3637
// module name: main
3738
}
39+
@kotlin/OptionalExpectation
3840
public final expect annotation class test/B : kotlin/Annotation {
3941

4042
// signature: <init>(Lkotlin/Array;)V
@@ -45,6 +47,7 @@ module {
4547

4648
// module name: main
4749
}
50+
@kotlin/OptionalExpectation
4851
public final expect annotation class test/C : kotlin/Annotation {
4952

5053
// signature: <init>()V

libraries/tools/kotlinp/klib/src/org/jetbrains/kotlin/kotlinp/klib/KlibKotlinp.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,8 @@ class KlibKotlinp(
4444
appendLine("}")
4545
}
4646

47-
override fun getAnnotations(clazz: KmClass): List<KmAnnotation> = clazz.annotations
48-
override fun getAnnotations(constructor: KmConstructor): List<KmAnnotation> = constructor.annotations
49-
override fun getAnnotations(function: KmFunction): List<KmAnnotation> = function.annotations
50-
override fun getAnnotations(property: KmProperty): List<KmAnnotation> = property.annotations
51-
override fun getGetterAnnotations(property: KmProperty): List<KmAnnotation> = property.getter.annotations
52-
override fun getSetterAnnotations(property: KmProperty): List<KmAnnotation> = property.setter?.annotations.orEmpty()
5347
override fun getAnnotations(typeParameter: KmTypeParameter): List<KmAnnotation> = typeParameter.annotations
5448
override fun getAnnotations(type: KmType): List<KmAnnotation> = type.annotations
55-
override fun getAnnotations(valueParameter: KmValueParameter): List<KmAnnotation> = valueParameter.annotations
5649

5750
override fun Printer.appendSignatures(clazz: KmClass) = appendSignature { classSignature(clazz) }
5851
override fun Printer.appendSignatures(constructor: KmConstructor) = appendSignature { constructorSignature(constructor) }

libraries/tools/kotlinp/src/org/jetbrains/kotlin/kotlinp/BuiltInsKotlinp.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
package org.jetbrains.kotlin.kotlinp
77

88
import kotlin.metadata.*
9-
import kotlin.metadata.internal.common.*
9+
import kotlin.metadata.internal.common.BuiltInExtensionsAccessor
10+
import kotlin.metadata.internal.common.KmModuleFragment
11+
import kotlin.metadata.internal.common.KotlinCommonMetadata
1012

1113
class BuiltInsKotlinp(settings: Settings) : Kotlinp(settings) {
1214
fun printBuiltInsFile(metadata: KotlinCommonMetadata?): String = printString {
@@ -26,15 +28,8 @@ class BuiltInsKotlinp(settings: Settings) : Kotlinp(settings) {
2628
}
2729
}
2830

29-
override fun getAnnotations(clazz: KmClass) = extension { clazz.builtinsAnnotations }
30-
override fun getAnnotations(constructor: KmConstructor) = extension { constructor.builtinsAnnotations }
31-
override fun getAnnotations(function: KmFunction) = extension { function.builtinsAnnotations }
32-
override fun getAnnotations(property: KmProperty) = extension { property.builtinsAnnotations }
33-
override fun getGetterAnnotations(property: KmProperty) = extension { property.builtinsGetterAnnotations }
34-
override fun getSetterAnnotations(property: KmProperty) = extension { property.builtinsSetterAnnotations }
3531
override fun getAnnotations(typeParameter: KmTypeParameter) = extension { typeParameter.annotations }
3632
override fun getAnnotations(type: KmType) = extension { type.annotations }
37-
override fun getAnnotations(valueParameter: KmValueParameter) = extension { valueParameter.builtinsAnnotations }
3833

3934
override fun sortConstructors(constructors: List<KmConstructor>): List<KmConstructor> =
4035
constructors.sortedBy { render(it, ::renderConstructor) }

libraries/tools/kotlinp/src/org/jetbrains/kotlin/kotlinp/Kotlinp.kt

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ abstract class Kotlinp(protected val settings: Settings) {
7777
appendOrigin(clazz)
7878
appendVersionRequirements(clazz.versionRequirements)
7979
appendSignatures(clazz)
80-
appendAnnotations(getAnnotations(clazz))
80+
appendAnnotations(clazz.annotations)
8181
appendContextReceiverTypes(clazz.contextReceiverTypes)
8282
append(VISIBILITY_MAP[clazz.visibility])
8383
append(MODALITY_MAP[clazz.modality])
@@ -137,7 +137,7 @@ abstract class Kotlinp(protected val settings: Settings) {
137137
appendLine()
138138
appendVersionRequirements(constructor.versionRequirements)
139139
appendSignatures(constructor)
140-
appendAnnotations(getAnnotations(constructor))
140+
appendAnnotations(constructor.annotations)
141141
renderConstructorModifiers(constructor, printer)
142142
append("constructor")
143143
appendValueParameters(constructor.valueParameters)
@@ -158,7 +158,7 @@ abstract class Kotlinp(protected val settings: Settings) {
158158
appendOrigin(function)
159159
appendVersionRequirements(function.versionRequirements)
160160
appendSignatures(function)
161-
appendAnnotations(getAnnotations(function))
161+
appendAnnotations(function.annotations)
162162
appendContextReceiverTypes(function.contextReceiverTypes)
163163
renderFunctionModifiers(function, printer)
164164
append("fun ")
@@ -292,7 +292,7 @@ abstract class Kotlinp(protected val settings: Settings) {
292292
appendVersionRequirements(property.versionRequirements)
293293
appendSignatures(property)
294294
appendCustomAttributes(property)
295-
appendAnnotations(getAnnotations(property))
295+
appendAnnotations(property.annotations)
296296
appendContextReceiverTypes(property.contextReceiverTypes)
297297
renderPropertyModifiers(property, printer)
298298
append(if (property.isVar) "var " else "val ")
@@ -306,12 +306,12 @@ abstract class Kotlinp(protected val settings: Settings) {
306306
appendLine()
307307
withIndent {
308308
appendGetterSignatures(property)
309-
appendAnnotations(getGetterAnnotations(property))
309+
appendAnnotations(property.getter.annotations)
310310
renderPropertyAccessorModifiers(property.getter, printer)
311311
appendLine("get")
312312
property.setter?.let { setter ->
313313
appendSetterSignatures(property)
314-
appendAnnotations(getSetterAnnotations(property))
314+
appendAnnotations(setter.annotations)
315315
renderPropertyAccessorModifiers(setter, printer)
316316
append("set")
317317
property.setterParameter?.let {
@@ -440,7 +440,7 @@ abstract class Kotlinp(protected val settings: Settings) {
440440
}
441441

442442
fun renderValueParameter(valueParameter: KmValueParameter, printer: Printer): Unit = with(printer) {
443-
appendAnnotations(getAnnotations(valueParameter), onePerLine = false)
443+
appendAnnotations(valueParameter.annotations, onePerLine = false)
444444
appendFlags(
445445
valueParameter.isCrossinline to "crossinline",
446446
valueParameter.isNoinline to "noinline"
@@ -527,15 +527,8 @@ abstract class Kotlinp(protected val settings: Settings) {
527527
)
528528
}
529529

530-
protected abstract fun getAnnotations(clazz: KmClass): List<KmAnnotation>
531-
protected abstract fun getAnnotations(constructor: KmConstructor): List<KmAnnotation>
532-
protected abstract fun getAnnotations(function: KmFunction): List<KmAnnotation>
533-
protected abstract fun getAnnotations(property: KmProperty): List<KmAnnotation>
534-
protected abstract fun getGetterAnnotations(property: KmProperty): List<KmAnnotation>
535-
protected abstract fun getSetterAnnotations(property: KmProperty): List<KmAnnotation>
536530
protected abstract fun getAnnotations(typeParameter: KmTypeParameter): List<KmAnnotation>
537531
protected abstract fun getAnnotations(type: KmType): List<KmAnnotation>
538-
protected abstract fun getAnnotations(valueParameter: KmValueParameter): List<KmAnnotation>
539532

540533
protected open fun sortConstructors(constructors: List<KmConstructor>): List<KmConstructor> = constructors
541534
protected open fun sortFunctions(functions: List<KmFunction>): List<KmFunction> = functions

0 commit comments

Comments
 (0)