[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
This commit is contained in:
Leonid Startsev
2023-04-24 18:32:13 +02:00
committed by Space Team
parent d757847ed6
commit c4255f9a9e
34 changed files with 797 additions and 157 deletions
@@ -0,0 +1,73 @@
public final annotation class A : R|kotlin/Annotation| {
public final val aI: R|kotlin/IntArray| = <implicitArrayOf>(Int(1), Int(2))
public get(): R|kotlin/IntArray|
public final val aS: R|kotlin/Array<kotlin/String>| = <implicitArrayOf>(String(a), String(b))
public get(): R|kotlin/Array<kotlin/String>|
public final val anno: R|test/Empty| = @R|test/Empty|()
public get(): R|test/Empty|
public final val e: R|test/E| = test/E.E0
public get(): R|test/E|
public final val i: R|kotlin/Int| = Int(42)
public get(): R|kotlin/Int|
public final val kClass: R|kotlin/reflect/KClass<*>| = <getClass>(<getClass>(R|kotlin/Int|))
public get(): R|kotlin/reflect/KClass<*>|
public final val kClassArray: R|kotlin/Array<kotlin/reflect/KClass<*>>| = <implicitArrayOf>(<getClass>(<getClass>(R|test/A|)))
public get(): R|kotlin/Array<kotlin/reflect/KClass<*>>|
public final val s: R|kotlin/String| = String(foo)
public get(): R|kotlin/String|
public constructor(i: R|kotlin/Int| = STUB, s: R|kotlin/String| = STUB, kClass: R|kotlin/reflect/KClass<*>| = STUB, kClassArray: R|kotlin/Array<kotlin/reflect/KClass<*>>| = STUB, e: R|test/E| = STUB, anno: R|test/Empty| = STUB, aS: R|kotlin/Array<kotlin/String>| = STUB, aI: R|kotlin/IntArray| = STUB): R|test/A|
}
public final enum class E : R|kotlin/Enum<test/E>| {
private constructor(): R|test/E|
public final static enum entry E0: R|test/E|
public final static fun values(): R|kotlin/Array<test/E>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|test/E| {
}
public final static val entries: R|kotlin/enums/EnumEntries<test/E>|
public get(): R|kotlin/enums/EnumEntries<test/E>|
}
public final annotation class Empty : R|kotlin/Annotation| {
public constructor(): R|test/Empty|
}
public final annotation class OtherArrays : R|kotlin/Annotation| {
public final val annotationsArray: R|kotlin/Array<kotlin/jvm/JvmStatic>| = <implicitArrayOf>()
public get(): R|kotlin/Array<kotlin/jvm/JvmStatic>|
public final val doublesArray: R|kotlin/DoubleArray| = <implicitArrayOf>(Double(1.5))
public get(): R|kotlin/DoubleArray|
public final val enumArray: R|kotlin/Array<kotlin/text/RegexOption>| = <implicitArrayOf>(kotlin/text/RegexOption.IGNORE_CASE)
public get(): R|kotlin/Array<kotlin/text/RegexOption>|
public final val namesArray: R|kotlin/Array<kotlin/jvm/JvmName>| = <implicitArrayOf>(@R|kotlin/jvm/JvmName|(name = String(foo)) )
public get(): R|kotlin/Array<kotlin/jvm/JvmName>|
public constructor(doublesArray: R|kotlin/DoubleArray| = STUB, enumArray: R|kotlin/Array<kotlin/text/RegexOption>| = STUB, annotationsArray: R|kotlin/Array<kotlin/jvm/JvmStatic>| = STUB, namesArray: R|kotlin/Array<kotlin/jvm/JvmName>| = STUB): R|test/OtherArrays|
}
public final annotation class UnsignedValue : R|kotlin/Annotation| {
public final val uint: R|kotlin/UInt| = UInt(-2147483639)
public get(): R|kotlin/UInt|
public constructor(uint: R|kotlin/UInt| = STUB): R|test/UnsignedValue|
}
@@ -0,0 +1,73 @@
public final annotation class A : R|kotlin/Annotation| {
public final val i: R|kotlin/Int| = Int(42)
public get(): R|kotlin/Int|
public final val s: R|kotlin/String| = String(foo)
public get(): R|kotlin/String|
public final val kClass: R|kotlin/reflect/KClass<*>| = <getClass>(<getClass>(R|kotlin/Int|))
public get(): R|kotlin/reflect/KClass<*>|
public final val kClassArray: R|kotlin/Array<kotlin/reflect/KClass<*>>| = <implicitArrayOf>(<getClass>(<getClass>(R|test/A|)))
public get(): R|kotlin/Array<kotlin/reflect/KClass<*>>|
public final val e: R|test/E| = test/E.E0
public get(): R|test/E|
public final val anno: R|test/Empty| = @R|test/Empty|()
public get(): R|test/Empty|
public final val aS: R|kotlin/Array<kotlin/String>| = <implicitArrayOf>(String(a), String(b))
public get(): R|kotlin/Array<kotlin/String>|
public final val aI: R|kotlin/IntArray| = <implicitArrayOf>(Int(1), Int(2))
public get(): R|kotlin/IntArray|
public constructor(i: R|kotlin/Int| = STUB, s: R|kotlin/String| = STUB, kClass: R|kotlin/reflect/KClass<*>| = STUB, kClassArray: R|kotlin/Array<kotlin/reflect/KClass<*>>| = STUB, e: R|test/E| = STUB, anno: R|test/Empty| = STUB, aS: R|kotlin/Array<kotlin/String>| = STUB, aI: R|kotlin/IntArray| = STUB): R|test/A|
}
public final enum class E : R|kotlin/Enum<test/E>| {
private constructor(): R|test/E|
public final static enum entry E0: R|test/E|
public final static fun values(): R|kotlin/Array<test/E>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|test/E| {
}
public final static val entries: R|kotlin/enums/EnumEntries<test/E>|
public get(): R|kotlin/enums/EnumEntries<test/E>|
}
public final annotation class Empty : R|kotlin/Annotation| {
public constructor(): R|test/Empty|
}
public final annotation class OtherArrays : R|kotlin/Annotation| {
public final val doublesArray: R|kotlin/DoubleArray| = <implicitArrayOf>(Double(1.5))
public get(): R|kotlin/DoubleArray|
public final val enumArray: R|kotlin/Array<kotlin/text/RegexOption>| = <implicitArrayOf>(kotlin/text/RegexOption.IGNORE_CASE)
public get(): R|kotlin/Array<kotlin/text/RegexOption>|
public final val annotationsArray: R|kotlin/Array<kotlin/jvm/JvmStatic>| = <implicitArrayOf>()
public get(): R|kotlin/Array<kotlin/jvm/JvmStatic>|
public final val namesArray: R|kotlin/Array<kotlin/jvm/JvmName>| = <implicitArrayOf>(@R|kotlin/jvm/JvmName|(name = String(foo)) )
public get(): R|kotlin/Array<kotlin/jvm/JvmName>|
public constructor(doublesArray: R|kotlin/DoubleArray| = STUB, enumArray: R|kotlin/Array<kotlin/text/RegexOption>| = STUB, annotationsArray: R|kotlin/Array<kotlin/jvm/JvmStatic>| = STUB, namesArray: R|kotlin/Array<kotlin/jvm/JvmName>| = STUB): R|test/OtherArrays|
}
public final annotation class UnsignedValue : R|kotlin/Annotation| {
public final val uint: R|kotlin/UInt| = UInt(-2147483639)
public get(): R|kotlin/UInt|
public constructor(uint: R|kotlin/UInt| = STUB): R|test/UnsignedValue|
}
@@ -0,0 +1,31 @@
// NO_CHECK_SOURCE_VS_BINARY
//^ While compiling source with K1, we do not store annotation default values, but we load them when reading compiled files both in K1 and K2
// This test verifies exactly loading of default values
package test
import kotlin.reflect.KClass
enum class E { E0 }
annotation class Empty
annotation class A(
val i: Int = 42,
val s: String = "foo",
val kClass: KClass<*> = Int::class,
val kClassArray: Array<KClass<*>> = [A::class],
val e: E = E.E0,
val anno: Empty = Empty(),
val aS: Array<String> = arrayOf("a", "b"),
val aI: IntArray = intArrayOf(1, 2)
)
annotation class OtherArrays(
val doublesArray: DoubleArray = [1.5],
val enumArray: Array<kotlin.text.RegexOption> = [kotlin.text.RegexOption.IGNORE_CASE],
val annotationsArray: Array<JvmStatic> = [],
val namesArray: Array<JvmName> = [JvmName("foo")]
)
annotation class UnsignedValue(
val uint: UInt = 2147483657U // Int.MAX_VALUE + 10
)
@@ -0,0 +1,63 @@
package test
public final annotation class A : kotlin.Annotation {
/*primary*/ public constructor A(/*0*/ i: kotlin.Int = ..., /*1*/ s: kotlin.String = ..., /*2*/ kClass: kotlin.reflect.KClass<*> = ..., /*3*/ kClassArray: kotlin.Array<kotlin.reflect.KClass<*>> = ..., /*4*/ e: test.E = ..., /*5*/ anno: test.Empty = ..., /*6*/ aS: kotlin.Array<kotlin.String> = ..., /*7*/ aI: kotlin.IntArray = ...)
public final val aI: kotlin.IntArray = {1, 2}
public final fun `<get-aI>`(): kotlin.IntArray
public final val aS: kotlin.Array<kotlin.String> = {"a", "b"}
public final fun `<get-aS>`(): kotlin.Array<kotlin.String>
public final val anno: test.Empty = test.Empty
public final fun `<get-anno>`(): test.Empty
public final val e: test.E = E.E0
public final fun `<get-e>`(): test.E
public final val i: kotlin.Int = 42
public final fun `<get-i>`(): kotlin.Int
public final val kClass: kotlin.reflect.KClass<*> = kotlin.Int::class
public final fun `<get-kClass>`(): kotlin.reflect.KClass<*>
public final val kClassArray: kotlin.Array<kotlin.reflect.KClass<*>> = {test.A::class}
public final fun `<get-kClassArray>`(): kotlin.Array<kotlin.reflect.KClass<*>>
public final val s: kotlin.String = "foo"
public final fun `<get-s>`(): kotlin.String
}
public final enum class E : kotlin.Enum<test.E> {
enum entry E0
/*primary*/ private constructor E()
@kotlin.internal.IntrinsicConstEvaluation public final override /*1*/ /*fake_override*/ val name: kotlin.String
public final override /*1*/ /*fake_override*/ fun `<get-name>`(): kotlin.String
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
public final override /*1*/ /*fake_override*/ fun `<get-ordinal>`(): kotlin.Int
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: test.E): kotlin.Int
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<test.E!>!
// Static members
public final /*synthesized*/ val entries: kotlin.enums.EnumEntries<test.E>
public final /*synthesized*/ fun `<get-entries>`(): kotlin.enums.EnumEntries<test.E>
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): test.E
public final /*synthesized*/ fun values(): kotlin.Array<test.E>
}
public final annotation class Empty : kotlin.Annotation {
/*primary*/ public constructor Empty()
}
public final annotation class OtherArrays : kotlin.Annotation {
/*primary*/ public constructor OtherArrays(/*0*/ doublesArray: kotlin.DoubleArray = ..., /*1*/ enumArray: kotlin.Array<kotlin.text.RegexOption> = ..., /*2*/ annotationsArray: kotlin.Array<kotlin.jvm.JvmStatic> = ..., /*3*/ namesArray: kotlin.Array<kotlin.jvm.JvmName> = ...)
public final val annotationsArray: kotlin.Array<kotlin.jvm.JvmStatic> = {}
public final fun `<get-annotationsArray>`(): kotlin.Array<kotlin.jvm.JvmStatic>
public final val doublesArray: kotlin.DoubleArray = {1.5.toDouble()}
public final fun `<get-doublesArray>`(): kotlin.DoubleArray
public final val enumArray: kotlin.Array<kotlin.text.RegexOption> = {RegexOption.IGNORE_CASE}
public final fun `<get-enumArray>`(): kotlin.Array<kotlin.text.RegexOption>
public final val namesArray: kotlin.Array<kotlin.jvm.JvmName> = {kotlin.jvm.JvmName(name = "foo")}
public final fun `<get-namesArray>`(): kotlin.Array<kotlin.jvm.JvmName>
}
public final annotation class UnsignedValue : kotlin.Annotation {
/*primary*/ public constructor UnsignedValue(/*0*/ uint: kotlin.UInt = ...)
public final val uint: kotlin.UInt = -2147483639.toUInt()
public final fun `<get-uint>`(): kotlin.UInt
}
@@ -1,8 +1,8 @@
public final annotation class Anno : R|kotlin/Annotation| {
public final val value: R|kotlin/String|
public final val value: R|kotlin/String| = String(0)
public get(): R|kotlin/String|
public final val x: R|kotlin/Int|
public final val x: R|kotlin/Int| = Int(0)
public get(): R|kotlin/Int|
public constructor(value: R|kotlin/String| = STUB, x: R|kotlin/Int| = STUB): R|test/Anno|