diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index a589881e463..294c0315772 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -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 { diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AnnotationImplementationTransformer.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AnnotationImplementationTransformer.kt index 30d2aa70029..b70cd74b122 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AnnotationImplementationTransformer.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/AnnotationImplementationTransformer.kt @@ -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('.', '_') diff --git a/compiler/testData/codegen/box/annotations/instances/kotlinExistingAnnotation.kt b/compiler/testData/codegen/box/annotations/instances/kotlinExistingAnnotation.kt new file mode 100644 index 00000000000..f75af1eaf8a --- /dev/null +++ b/compiler/testData/codegen/box/annotations/instances/kotlinExistingAnnotation.kt @@ -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() +} \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index 7ef3db8a5a7..e160fc79670 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -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 {