diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 8fd95b9ae79..1797cb1efda 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -355,6 +355,16 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/argumentOrder/extensionInClass.kt"); } + @TestMetadata("kt17691.kt") + public void testKt17691() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691.kt"); + } + + @TestMetadata("kt17691WithEnabledFeature.kt") + public void testKt17691WithEnabledFeature() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt"); + } + @TestMetadata("kt9277.kt") public void testKt9277() throws Exception { runTest("compiler/testData/codegen/box/argumentOrder/kt9277.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index 11963e0918a..2cf7d52ff64 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -6,6 +6,8 @@ package org.jetbrains.kotlin.resolve.calls.tower import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.config.LanguageFeature +import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor @@ -227,7 +229,13 @@ class KotlinToResolvedCallTransformer( return storedResolvedCall } } - return NewResolvedCallImpl(completedSimpleAtom, resultSubstitutor, diagnostics, typeApproximator) + return NewResolvedCallImpl( + completedSimpleAtom, + resultSubstitutor, + diagnostics, + typeApproximator, + expressionTypingServices.languageVersionSettings + ) } fun runCallCheckers(resolvedCall: ResolvedCall<*>, callCheckerContext: CallCheckerContext) { @@ -551,6 +559,7 @@ class TrackingBindingTrace(val trace: BindingTrace) : BindingTrace by trace { sealed class NewAbstractResolvedCall() : ResolvedCall { abstract val argumentMappingByOriginal: Map abstract val kotlinCall: KotlinCall + protected abstract val languageVersionSettings: LanguageVersionSettings protected var argumentToParameterMap: Map? = null protected var _valueArguments: Map? = null @@ -620,6 +629,8 @@ sealed class NewAbstractResolvedCall() : ResolvedCall private fun createValueArguments(): Map = LinkedHashMap().also { result -> + val needToUseCorrectExecutionOrderForVarargArguments = + languageVersionSettings.getFeatureSupport(LanguageFeature.UseCorrectExecutionOrderForVarargArguments) == LanguageFeature.State.ENABLED var varargMappings: MutableList>? = null for ((originalParameter, resolvedCallArgument) in argumentMappingByOriginal) { val resultingParameter = resultingDescriptor.valueParameters[originalParameter.index] @@ -630,12 +641,17 @@ sealed class NewAbstractResolvedCall() : ResolvedCall is ResolvedCallArgument.SimpleArgument -> { val valueArgument = resolvedCallArgument.callArgument.psiCallArgument.valueArgument if (resultingParameter.isVararg) { - val vararg = VarargValueArgument().apply { addArgument(valueArgument) } - if (varargMappings == null) varargMappings = SmartList() - varargMappings.add(resultingParameter to vararg) - continue - } else + if (needToUseCorrectExecutionOrderForVarargArguments) { + VarargValueArgument().apply { addArgument(valueArgument) } + } else { + val vararg = VarargValueArgument().apply { addArgument(valueArgument) } + if (varargMappings == null) varargMappings = SmartList() + varargMappings.add(resultingParameter to vararg) + continue + } + } else { ExpressionValueArgument(valueArgument) + } } is ResolvedCallArgument.VarargArgument -> VarargValueArgument().apply { @@ -644,9 +660,11 @@ sealed class NewAbstractResolvedCall() : ResolvedCall } } - if (varargMappings != null) { - for ((parameter, argument) in varargMappings) { - result[parameter] = argument + if (varargMappings != null && !needToUseCorrectExecutionOrderForVarargArguments) { + if (varargMappings != null) { + for ((parameter, argument) in varargMappings) { + result[parameter] = argument + } } } } @@ -658,6 +676,7 @@ class NewResolvedCallImpl( substitutor: NewTypeSubstitutor?, private var diagnostics: Collection, private val typeApproximator: TypeApproximator, + override val languageVersionSettings: LanguageVersionSettings, ) : NewAbstractResolvedCall() { var isCompleted = false private set diff --git a/compiler/testData/codegen/box/argumentOrder/kt17691.kt b/compiler/testData/codegen/box/argumentOrder/kt17691.kt new file mode 100644 index 00000000000..8718236b66a --- /dev/null +++ b/compiler/testData/codegen/box/argumentOrder/kt17691.kt @@ -0,0 +1,19 @@ +// !LANGUAGE: -UseCorrectExecutionOrderForVarargArguments +// WITH_RUNTIME +// KJS_WITH_FULL_RUNTIME + +fun foo(vararg x: Unit, y: Any) {} + +fun box(): String { + var acc1 = "" + var acc2 = "" + var acc3 = "" + var acc4 = "" + foo({ acc1 += "1" }(), y = { acc1 += "2" }()) + foo(x = *arrayOf({ acc2 += "1" }()), y = { acc2 += "2" }()) + foo(x = arrayOf({ acc3 += "1" }()), y = { acc3 += "2" }()) + foo(*arrayOf({ acc4 += "1" }()), y = { acc4 += "2" }()) + + return if (acc1 == "12" && acc2 == "21" && acc3 == "21" && acc4 == "12") "OK" else "ERROR" + return "OK" +} diff --git a/compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt b/compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt new file mode 100644 index 00000000000..48c994256fa --- /dev/null +++ b/compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt @@ -0,0 +1,18 @@ +// !LANGUAGE: +UseCorrectExecutionOrderForVarargArguments +// WITH_RUNTIME +// KJS_WITH_FULL_RUNTIME + +fun foo(vararg x: Unit, y: Any) {} + +fun box(): String { + var acc1 = "" + var acc2 = "" + var acc3 = "" + var acc4 = "" + foo({ acc1 += "1" }(), y = { acc1 += "2" }()) + foo(x = *arrayOf({ acc2 += "1" }()), y = { acc2 += "2" }()) + foo(x = arrayOf({ acc3 += "1" }()), y = { acc3 += "2" }()) + foo(*arrayOf({ acc4 += "1" }()), y = { acc4 += "2" }()) + + return if (acc1 == "12" && acc2 == "12" && acc3 == "12" && acc4 == "12") "OK" else "ERROR" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index a028a2c1375..d106e79bb1e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -355,6 +355,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/argumentOrder/extensionInClass.kt"); } + @TestMetadata("kt17691.kt") + public void testKt17691() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691.kt"); + } + + @TestMetadata("kt17691WithEnabledFeature.kt") + public void testKt17691WithEnabledFeature() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt"); + } + @TestMetadata("kt9277.kt") public void testKt9277() throws Exception { runTest("compiler/testData/codegen/box/argumentOrder/kt9277.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 61a12a1cbef..044129db114 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -355,6 +355,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/argumentOrder/extensionInClass.kt"); } + @TestMetadata("kt17691.kt") + public void testKt17691() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691.kt"); + } + + @TestMetadata("kt17691WithEnabledFeature.kt") + public void testKt17691WithEnabledFeature() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt"); + } + @TestMetadata("kt9277.kt") public void testKt9277() throws Exception { runTest("compiler/testData/codegen/box/argumentOrder/kt9277.kt"); diff --git a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt index 83b12a93a69..682b69ae7bd 100644 --- a/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt +++ b/compiler/util/src/org/jetbrains/kotlin/config/LanguageVersionSettings.kt @@ -140,6 +140,7 @@ enum class LanguageFeature( RequiredPrimaryConstructorDelegationCallInEnums(KOTLIN_1_5, kind = BUG_FIX), ForbidAnonymousReturnTypesInPrivateInlineFunctions(KOTLIN_1_5, kind = BUG_FIX), ForbidReferencingToUnderscoreNamedParameterOfCatchBlock(KOTLIN_1_5, kind = BUG_FIX), + UseCorrectExecutionOrderForVarargArguments(KOTLIN_1_5, kind = BUG_FIX), // Temporarily disabled, see KT-27084/KT-22379 SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX), diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 73a353dc16e..2ad1494708d 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -140,6 +140,16 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/argumentOrder/extensionInClass.kt"); } + @TestMetadata("kt17691.kt") + public void testKt17691() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691.kt"); + } + + @TestMetadata("kt17691WithEnabledFeature.kt") + public void testKt17691WithEnabledFeature() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt"); + } + @TestMetadata("kt9277.kt") public void testKt9277() throws Exception { runTest("compiler/testData/codegen/box/argumentOrder/kt9277.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index af695fcad09..f3d056cd111 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -140,6 +140,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/argumentOrder/extensionInClass.kt"); } + @TestMetadata("kt17691.kt") + public void testKt17691() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691.kt"); + } + + @TestMetadata("kt17691WithEnabledFeature.kt") + public void testKt17691WithEnabledFeature() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt"); + } + @TestMetadata("kt9277.kt") public void testKt9277() throws Exception { runTest("compiler/testData/codegen/box/argumentOrder/kt9277.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 0e422c9defa..deb24e6c0b1 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -140,6 +140,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/argumentOrder/extensionInClass.kt"); } + @TestMetadata("kt17691.kt") + public void testKt17691() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691.kt"); + } + + @TestMetadata("kt17691WithEnabledFeature.kt") + public void testKt17691WithEnabledFeature() throws Exception { + runTest("compiler/testData/codegen/box/argumentOrder/kt17691WithEnabledFeature.kt"); + } + @TestMetadata("kt9277.kt") public void testKt9277() throws Exception { runTest("compiler/testData/codegen/box/argumentOrder/kt9277.kt");