Correctly rearrange arguments for annotation constructors copied from another module

It won't fix KT-48181 completely, but it will allow to create such annotations
if all value arguments are specified.

For the full fix, we need to read default annotation values from classfiles in jar.

#KT-48181 In progress
This commit is contained in:
Leonid Startsev
2021-09-24 20:44:55 +03:00
committed by Space
parent 60cbfbf107
commit e7f1cef3a1
4 changed files with 53 additions and 2 deletions
@@ -455,6 +455,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/annotations/instances/javaAnnotation.kt");
}
@Test
@TestMetadata("kotlinExistingAnnotation.kt")
public void testKotlinExistingAnnotation() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/kotlinExistingAnnotation.kt");
}
@Test
@TestMetadata("multifileEqHc.kt")
public void testMultifileEqHc() throws Exception {
@@ -12,7 +12,8 @@ import org.jetbrains.kotlin.backend.common.ir.addFakeOverrides
import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.builders.declarations.addConstructor
import org.jetbrains.kotlin.ir.builders.declarations.buildClass
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
@@ -56,13 +57,28 @@ abstract class AnnotationImplementationTransformer(val context: BackendContext,
implClass.defaultType,
ctor.symbol,
)
newCall.copyTypeAndValueArgumentsFrom(expression)
moveValueArgumentsUsingNames(expression, newCall)
newCall.transformChildrenVoid() // for annotations in annotations
return newCall
}
open fun IrClass.platformSetup() {}
private fun moveValueArgumentsUsingNames(source: IrConstructorCall, destination: IrConstructorCall) {
val argumentsByName = source.getArgumentsWithIr().associateBy(
{ (param, _) -> param.name },
{ (_, value) -> value }
)
destination.symbol.owner.valueParameters.forEachIndexed { index, parameter ->
val valueArg = argumentsByName[parameter.name]
if (parameter.defaultValue == null && valueArg == null)
error("Usage of default value argument for this annotation is not yet possible.\n" +
"Please specify value for '${source.type.classOrNull?.owner?.name}.${parameter.name}' explicitly")
destination.putValueArgument(index, valueArg)
}
}
private fun createAnnotationImplementation(annotationClass: IrClass): IrClass {
val localDeclarationParent = currentClass?.scope?.getLocalDeclarationParent() as? IrClass
val parentFqName = annotationClass.fqNameWhenAvailable!!.asString().replace('.', '_')
@@ -0,0 +1,23 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
// !LANGUAGE: +InstantiationOfAnnotationClasses
fun f(): Metadata = Metadata(
kind = 0,
metadataVersion = intArrayOf(),
data1 = arrayOf(),
data2 = arrayOf(),
extraString = "",
packageName = "foo",
extraInt = 0,
bytecodeVersion = intArrayOf(1, 0, 3),
)
fun box(): String {
val m = f()
if (m.toString() == """@kotlin.Metadata(bytecodeVersion=[1, 0, 3], data1=[], data2=[], extraInt=0, extraString=, kind=0, metadataVersion=[], packageName=foo)""")
return "OK"
return m.toString()
}
@@ -455,6 +455,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/annotations/instances/javaAnnotation.kt");
}
@Test
@TestMetadata("kotlinExistingAnnotation.kt")
public void testKotlinExistingAnnotation() throws Exception {
runTest("compiler/testData/codegen/box/annotations/instances/kotlinExistingAnnotation.kt");
}
@Test
@TestMetadata("multifileEqHc.kt")
public void testMultifileEqHc() throws Exception {