diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index 2d4f06f2029..12cf0970106 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -542,6 +542,20 @@ class Fir2IrVisitor( val argumentsCount = call.arguments.size if (argumentsCount <= valueArgumentsCount) { apply { + val argumentMapping = call.argumentMapping + if (argumentMapping != null && argumentMapping.isNotEmpty()) { + require(call is FirFunctionCall) + val function = + ((call.calleeReference as? FirResolvedNamedReference)?.resolvedSymbol as? FirFunctionSymbol<*>)?.fir + val valueParameters = function?.valueParameters + if (valueParameters != null) { + for ((argument, parameter) in argumentMapping) { + val argumentExpression = argument.toIrExpression() + putValueArgument(valueParameters.indexOf(parameter), argumentExpression) + } + return this + } + } for ((index, argument) in call.arguments.withIndex()) { val argumentExpression = argument.toIrExpression() putValueArgument(index, argumentExpression) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt index f95b55dab35..dbeaf4cd67f 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirCallCompletionResultsWriterTransformer.kt @@ -181,15 +181,16 @@ class FirCallCompletionResultsWriterTransformer( else -> { resultType = typeRef.substituteTypeRef(subCandidate) val argumentMapping = subCandidate.argumentMapping - val vararg = argumentMapping?.values?.firstOrNull { it.isVararg } + val varargParameter = argumentMapping?.values?.firstOrNull { it.isVararg } result.argumentList.transformArguments(this, subCandidate.createArgumentsMapping()) with(result.argumentList) call@{ - if (vararg != null) { + if (varargParameter != null) { // Create a FirVarargArgumentExpression for the vararg arguments - val varargParameterTypeRef = vararg.returnTypeRef + val varargParameterTypeRef = varargParameter.returnTypeRef val resolvedArrayType = varargParameterTypeRef.substitute(subCandidate) val resolvedElementType = resolvedArrayType.arrayElementType(session) var firstIndex = this@call.arguments.size + val newArgumentMapping = mutableMapOf() val varargArgument = buildVarargArgumentsExpression { varargElementType = varargParameterTypeRef.withReplacedConeType(resolvedElementType) this.typeRef = varargParameterTypeRef.withReplacedConeType(resolvedArrayType) @@ -198,20 +199,18 @@ class FirCallCompletionResultsWriterTransformer( if (valueParameter.isVararg) { firstIndex = min(firstIndex, i) arguments += arg + } else { + newArgumentMapping[arg] = valueParameter } } } - result.replaceArgumentList( - buildArgumentList { - arguments += result.arguments - for (arg in varargArgument.arguments) { - arguments.remove(arg) - } - arguments.add(firstIndex, varargArgument) - } - ) + newArgumentMapping[varargArgument] = varargParameter + subCandidate.argumentMapping = newArgumentMapping } } + subCandidate.argumentMapping?.let { + result.replaceArgumentList(FirResolvedArgumentList(it)) + } result.transformExplicitReceiver(integerApproximator, null) } } diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirArgumentUtil.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirArgumentUtil.kt index 2dde86392bb..d08aeaba6a2 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirArgumentUtil.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirArgumentUtil.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.expressions import org.jetbrains.kotlin.fir.FirElement import org.jetbrains.kotlin.fir.FirSourceElement +import org.jetbrains.kotlin.fir.declarations.FirValueParameter import org.jetbrains.kotlin.fir.expressions.builder.buildArgumentList import org.jetbrains.kotlin.fir.visitors.FirTransformer import org.jetbrains.kotlin.fir.visitors.FirVisitor @@ -48,4 +49,17 @@ class FirArraySetArgumentList( ) : FirAbstractArgumentList() { override val arguments: List get() = indexes + rValue +} + +class FirResolvedArgumentList( + val mapping: Map +) : FirAbstractArgumentList() { + override val arguments: List + get() = mapping.keys.toList() + + override fun acceptChildren(visitor: FirVisitor, data: D) { + for (argument in mapping.keys) { + argument.accept(visitor, data) + } + } } \ No newline at end of file diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirExpressionUtil.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirExpressionUtil.kt index 7bc1994a90e..505d372de6b 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirExpressionUtil.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirExpressionUtil.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.fir.expressions import org.jetbrains.kotlin.fir.FirSourceElement +import org.jetbrains.kotlin.fir.declarations.FirValueParameter import org.jetbrains.kotlin.fir.diagnostics.FirDiagnostic import org.jetbrains.kotlin.fir.expressions.builder.buildConstExpression import org.jetbrains.kotlin.fir.expressions.builder.buildErrorExpression @@ -38,6 +39,9 @@ inline val FirCall.arguments: List get() = argumentList.arguments inline val FirCall.argument: FirExpression get() = argumentList.arguments.first() +inline val FirCall.argumentMapping: Map? + get() = (argumentList as? FirResolvedArgumentList)?.mapping + fun FirExpression.toResolvedCallableReference(): FirResolvedNamedReference? { return (this as? FirQualifiedAccess)?.calleeReference as? FirResolvedNamedReference } diff --git a/compiler/testData/codegen/box/argumentOrder/kt9277.kt b/compiler/testData/codegen/box/argumentOrder/kt9277.kt index eb08fb7b719..a0135659baa 100644 --- a/compiler/testData/codegen/box/argumentOrder/kt9277.kt +++ b/compiler/testData/codegen/box/argumentOrder/kt9277.kt @@ -1,3 +1,4 @@ +// IGNORE_BACKEND_FIR: JVM_IR // KT-9277 Unexpected NullPointerException in an invocaton with named arguments fun box(): String { diff --git a/compiler/testData/codegen/box/argumentOrder/lambdaMigration.kt b/compiler/testData/codegen/box/argumentOrder/lambdaMigration.kt index cde088f0309..6ef53bff6ee 100644 --- a/compiler/testData/codegen/box/argumentOrder/lambdaMigration.kt +++ b/compiler/testData/codegen/box/argumentOrder/lambdaMigration.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun box(): String { var res = ""; var call = test(a = {res += "K"; "K"}(), b = {res+="O"; "O"}(), c = {res += "L"; "L"}) diff --git a/compiler/testData/codegen/box/argumentOrder/lambdaMigrationInClass.kt b/compiler/testData/codegen/box/argumentOrder/lambdaMigrationInClass.kt index 2ab32dbef46..7100b035e29 100644 --- a/compiler/testData/codegen/box/argumentOrder/lambdaMigrationInClass.kt +++ b/compiler/testData/codegen/box/argumentOrder/lambdaMigrationInClass.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun box(): String { var res = ""; var call = Z("Z").test(a = {res += "K"; "K"}(), b = {res+="O"; "O"}(), c = {res += "L"; "L"}) diff --git a/compiler/testData/codegen/box/defaultArguments/constructor/defArgs2.kt b/compiler/testData/codegen/box/defaultArguments/constructor/defArgs2.kt index 68ecc1a7133..9a22d318336 100644 --- a/compiler/testData/codegen/box/defaultArguments/constructor/defArgs2.kt +++ b/compiler/testData/codegen/box/defaultArguments/constructor/defArgs2.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR class A(val a: Int = 0, val b: String = "a") fun box(): String { diff --git a/compiler/testData/codegen/box/defaultArguments/function/extensionFunctionManyArgs.kt b/compiler/testData/codegen/box/defaultArguments/function/extensionFunctionManyArgs.kt index f11e2a3bd4a..82984a99369 100644 --- a/compiler/testData/codegen/box/defaultArguments/function/extensionFunctionManyArgs.kt +++ b/compiler/testData/codegen/box/defaultArguments/function/extensionFunctionManyArgs.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun Int.foo(a: Int = 1, b: Int = 2, c: Int = 3, diff --git a/compiler/testData/codegen/box/defaultArguments/function/extentionFunctionWithOneDefArg.kt b/compiler/testData/codegen/box/defaultArguments/function/extentionFunctionWithOneDefArg.kt index 2bba9005ff3..c905daed1b2 100644 --- a/compiler/testData/codegen/box/defaultArguments/function/extentionFunctionWithOneDefArg.kt +++ b/compiler/testData/codegen/box/defaultArguments/function/extentionFunctionWithOneDefArg.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun Int.foo(a: Int = 1, b: String): Int { return a } diff --git a/compiler/testData/codegen/box/defaultArguments/function/innerExtentionFunctionManyArgs.kt b/compiler/testData/codegen/box/defaultArguments/function/innerExtentionFunctionManyArgs.kt index 4628d81a70d..3ed1be3d6c4 100644 --- a/compiler/testData/codegen/box/defaultArguments/function/innerExtentionFunctionManyArgs.kt +++ b/compiler/testData/codegen/box/defaultArguments/function/innerExtentionFunctionManyArgs.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR class A { fun Int.foo(a: Int = 1, b: Int = 2, diff --git a/compiler/testData/codegen/box/defaultArguments/function/memberFunctionManyArgs.kt b/compiler/testData/codegen/box/defaultArguments/function/memberFunctionManyArgs.kt index 13ac09d5832..def2549e106 100644 --- a/compiler/testData/codegen/box/defaultArguments/function/memberFunctionManyArgs.kt +++ b/compiler/testData/codegen/box/defaultArguments/function/memberFunctionManyArgs.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR class A() { fun foo(a: Int = 1, b: Int = 2, diff --git a/compiler/testData/codegen/box/defaultArguments/function/mixingNamedAndPositioned.kt b/compiler/testData/codegen/box/defaultArguments/function/mixingNamedAndPositioned.kt index ead077b8526..7b3aebeec18 100644 --- a/compiler/testData/codegen/box/defaultArguments/function/mixingNamedAndPositioned.kt +++ b/compiler/testData/codegen/box/defaultArguments/function/mixingNamedAndPositioned.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun foo(a: String = "Companion", b: Int = 1, c: Long = 2): String { return "$a $b $c" } diff --git a/compiler/testData/codegen/box/defaultArguments/function/topLevelManyArgs.kt b/compiler/testData/codegen/box/defaultArguments/function/topLevelManyArgs.kt index d4765b4e644..1086f23b45f 100644 --- a/compiler/testData/codegen/box/defaultArguments/function/topLevelManyArgs.kt +++ b/compiler/testData/codegen/box/defaultArguments/function/topLevelManyArgs.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun foo(a: Int = 1, b: Int = 2, c: Int = 3, diff --git a/compiler/testData/codegen/box/diagnostics/functions/tailRecursion/defaultArgsWithSideEffects.kt b/compiler/testData/codegen/box/diagnostics/functions/tailRecursion/defaultArgsWithSideEffects.kt index 056cd82ba40..54467ceb3a6 100644 --- a/compiler/testData/codegen/box/diagnostics/functions/tailRecursion/defaultArgsWithSideEffects.kt +++ b/compiler/testData/codegen/box/diagnostics/functions/tailRecursion/defaultArgsWithSideEffects.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +ProperComputationOrderOfTailrecDefaultParameters -// IGNORE_BACKEND_FIR: JVM_IR // DONT_RUN_GENERATED_CODE: JS var counter = 0 diff --git a/compiler/testData/codegen/box/functions/defaultargs1.kt b/compiler/testData/codegen/box/functions/defaultargs1.kt index 2c68c3ad37d..5666dd5603d 100644 --- a/compiler/testData/codegen/box/functions/defaultargs1.kt +++ b/compiler/testData/codegen/box/functions/defaultargs1.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun T.toPrefixedString(prefix: String = "", suffix: String="") = prefix + this.toString() + suffix fun box() : String { diff --git a/compiler/testData/codegen/box/javaInterop/notNullAssertions/destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt b/compiler/testData/codegen/box/javaInterop/notNullAssertions/destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt index cfc1b02ff08..6f81c5c535c 100644 --- a/compiler/testData/codegen/box/javaInterop/notNullAssertions/destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt +++ b/compiler/testData/codegen/box/javaInterop/notNullAssertions/destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: destructuringAssignmentWithNullabilityAssertionOnExtensionReceiver_lv12.kt diff --git a/compiler/testData/codegen/box/javaInterop/notNullAssertions/incWithNullabilityAssertionOnExtensionReceiver.kt b/compiler/testData/codegen/box/javaInterop/notNullAssertions/incWithNullabilityAssertionOnExtensionReceiver.kt index ec1a5bbeed1..7eea297f2b7 100644 --- a/compiler/testData/codegen/box/javaInterop/notNullAssertions/incWithNullabilityAssertionOnExtensionReceiver.kt +++ b/compiler/testData/codegen/box/javaInterop/notNullAssertions/incWithNullabilityAssertionOnExtensionReceiver.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // FILE: test.kt // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/javaInterop/notNullAssertions/incWithNullabilityAssertionOnExtensionReceiverInPrivateOperator.kt b/compiler/testData/codegen/box/javaInterop/notNullAssertions/incWithNullabilityAssertionOnExtensionReceiverInPrivateOperator.kt index 3c341674567..5a1d1602d93 100644 --- a/compiler/testData/codegen/box/javaInterop/notNullAssertions/incWithNullabilityAssertionOnExtensionReceiverInPrivateOperator.kt +++ b/compiler/testData/codegen/box/javaInterop/notNullAssertions/incWithNullabilityAssertionOnExtensionReceiverInPrivateOperator.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // FILE: test.kt // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/javaInterop/notNullAssertions/nullabilityAssertionOnExtensionReceiver.kt b/compiler/testData/codegen/box/javaInterop/notNullAssertions/nullabilityAssertionOnExtensionReceiver.kt index 2d73b561374..fd60e9e469b 100644 --- a/compiler/testData/codegen/box/javaInterop/notNullAssertions/nullabilityAssertionOnExtensionReceiver.kt +++ b/compiler/testData/codegen/box/javaInterop/notNullAssertions/nullabilityAssertionOnExtensionReceiver.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: test.kt diff --git a/compiler/testData/codegen/box/javaInterop/notNullAssertions/nullabilityAssertionOnMemberExtensionReceiver.kt b/compiler/testData/codegen/box/javaInterop/notNullAssertions/nullabilityAssertionOnMemberExtensionReceiver.kt index d62a0670301..4444ee18621 100644 --- a/compiler/testData/codegen/box/javaInterop/notNullAssertions/nullabilityAssertionOnMemberExtensionReceiver.kt +++ b/compiler/testData/codegen/box/javaInterop/notNullAssertions/nullabilityAssertionOnMemberExtensionReceiver.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: test.kt diff --git a/compiler/testData/codegen/box/platformTypes/primitives/equalsNull.kt b/compiler/testData/codegen/box/platformTypes/primitives/equalsNull.kt index f43b8c96106..220e31ff53b 100644 --- a/compiler/testData/codegen/box/platformTypes/primitives/equalsNull.kt +++ b/compiler/testData/codegen/box/platformTypes/primitives/equalsNull.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME // FILE: equalsNull.kt diff --git a/compiler/testData/codegen/box/secondaryConstructors/defaultArgs.kt b/compiler/testData/codegen/box/secondaryConstructors/defaultArgs.kt index a787cfe01f2..3c9d312462e 100644 --- a/compiler/testData/codegen/box/secondaryConstructors/defaultArgs.kt +++ b/compiler/testData/codegen/box/secondaryConstructors/defaultArgs.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR val global = "OK" class A { val prop: String diff --git a/compiler/testData/codegen/box/signatureAnnotations/reorderedParameterNames.kt b/compiler/testData/codegen/box/signatureAnnotations/reorderedParameterNames.kt index 5fa9e3b634d..6227b07240c 100644 --- a/compiler/testData/codegen/box/signatureAnnotations/reorderedParameterNames.kt +++ b/compiler/testData/codegen/box/signatureAnnotations/reorderedParameterNames.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // FILE: A.java diff --git a/compiler/testData/codegen/box/vararg/varargsAndFunctionLiterals.kt b/compiler/testData/codegen/box/vararg/varargsAndFunctionLiterals.kt index d2132071175..3c7dca68c44 100644 --- a/compiler/testData/codegen/box/vararg/varargsAndFunctionLiterals.kt +++ b/compiler/testData/codegen/box/vararg/varargsAndFunctionLiterals.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR fun foo(a: Int, vararg b: Int, f: (IntArray) -> String): String { return "test" + a + " " + f(b) } diff --git a/compiler/testData/ir/irText/declarations/parameters/useNextParamInLambda.fir.txt b/compiler/testData/ir/irText/declarations/parameters/useNextParamInLambda.fir.txt index 3af32fcadd7..7f75d584782 100644 --- a/compiler/testData/ir/irText/declarations/parameters/useNextParamInLambda.fir.txt +++ b/compiler/testData/ir/irText/declarations/parameters/useNextParamInLambda.fir.txt @@ -32,7 +32,7 @@ FILE fqName: fileName:/useNextParamInLambda.kt RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' CALL 'public final fun plus (other: kotlin.Any?): kotlin.String [operator] declared in kotlin.String' type=kotlin.String origin=PLUS $this: CALL 'public final fun f (f1: kotlin.Function0, f2: kotlin.Function0): kotlin.String declared in ' type=kotlin.String origin=null - f1: FUN_EXPR type=kotlin.Function0 origin=LAMBDA + f2: FUN_EXPR type=kotlin.Function0 origin=LAMBDA FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> () returnType:kotlin.String BLOCK_BODY RETURN type=kotlin.Nothing from='local final fun (): kotlin.String declared in .box' diff --git a/compiler/testData/ir/irText/expressions/callWithReorderedArguments.fir.txt b/compiler/testData/ir/irText/expressions/callWithReorderedArguments.fir.txt index 6b7dcb6ba07..fcc441cf975 100644 --- a/compiler/testData/ir/irText/expressions/callWithReorderedArguments.fir.txt +++ b/compiler/testData/ir/irText/expressions/callWithReorderedArguments.fir.txt @@ -25,8 +25,8 @@ FILE fqName: fileName:/callWithReorderedArguments.kt a: CALL 'public final fun noReorder1 (): kotlin.Int declared in ' type=kotlin.Int origin=null b: CALL 'public final fun noReorder2 (): kotlin.Int declared in ' type=kotlin.Int origin=null CALL 'public final fun foo (a: kotlin.Int, b: kotlin.Int): kotlin.Unit declared in ' type=kotlin.Unit origin=null - a: CALL 'public final fun reordered1 (): kotlin.Int declared in ' type=kotlin.Int origin=null - b: CALL 'public final fun reordered2 (): kotlin.Int declared in ' type=kotlin.Int origin=null + a: CALL 'public final fun reordered2 (): kotlin.Int declared in ' type=kotlin.Int origin=null + b: CALL 'public final fun reordered1 (): kotlin.Int declared in ' type=kotlin.Int origin=null CALL 'public final fun foo (a: kotlin.Int, b: kotlin.Int): kotlin.Unit declared in ' type=kotlin.Unit origin=null - a: CONST Int type=kotlin.Int value=1 - b: CALL 'public final fun reordered2 (): kotlin.Int declared in ' type=kotlin.Int origin=null + a: CALL 'public final fun reordered2 (): kotlin.Int declared in ' type=kotlin.Int origin=null + b: CONST Int type=kotlin.Int value=1