Files
kotlin-fork/compiler/testData/codegen/box/annotations/instances/multimoduleCreation.kt
T
Leonid Startsev c4255f9a9e [K2] Instantiation of annotations having default values for properties
This commit adds missing pieces for the puzzle:

Annotation instantiation feature uses IrProperty's initializer to instantiate
properties from other modules that have default values which weren't
specified on call site.

To support this feature properly, Fir2IrVisitor should fill LazyIrProperty's
backing field initializer with information from Fir.

To get this information into Fir, FirMemberDeserializer should be able to read
it from KotlinJvmBinaryClass with AnnotationLoaderVisitorImpl. (klibs are unsupported for now)

There's a catch with enum entries references: we can't access session.SymbolProvider to resolve it
because we're still at the deserialization stage, and it can cause StackOverflow if enum is nested in the
same class (see RequiresOptIn.Level). To mitigate this, a new FirEnumEntryDeserializedAccessExpression is produced
instead; it is later replaced with the correct reference in the Fir2IrVisitor.

^KT-58137 Fixed

Also add test to loadJava folder with annotations default values that
verifies metadata loading
2023-05-12 16:08:02 +00:00

75 lines
2.2 KiB
Kotlin
Vendored

// TARGET_BACKEND: JVM_IR
// IGNORE_DEXING
// WITH_STDLIB
// !LANGUAGE: +InstantiationOfAnnotationClasses
// MODULE: lib
// FILE: lib.kt
package a
import kotlin.reflect.KClass
annotation class A(val kClass: KClass<*> = Int::class)
annotation class OtherArrays(
val doublesArray: DoubleArray = [],
val enumArray: Array<kotlin.text.RegexOption> = [],
val annotationsArray: Array<JvmStatic> = [],
val namesArray: Array<JvmName> = [JvmName("foo")]
)
annotation class UnsignedValue(
val uint: UInt = 2147483657U // Int.MAX_VALUE + 10
)
annotation class Outer(
val array: Array<Inner> = [Inner(1), Inner(2)]
) {
annotation class Inner(val v: Int = 0)
}
// MODULE: app(lib)
// FILE: app.kt
// kotlin.Metadata: IntArray, Array<String>
// kotlin.Deprecated: Nested annotation, enum instance
// a.A: KClass
// a.OtherArrays: Arrays of enums and other annotations
package test
import a.*
import kotlin.test.*
class C {
fun one(): A = A()
fun two(): Metadata = Metadata()
fun three(): Deprecated = Deprecated("foo")
fun four(): OtherArrays = OtherArrays()
fun five(): UnsignedValue = UnsignedValue()
fun six(): Outer = Outer()
}
fun box(): String {
val a = C().one()
assertEquals(Int::class, a.kClass)
assertEquals(
"""@kotlin.Metadata(bytecodeVersion=[1, 0, 3], data1=[], data2=[], extraInt=0, extraString=, kind=1, metadataVersion=[], packageName=)""",
C().two().toString()
)
assertEquals(
"""@kotlin.Deprecated(level=WARNING, message=foo, replaceWith=@kotlin.ReplaceWith(expression=, imports=[]))""",
C().three().toString()
)
val otherArraysStr = C().four().toString()
// K1 and K2 have different properties order after metadata deserialization
assertTrue(
otherArraysStr == """@a.OtherArrays(doublesArray=[], enumArray=[], annotationsArray=[], namesArray=[@kotlin.jvm.JvmName(name=foo)])""" ||
otherArraysStr == """@a.OtherArrays(annotationsArray=[], doublesArray=[], enumArray=[], namesArray=[@kotlin.jvm.JvmName(name=foo)])"""
)
assertEquals(Int.MAX_VALUE.toUInt() + 10.toUInt(), C().five().uint)
assertEquals("""@a.Outer(array=[@a.Outer.Inner(v=1), @a.Outer.Inner(v=2)])""", C().six().toString())
return "OK"
}