FIR2IR: allow array expression as named argument for vararg
This commit is contained in:
committed by
Mikhail Glukhikh
parent
54d3c5fb0a
commit
085e0dc1de
+33
-2
@@ -611,8 +611,11 @@ class CallAndReferenceGenerator(
|
||||
}
|
||||
}
|
||||
for ((index, argument) in call.arguments.withIndex()) {
|
||||
val valueParameter = valueParameters?.get(index)
|
||||
val argumentExpression =
|
||||
visitor.convertToIrExpression(argument).applySamConversionIfNeeded(argument, valueParameters?.get(index))
|
||||
visitor.convertToIrExpression(argument)
|
||||
.applySamConversionIfNeeded(argument, valueParameter)
|
||||
.applyAssigningArrayElementsToVarargInNamedForm(argument, valueParameter)
|
||||
putValueArgument(index, argumentExpression)
|
||||
}
|
||||
}
|
||||
@@ -650,7 +653,10 @@ class CallAndReferenceGenerator(
|
||||
return IrBlockImpl(startOffset, endOffset, type, IrStatementOrigin.ARGUMENTS_REORDERING_FOR_CALL).apply {
|
||||
for ((argument, parameter) in argumentMapping) {
|
||||
val parameterIndex = valueParameters.indexOf(parameter)
|
||||
val irArgument = visitor.convertToIrExpression(argument).applySamConversionIfNeeded(argument, parameter)
|
||||
val irArgument =
|
||||
visitor.convertToIrExpression(argument)
|
||||
.applySamConversionIfNeeded(argument, parameter)
|
||||
.applyAssigningArrayElementsToVarargInNamedForm(argument, parameter)
|
||||
if (irArgument.hasNoSideEffects()) {
|
||||
putValueArgument(parameterIndex, irArgument)
|
||||
} else {
|
||||
@@ -668,6 +674,7 @@ class CallAndReferenceGenerator(
|
||||
val argumentExpression =
|
||||
visitor.convertToIrExpression(argument, annotationMode)
|
||||
.applySamConversionIfNeeded(argument, parameter)
|
||||
.applyAssigningArrayElementsToVarargInNamedForm(argument, parameter)
|
||||
putValueArgument(valueParameters.indexOf(parameter), argumentExpression)
|
||||
}
|
||||
if (annotationMode) {
|
||||
@@ -721,6 +728,30 @@ class CallAndReferenceGenerator(
|
||||
return argument.isFunctional(session)
|
||||
}
|
||||
|
||||
private fun IrExpression.applyAssigningArrayElementsToVarargInNamedForm(
|
||||
argument: FirExpression,
|
||||
parameter: FirValueParameter?
|
||||
): IrExpression {
|
||||
// TODO: Need to refer to language feature: AllowAssigningArrayElementsToVarargsInNamedFormForFunctions
|
||||
if (this !is IrVarargImpl ||
|
||||
parameter?.isVararg != true ||
|
||||
argument !is FirVarargArgumentsExpression ||
|
||||
argument.arguments.none { it is FirNamedArgumentExpression }
|
||||
) {
|
||||
return this
|
||||
}
|
||||
elements.forEachIndexed { i, irVarargElement ->
|
||||
if (irVarargElement !is IrSpreadElement &&
|
||||
argument.arguments[i] is FirNamedArgumentExpression &&
|
||||
irVarargElement is IrExpression &&
|
||||
irVarargElement.type.isArray()
|
||||
) {
|
||||
elements[i] = IrSpreadElementImpl(irVarargElement.startOffset, irVarargElement.endOffset, irVarargElement)
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
private fun IrExpression.applyTypeArguments(access: FirQualifiedAccess): IrExpression {
|
||||
return when (this) {
|
||||
is IrMemberAccessExpression<*> -> {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// !LANGUAGE: +AllowAssigningArrayElementsToVarargsInNamedFormForFunctions
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// IGNORE_BACKEND: JS
|
||||
|
||||
fun test(vararg s: String) = s[1]
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
FILE fqName:<root> fileName:/kt37779.kt
|
||||
FUN name:foo visibility:public modality:FINAL <> (s:kotlin.Array<out kotlin.String>) returnType:kotlin.Unit
|
||||
VALUE_PARAMETER name:s index:0 type:kotlin.Array<out kotlin.String> varargElementType:kotlin.String [vararg]
|
||||
BLOCK_BODY
|
||||
FUN name:test1 visibility:public modality:FINAL <> () returnType:kotlin.Unit
|
||||
BLOCK_BODY
|
||||
CALL 'public final fun foo (vararg s: kotlin.String): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
|
||||
s: VARARG type=kotlin.Array<out kotlin.String> varargElementType=kotlin.String
|
||||
CALL 'public final fun arrayOf <T> (vararg elements: T of kotlin.arrayOf): kotlin.Array<T of kotlin.arrayOf> [inline] declared in kotlin' type=kotlin.Array<kotlin.String> origin=null
|
||||
<T>: kotlin.String
|
||||
elements: VARARG type=kotlin.Array<out kotlin.String> varargElementType=kotlin.String
|
||||
CONST String type=kotlin.String value=""
|
||||
CONST String type=kotlin.String value="OK"
|
||||
FUN name:test2 visibility:public modality:FINAL <> (ss:kotlin.Array<kotlin.String>) returnType:kotlin.Unit
|
||||
VALUE_PARAMETER name:ss index:0 type:kotlin.Array<kotlin.String>
|
||||
BLOCK_BODY
|
||||
CALL 'public final fun foo (vararg s: kotlin.String): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
|
||||
s: VARARG type=kotlin.Array<out kotlin.String> varargElementType=kotlin.String
|
||||
GET_VAR 'ss: kotlin.Array<kotlin.String> declared in <root>.test2' type=kotlin.Array<kotlin.String> origin=null
|
||||
@@ -1,3 +1,4 @@
|
||||
// FIR_IDENTICAL
|
||||
// !LANGUAGE: +AllowAssigningArrayElementsToVarargsInNamedFormForFunctions
|
||||
fun foo(vararg s: String) {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user