diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt index 826f9d30455..50cbd5074bd 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt @@ -324,20 +324,31 @@ class CallAndReferenceGenerator( } } + fun IrValueParameter.toGetValue(): IrGetValue = + IrGetValueImpl(startOffset, endOffset, this.type, this.symbol) + adapteeFunction.valueParameters.mapIndexed { index, valueParameter -> when { valueParameter.hasDefaultValue() -> { irCall.putValueArgument(shift + index, null) } valueParameter.isVararg -> { - // TODO: handle vararg and spread - irCall.putValueArgument(shift + index, null) + if (adapterFunction.valueParameters.size <= index) { + irCall.putValueArgument(shift + index, null) + } else { + val adaptedValueArgument = + IrVarargImpl(startOffset, endOffset, valueParameter.type, valueParameter.varargElementType!!) + val irValueArgument = adapterFunction.valueParameters[index].toGetValue() + if (irValueArgument.type == valueParameter.type) { + adaptedValueArgument.addElement(IrSpreadElementImpl(startOffset, endOffset, irValueArgument)) + } else { + adaptedValueArgument.addElement(irValueArgument) + } + irCall.putValueArgument(shift + index, adaptedValueArgument) + } } else -> { - val irValueArgument = adapterFunction.valueParameters[index] - irCall.putValueArgument( - shift + index, IrGetValueImpl(startOffset, endOffset, irValueArgument.type, irValueArgument.symbol) - ) + irCall.putValueArgument(shift + index, adapterFunction.valueParameters[index].toGetValue()) } } } diff --git a/compiler/testData/codegen/box/callableReference/adaptedReferences/suspendConversion/adaptedWithVarargs.kt b/compiler/testData/codegen/box/callableReference/adaptedReferences/suspendConversion/adaptedWithVarargs.kt index 458f1c04fe8..77ed5a7ce9b 100644 --- a/compiler/testData/codegen/box/callableReference/adaptedReferences/suspendConversion/adaptedWithVarargs.kt +++ b/compiler/testData/codegen/box/callableReference/adaptedReferences/suspendConversion/adaptedWithVarargs.kt @@ -1,7 +1,6 @@ // !LANGUAGE: +SuspendConversion // WITH_RUNTIME // WITH_COROUTINES -// IGNORE_BACKEND_FIR: JVM_IR import helpers.* import kotlin.coroutines.* diff --git a/compiler/testData/codegen/box/callableReference/adaptedReferences/suspendConversion/inlineAdaptedWithVarargs.kt b/compiler/testData/codegen/box/callableReference/adaptedReferences/suspendConversion/inlineAdaptedWithVarargs.kt index 78d3289f5d5..8444182b076 100644 --- a/compiler/testData/codegen/box/callableReference/adaptedReferences/suspendConversion/inlineAdaptedWithVarargs.kt +++ b/compiler/testData/codegen/box/callableReference/adaptedReferences/suspendConversion/inlineAdaptedWithVarargs.kt @@ -1,7 +1,6 @@ // !LANGUAGE: +SuspendConversion // WITH_RUNTIME // WITH_COROUTINES -// IGNORE_BACKEND_FIR: JVM_IR import helpers.* import kotlin.coroutines.* diff --git a/compiler/testData/ir/irText/expressions/callableReferences/adaptedWithCoercionToUnit.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/adaptedWithCoercionToUnit.fir.txt index c272dd0f371..cd6c5237784 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/adaptedWithCoercionToUnit.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/adaptedWithCoercionToUnit.fir.txt @@ -54,3 +54,5 @@ FILE fqName: fileName:/adaptedWithCoercionToUnit.kt VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:kotlin.Int BLOCK_BODY CALL 'public final fun fnv (vararg xs: kotlin.Int): kotlin.Int declared in ' type=kotlin.Int origin=null + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .testV1.fnv' type=kotlin.Int origin=null diff --git a/compiler/testData/ir/irText/expressions/callableReferences/suspendConversion.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/suspendConversion.fir.txt index 8ed79f2dc26..330817e791b 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/suspendConversion.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/suspendConversion.fir.txt @@ -88,6 +88,8 @@ FILE fqName: fileName:/suspendConversion.kt VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:kotlin.Int BLOCK_BODY CALL 'public final fun foo2 (vararg xs: kotlin.Int): kotlin.Unit declared in ' type=kotlin.Unit origin=null + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .testWithVarargMapped.foo2' type=kotlin.Int origin=null FUN name:testWithCoercionToUnit visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY CALL 'public final fun useSuspend (fn: kotlin.coroutines.SuspendFunction0): kotlin.Unit declared in ' type=kotlin.Unit origin=null diff --git a/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt index 4fca87281ae..03618a22e41 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt @@ -67,6 +67,8 @@ FILE fqName: fileName:/unboundMemberReferenceWithAdaptedArguments.kt BLOCK_BODY CALL 'public open fun foo (vararg xs: kotlin.Int): kotlin.Int declared in .A' type=kotlin.Int origin=null $this: GET_VAR 'p0: .A declared in .testUnbound.foo' type=.A origin=null + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: .A declared in .testUnbound.foo' type=.A origin=null FUN name:testBound visibility:public modality:FINAL <> (a:.A) returnType:kotlin.Unit VALUE_PARAMETER name:a index:0 type:.A BLOCK_BODY @@ -78,6 +80,8 @@ FILE fqName: fileName:/unboundMemberReferenceWithAdaptedArguments.kt BLOCK_BODY CALL 'public open fun foo (vararg xs: kotlin.Int): kotlin.Int declared in .A' type=kotlin.Int origin=null $this: GET_VAR 'receiver: .A declared in .testBound.foo' type=.A origin=ADAPTED_FUNCTION_REFERENCE + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .testBound.foo' type=kotlin.Int origin=null FUNCTION_REFERENCE 'local final fun foo (p0: kotlin.Int): kotlin.Unit declared in .testBound' type=kotlin.Function1 origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=null $receiver: GET_VAR 'a: .A declared in .testBound' type=.A origin=null FUN name:testObject visibility:public modality:FINAL <> () returnType:kotlin.Unit @@ -90,5 +94,7 @@ FILE fqName: fileName:/unboundMemberReferenceWithAdaptedArguments.kt BLOCK_BODY CALL 'public final fun foo (vararg xs: kotlin.Int): kotlin.Int declared in .Obj' type=kotlin.Int origin=null $this: GET_VAR 'receiver: .Obj declared in .testObject.foo' type=.Obj origin=ADAPTED_FUNCTION_REFERENCE + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .testObject.foo' type=kotlin.Int origin=null FUNCTION_REFERENCE 'local final fun foo (p0: kotlin.Int): kotlin.Unit declared in .testObject' type=kotlin.Function1 origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=null $receiver: GET_OBJECT 'CLASS OBJECT name:Obj modality:FINAL visibility:public superTypes:[.A]' type=.Obj diff --git a/compiler/testData/ir/irText/expressions/callableReferences/withAdaptationForSam.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/withAdaptationForSam.fir.txt index 724d82ed2b8..395717b0639 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/withAdaptationForSam.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/withAdaptationForSam.fir.txt @@ -34,3 +34,5 @@ FILE fqName: fileName:/withAdaptationForSam.kt VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:kotlin.Int BLOCK_BODY CALL 'public final fun withVararg (vararg xs: kotlin.Int): kotlin.Int declared in ' type=kotlin.Int origin=null + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .test.withVararg' type=kotlin.Int origin=null diff --git a/compiler/testData/ir/irText/expressions/callableReferences/withArgumentAdaptationAndReceiver.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/withArgumentAdaptationAndReceiver.fir.txt index 23666ea474e..b93fa1d6b6e 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/withArgumentAdaptationAndReceiver.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/withArgumentAdaptationAndReceiver.fir.txt @@ -28,6 +28,8 @@ FILE fqName: fileName:/withArgumentAdaptationAndReceiver.kt BLOCK_BODY CALL 'public final fun withVararg (vararg xs: kotlin.Int): kotlin.String declared in .Host' type=kotlin.String origin=null $this: GET_VAR 'receiver: .Host declared in .Host.testImplicitThis.withVararg' type=.Host origin=ADAPTED_FUNCTION_REFERENCE + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .Host.testImplicitThis.withVararg' type=kotlin.Int origin=null FUNCTION_REFERENCE 'local final fun withVararg (p0: kotlin.Int): kotlin.Unit declared in .Host.testImplicitThis' type=kotlin.Function1 origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=null FUN name:testBoundReceiverLocalVal visibility:public modality:FINAL <> ($this:.Host) returnType:kotlin.Unit $this: VALUE_PARAMETER name: type:.Host @@ -42,6 +44,8 @@ FILE fqName: fileName:/withArgumentAdaptationAndReceiver.kt BLOCK_BODY CALL 'public final fun withVararg (vararg xs: kotlin.Int): kotlin.String declared in .Host' type=kotlin.String origin=null $this: GET_VAR 'receiver: .Host declared in .Host.testBoundReceiverLocalVal.withVararg' type=.Host origin=ADAPTED_FUNCTION_REFERENCE + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .Host.testBoundReceiverLocalVal.withVararg' type=kotlin.Int origin=null FUNCTION_REFERENCE 'local final fun withVararg (p0: kotlin.Int): kotlin.Unit declared in .Host.testBoundReceiverLocalVal' type=kotlin.Function1 origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=null $receiver: GET_VAR 'val h: .Host [val] declared in .Host.testBoundReceiverLocalVal' type=.Host origin=null FUN name:testBoundReceiverLocalVar visibility:public modality:FINAL <> ($this:.Host) returnType:kotlin.Unit @@ -59,6 +63,8 @@ FILE fqName: fileName:/withArgumentAdaptationAndReceiver.kt BLOCK_BODY CALL 'public final fun withVararg (vararg xs: kotlin.Int): kotlin.String declared in .Host' type=kotlin.String origin=null $this: GET_VAR 'receiver: .Host declared in .Host.testBoundReceiverLocalVar.withVararg' type=.Host origin=ADAPTED_FUNCTION_REFERENCE + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .Host.testBoundReceiverLocalVar.withVararg' type=kotlin.Int origin=null FUNCTION_REFERENCE 'local final fun withVararg (p0: kotlin.Int): kotlin.Unit declared in .Host.testBoundReceiverLocalVar' type=kotlin.Function1 origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=null $receiver: GET_VAR 'var h: .Host [var] declared in .Host.testBoundReceiverLocalVar' type=.Host origin=null FUN name:testBoundReceiverParameter visibility:public modality:FINAL <> ($this:.Host, h:.Host) returnType:kotlin.Unit @@ -73,6 +79,8 @@ FILE fqName: fileName:/withArgumentAdaptationAndReceiver.kt BLOCK_BODY CALL 'public final fun withVararg (vararg xs: kotlin.Int): kotlin.String declared in .Host' type=kotlin.String origin=null $this: GET_VAR 'receiver: .Host declared in .Host.testBoundReceiverParameter.withVararg' type=.Host origin=ADAPTED_FUNCTION_REFERENCE + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .Host.testBoundReceiverParameter.withVararg' type=kotlin.Int origin=null FUNCTION_REFERENCE 'local final fun withVararg (p0: kotlin.Int): kotlin.Unit declared in .Host.testBoundReceiverParameter' type=kotlin.Function1 origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=null $receiver: GET_VAR 'h: .Host declared in .Host.testBoundReceiverParameter' type=.Host origin=null FUN name:testBoundReceiverExpression visibility:public modality:FINAL <> ($this:.Host) returnType:kotlin.Unit @@ -88,6 +96,8 @@ FILE fqName: fileName:/withArgumentAdaptationAndReceiver.kt BLOCK_BODY CALL 'public final fun withVararg (vararg xs: kotlin.Int): kotlin.String declared in .Host' type=kotlin.String origin=null $this: GET_VAR 'receiver: .Host declared in .Host.testBoundReceiverExpression.withVararg' type=.Host origin=ADAPTED_FUNCTION_REFERENCE + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .Host.testBoundReceiverExpression.withVararg' type=kotlin.Int origin=null FUNCTION_REFERENCE 'local final fun withVararg (p0: kotlin.Int): kotlin.Unit declared in .Host.testBoundReceiverExpression' type=kotlin.Function1 origin=ADAPTED_FUNCTION_REFERENCE reflectionTarget=null $receiver: CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Host' type=.Host origin=null FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] diff --git a/compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargs.fir.txt b/compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargs.fir.txt index d9afb3cfadc..f254a234fbd 100644 --- a/compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargs.fir.txt +++ b/compiler/testData/ir/irText/expressions/funInterface/samConversionInVarargs.fir.txt @@ -66,3 +66,5 @@ FILE fqName: fileName:/samConversionInVarargs.kt VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:kotlin.Int BLOCK_BODY CALL 'public final fun withVarargOfInt (vararg xs: kotlin.Int): kotlin.String declared in ' type=kotlin.String origin=null + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p0: kotlin.Int declared in .testAdaptedCR.withVarargOfInt' type=kotlin.Int origin=null