From db9d42c153daf1d15e1254267cd069ae1407b92b Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Mon, 13 Jul 2020 01:20:57 -0700 Subject: [PATCH] IrConstTransformer: handle vararg with spread elements properly --- .../backend/evaluate/IrConstTransformer.kt | 21 ++++++++++++++----- .../collectionLiteralsWithVarargs.kt | 1 - ...preadOperatorInAnnotationArguments.fir.txt | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/evaluate/IrConstTransformer.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/evaluate/IrConstTransformer.kt index b12cc9b41b9..f2632460744 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/evaluate/IrConstTransformer.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/evaluate/IrConstTransformer.kt @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.ir.expressions.impl.IrVarargImpl import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid @@ -60,18 +61,28 @@ class IrConstTransformer(irBuiltIns: IrBuiltIns) : IrElementTransformerVoid() { for (i in 0 until annotation.valueArgumentsCount) { val arg = annotation.getValueArgument(i) ?: continue when (arg) { - is IrVararg -> arg.transformVarArg() + is IrVararg -> annotation.putValueArgument(i, arg.transformVarArg()) else -> annotation.putValueArgument(i, arg.transformSingleArg(annotation.symbol.owner.valueParameters[i].type)) } } } } - private fun IrVararg.transformVarArg() { - for (i in this.elements.indices) { - val irVarargElement = this.elements[i] as? IrExpression ?: continue - this.putElement(i, irVarargElement.transformSingleArg(this.varargElementType)) + private fun IrVararg.transformVarArg(): IrVararg { + if (elements.isEmpty()) return this + val newIrVararg = IrVarargImpl(this.startOffset, this.endOffset, this.type, this.varargElementType) + for (element in this.elements) { + when (element) { + is IrExpression -> newIrVararg.addElement(element.transformSingleArg(this.varargElementType)) + is IrSpreadElement -> { + when (val expression = element.expression) { + is IrVararg -> expression.transformVarArg().elements.forEach { newIrVararg.addElement(it) } + else -> newIrVararg.addElement(expression.transformSingleArg(this.varargElementType)) + } + } + } } + return newIrVararg } private fun IrExpression.transformSingleArg(expectedType: IrType): IrExpression { diff --git a/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithVarargs.kt b/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithVarargs.kt index deeb5138f19..c2f3a39b320 100644 --- a/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithVarargs.kt +++ b/compiler/testData/codegen/box/collectionLiterals/collectionLiteralsWithVarargs.kt @@ -1,5 +1,4 @@ // WITH_REFLECT -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM import java.util.Arrays diff --git a/compiler/testData/ir/irText/declarations/annotations/spreadOperatorInAnnotationArguments.fir.txt b/compiler/testData/ir/irText/declarations/annotations/spreadOperatorInAnnotationArguments.fir.txt index 11bd3b3f236..663810ef6b1 100644 --- a/compiler/testData/ir/irText/declarations/annotations/spreadOperatorInAnnotationArguments.fir.txt +++ b/compiler/testData/ir/irText/declarations/annotations/spreadOperatorInAnnotationArguments.fir.txt @@ -29,5 +29,5 @@ FILE fqName: fileName:/spreadOperatorInAnnotationArguments.kt $this: VALUE_PARAMETER name: type:kotlin.Any FUN name:test visibility:public modality:FINAL <> () returnType:kotlin.Unit annotations: - A(xs = [SPREAD_ELEMENT, SPREAD_ELEMENT]) + A(xs = ['a', 'b']) BLOCK_BODY