[K/N] Fix for instantiating annotations from already lowered file

If annotation class is already lowered, it can have more than one
constructor. So we should use primary one, not any one.

^KT-53475
This commit is contained in:
Pavel Kunyavskiy
2022-08-08 12:00:09 +02:00
committed by Space
parent c6cbab43f7
commit 3424e756ad
10 changed files with 104 additions and 1 deletions
@@ -450,6 +450,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefault.kt");
}
@Test
@TestMetadata("annotationInstancesEmptyDefaultLowered.kt")
public void testAnnotationInstancesEmptyDefaultLowered() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefaultLowered.kt");
}
@Test
@TestMetadata("annotationJvmHashCode.kt")
public void testAnnotationJvmHashCode() throws Exception {
@@ -69,6 +69,7 @@ abstract class AnnotationImplementationTransformer(val context: BackendContext,
val constructedClass = expression.type.classOrNull?.owner ?: return super.visitConstructorCall(expression)
if (!constructedClass.isAnnotationClass) return super.visitConstructorCall(expression)
if (constructedClass.typeParameters.isNotEmpty()) return super.visitConstructorCall(expression) // Not supported yet
require(expression.symbol.owner.isPrimary) { "Non-primary constructors of annotations are not supported" }
val implClass = implementations.getOrPut(constructedClass) { createAnnotationImplementation(constructedClass) }
val ctor = chooseConstructor(implClass, expression)
@@ -0,0 +1,62 @@
// IGNORE_BACKEND: JVM
// IGNORE_BACKEND: WASM
// DONT_TARGET_EXACT_BACKEND: JS
// WITH_STDLIB
// !LANGUAGE: +InstantiationOfAnnotationClasses
/**
* This test checks if annotation instantiation works correctly, when annotation class is lowered before instantiation point.
* So, filename of classes containing annotations should be earlier, than for box function
*/
// FILE: a.kt
package test
import kotlin.reflect.KClass
enum class E { A, B }
annotation class A()
annotation class B(val a: A = A())
annotation class C(
val i: Int = 42,
val b: B = B(),
val kClass: KClass<*> = B::class,
val kClassArray: Array<KClass<*>> = [E::class, A::class],
val e: E = E.B,
val aS: Array<String> = arrayOf("a", "b"),
val aI: IntArray = intArrayOf(1, 2)
)
annotation class Partial(
val i: Int = 42,
val s: String = "foo",
val e: E = E.A
)
// FILE: b.kt
package test
import kotlin.test.assertTrue as assert
import kotlin.test.assertEquals
fun box(): String {
val c = C()
assertEquals(42, c.i)
assertEquals(A(), c.b.a)
assertEquals(B::class, c.kClass)
assertEquals(2, c.kClassArray.size)
assertEquals(E.B, c.e)
assert(arrayOf("a", "b").contentEquals(c.aS))
assert(intArrayOf(1, 2).contentEquals(c.aI))
val p = Partial(e = E.B, s = "bar")
assertEquals(42, p.i)
assertEquals("bar", p.s)
assertEquals(E.B, p.e)
return "OK"
}
@@ -426,6 +426,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefault.kt");
}
@Test
@TestMetadata("annotationInstancesEmptyDefaultLowered.kt")
public void testAnnotationInstancesEmptyDefaultLowered() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefaultLowered.kt");
}
@Test
@TestMetadata("annotationToString.kt")
public void testAnnotationToString() throws Exception {
@@ -450,6 +450,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefault.kt");
}
@Test
@TestMetadata("annotationInstancesEmptyDefaultLowered.kt")
public void testAnnotationInstancesEmptyDefaultLowered() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefaultLowered.kt");
}
@Test
@TestMetadata("annotationJvmHashCode.kt")
public void testAnnotationJvmHashCode() throws Exception {
@@ -370,6 +370,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefault.kt");
}
@TestMetadata("annotationInstancesEmptyDefaultLowered.kt")
public void ignoreAnnotationInstancesEmptyDefaultLowered() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefaultLowered.kt");
}
@TestMetadata("annotationToString.kt")
public void ignoreAnnotationToString() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationToString.kt");
@@ -108,6 +108,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefault.kt");
}
@Test
@TestMetadata("annotationInstancesEmptyDefaultLowered.kt")
public void testAnnotationInstancesEmptyDefaultLowered() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefaultLowered.kt");
}
@Test
@TestMetadata("annotationToString.kt")
public void testAnnotationToString() throws Exception {
@@ -113,6 +113,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefault.kt");
}
@TestMetadata("annotationInstancesEmptyDefaultLowered.kt")
public void testAnnotationInstancesEmptyDefaultLowered() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefaultLowered.kt");
}
@TestMetadata("annotationToString.kt")
public void testAnnotationToString() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationToString.kt");
@@ -69,7 +69,7 @@ internal class NativeAnnotationImplementationTransformer(context: Context, irFil
properties.forEach { property ->
generatedConstructor.addValueParameter(property.name.asString(), property.getter!!.returnType)
}
createConstructorBody(generatedConstructor, annotationClass.constructors.single())
createConstructorBody(generatedConstructor, annotationClass.primaryConstructor ?: error("Annotation class does not have primary constructor"))
}
private fun createConstructorBody(constructor: IrConstructor, delegate: IrConstructor) {
@@ -120,6 +120,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefault.kt");
}
@Test
@TestMetadata("annotationInstancesEmptyDefaultLowered.kt")
public void testAnnotationInstancesEmptyDefaultLowered() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/annotationInstancesEmptyDefaultLowered.kt");
}
@Test
@TestMetadata("annotationToString.kt")
public void testAnnotationToString() throws Exception {