IrConstTransformer: handle vararg with spread elements properly
This commit is contained in:
committed by
Mikhail Glukhikh
parent
5600eefea5
commit
db9d42c153
+16
-5
@@ -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 {
|
||||
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
// WITH_REFLECT
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// TARGET_BACKEND: JVM
|
||||
|
||||
import java.util.Arrays
|
||||
|
||||
Vendored
+1
-1
@@ -29,5 +29,5 @@ FILE fqName:<root> fileName:/spreadOperatorInAnnotationArguments.kt
|
||||
$this: VALUE_PARAMETER name:<this> 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
|
||||
|
||||
Reference in New Issue
Block a user