[FIR] Keep vararg argument order in resolved calls (KT-17691).

This commit is contained in:
Mark Punzalan
2020-09-24 13:43:18 -07:00
committed by teamcityserver
parent f6ce2d893c
commit eb631bc429
14 changed files with 265 additions and 14 deletions
@@ -13016,6 +13016,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/increment/classVarargGetSet.kt");
}
@TestMetadata("classVarargGetSetEvaluationOrder.kt")
public void testClassVarargGetSetEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/increment/classVarargGetSetEvaluationOrder.kt");
}
@TestMetadata("classWithGetSet.kt")
public void testClassWithGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classWithGetSet.kt");
@@ -17861,6 +17866,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
public void testVarargs() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargs.kt");
}
@TestMetadata("varargsEvaluationOrder.kt")
public void testVarargsEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargsEvaluationOrder.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/multiDecl")
@@ -31536,6 +31546,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/vararg/emptyVarargOfBoxedPrimitiveType.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/vararg/evaluationOrder.kt");
}
@TestMetadata("kt1978.kt")
public void testKt1978() throws Exception {
runTest("compiler/testData/codegen/box/vararg/kt1978.kt");
@@ -29,6 +29,8 @@ data class ArgumentMapping(
// fun foo(a: Int, b: Int) {}
// foo(b = bar(), a = qux())
// parameterToCallArgumentMap.values() should be [ 'bar()', 'foo()' ]
// TODO: Consider changing this (and other similar declarations like Candidate.argumentMapping) to LinkedHashMap to signify that
// order is important. Right now we're assuming that mutableMapOf() will always return a LinkedHashMap.
val parameterToCallArgumentMap: Map<FirValueParameter, ResolvedCallArgument>,
val oldToNewArgumentMap: Map<FirExpression, FirExpression>,
val diagnostics: List<ResolutionDiagnostic>
@@ -40,10 +40,12 @@ internal fun remapArgumentsWithVararg(
argumentList: FirArgumentList,
argumentMapping: Map<FirExpression, FirValueParameter>
): Map<FirExpression, FirValueParameter> {
// Create a FirVarargArgumentExpression for the vararg arguments
// Create a FirVarargArgumentExpression for the vararg arguments.
// The order of arguments in the mapping must be preserved for FIR2IR, hence we have to find where the vararg arguments end.
// FIR2IR uses the mapping order to determine if arguments need to be reordered.
val varargParameterTypeRef = varargParameter.returnTypeRef
val varargElementType = varargArrayType.arrayElementType()
var firstIndex = argumentList.arguments.size
var indexAfterVarargs = argumentList.arguments.size
val newArgumentMapping = mutableMapOf<FirExpression, FirValueParameter>()
val varargArgument = buildVarargArgumentsExpression {
this.varargElementType = varargParameterTypeRef.withReplacedConeType(varargElementType)
@@ -51,14 +53,26 @@ internal fun remapArgumentsWithVararg(
for ((i, arg) in argumentList.arguments.withIndex()) {
val valueParameter = argumentMapping[arg] ?: continue
if (valueParameter.isVararg) {
firstIndex = min(firstIndex, i)
// `arg` is a vararg argument.
arguments += arg
} else {
} else if (arguments.isEmpty()) {
// `arg` is BEFORE the vararg arguments.
newArgumentMapping[arg] = valueParameter
} else {
// `arg` is AFTER the vararg arguments.
indexAfterVarargs = i
break
}
}
}
newArgumentMapping[varargArgument] = varargParameter
// Add mapping for arguments after the vararg arguments, if any.
for (i in indexAfterVarargs until argumentList.arguments.size) {
val arg = argumentList.arguments[i]
val valueParameter = argumentMapping[arg] ?: continue
newArgumentMapping[arg] = valueParameter
}
return newArgumentMapping
}
@@ -0,0 +1,41 @@
// IGNORE_BACKEND_FIR: JVM_IR
// Currently fails as the indices (`va`) are evaluated twice (on get and set).
var log = ""
fun logged(value: String) =
value.also { log += value }
object A {
var x = ""
var gets = ""
var sets = ""
operator fun get(vararg va: String): String {
for (s in va) {
gets += s
}
log += "get;"
return x
}
operator fun set(vararg va: String, value: String) {
for (s in va) {
sets += s
}
log += "set;"
x = value
}
}
operator fun String.inc() = this + logged("inc;")
fun box(): String {
A.x = "start;"
val xx = A[logged("1;"), logged("2;"), logged("3;")]++
if (xx != "start;") return "Failed xx: $xx"
if (A.x != "start;inc;") return "Failed A.x: ${A.x}"
if (A.gets != "1;2;3;") return "Failed A.gets: ${A.gets}"
if (A.sets != "1;2;3;") return "Failed A.sets: ${A.sets}"
if (log != "1;2;3;get;inc;set;") return "Failed log: $log"
return "OK"
}
@@ -19,6 +19,7 @@ fun foo3(
) = "$p1 $p2 ${p3[0].toInt()} ${p3[1].toInt()}"
fun box(): String {
if (foo1(1, 2, p2 = "3", p3 = 4.0) != "1 2 3 4") return "fail 1"
if (foo1(p1 = *intArrayOf(1, 2), "3", p3 = 4.0) != "1 2 3 4") return "fail 2"
if (foo2(p1 = 1, "2", "3", p3 = 4.0) != "1 2 3 4") return "fail 3"
@@ -0,0 +1,46 @@
// !LANGUAGE: +NewInference +MixedNamedArgumentsInTheirOwnPosition
// IGNORE_BACKEND: JVM
// See KT-17691: Wrong argument order in resolved call with varargs. (fixed in JVM_IR)
var log = ""
fun <T> logged(value: T): T =
value.also { log += " " + value }
fun doTest(id: String, expected: String, expectedLog: String, test: () -> String) {
log = ""
val actual = test()
if (actual != expected) throw AssertionError("$id expected: $expected, actual: $actual")
if (log != expectedLog) throw AssertionError("$id expectedLog: $expectedLog, actual: $log")
}
fun foo1(
vararg p1: Int,
p2: String,
p3: Double
) = "${p1[0]} ${p1[1]} $p2 ${p3.toInt()}"
fun foo2(
p1: Int,
vararg p2: String,
p3: Double
) = "$p1 ${p2[0]} ${p2[1]} ${p3.toInt()}"
fun foo3(
p1: Int,
p2: String,
vararg p3: Double
) = "$p1 $p2 ${p3[0].toInt()} ${p3[1].toInt()}"
fun box(): String {
doTest("test1", "1 2 3 4", " 1 2 3 4.5") { foo1(logged(1), logged(2), p2 = logged("3"), p3 = logged(4.5)) }
doTest("test2", "1 2 3 4", " 1 2 3 4.5") { foo1(p1 = *intArrayOf(logged(1), logged(2)), logged("3"), p3 = logged(4.5)) }
doTest("test3", "1 2 3 4", " 1 2 3 4.5") { foo2(p1 = logged(1), logged("2"), logged("3"), p3 = logged(4.5)) }
doTest("test4", "1 2 3 4", " 1 2 3 4.5") { foo2(logged(1), p2 = *arrayOf(logged("2"), logged("3")), logged(4.5)) }
doTest("test5", "1 2 3 4", " 1 2 3.5 4.5") { foo3(p1 = logged(1), logged("2"), logged(3.5), logged(4.5)) }
doTest("test6", "1 2 3 4", " 1 2 3.5 4.5") { foo3(p1 = logged(1), logged("2"), p3 = *doubleArrayOf(logged(3.5), logged(4.5))) }
return "OK"
}
+45
View File
@@ -0,0 +1,45 @@
var log = ""
fun <T> logged(value: T): T =
value.also { log += value }
fun doTest(id: String, expected: String, expectedLog: String, test: () -> String) {
log = ""
val actual = test()
if (actual != expected) throw AssertionError("$id expected: $expected, actual: $actual")
if (log != expectedLog) throw AssertionError("$id expectedLog: $expectedLog, actual: $log")
}
object A {
var sets = ""
fun f(vararg va: String, a: String = "default_a;", b: String): String {
var ret = ""
for (s in va) {
ret += s
}
return ret + a + b
}
operator fun set(vararg va: String, value: String) {
for (s in va) {
sets += s
}
log += "set=$value;"
}
}
operator fun String.inc() = this + logged("inc;")
fun box(): String {
doTest("test1", "1;2;3;", "1;2;3;value;set=value;;") {
A[logged("1;"), logged("2;"), logged("3;")] = logged("value;")
A.sets
}
doTest("test2", "1;2;3;default_a;b;", "1;2;3;b;") { A.f(logged("1;"), logged("2;"), logged("3;"), b = logged("b;")) }
doTest("test3", "1;2;3;a;b;", "1;2;3;b;a;") { A.f(logged("1;"), logged("2;"), logged("3;"), b = logged("b;"), a = logged("a;")) }
doTest("test4", "1;2;3;a;b;", "1;2;3;a;b;") { A.f(logged("1;"), logged("2;"), logged("3;"), a = logged("a;"), b = logged("b;")) }
return "OK"
}
+7 -10
View File
@@ -26,13 +26,10 @@ FILE fqName:<root> fileName:/kt28456a.kt
FUN name:testSimpleAssignment visibility:public modality:FINAL <> (a:<root>.A) returnType:kotlin.Unit
VALUE_PARAMETER name:a index:0 type:<root>.A
BLOCK_BODY
BLOCK type=kotlin.Unit origin=ARGUMENTS_REORDERING_FOR_CALL
VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:kotlin.IntArray [val]
VARARG type=kotlin.IntArray varargElementType=kotlin.Int
CONST Int type=kotlin.Int value=1
CONST Int type=kotlin.Int value=2
CONST Int type=kotlin.Int value=3
CALL 'public final fun set (vararg i: kotlin.Int, v: kotlin.Int): kotlin.Unit [operator] declared in <root>' type=kotlin.Unit origin=null
$receiver: GET_VAR 'a: <root>.A declared in <root>.testSimpleAssignment' type=<root>.A origin=null
i: GET_VAR 'val tmp_0: kotlin.IntArray [val] declared in <root>.testSimpleAssignment' type=kotlin.IntArray origin=null
v: CONST Int type=kotlin.Int value=0
CALL 'public final fun set (vararg i: kotlin.Int, v: kotlin.Int): kotlin.Unit [operator] declared in <root>' type=kotlin.Unit origin=null
$receiver: GET_VAR 'a: <root>.A declared in <root>.testSimpleAssignment' type=<root>.A origin=null
i: VARARG type=kotlin.IntArray varargElementType=kotlin.Int
CONST Int type=kotlin.Int value=1
CONST Int type=kotlin.Int value=2
CONST Int type=kotlin.Int value=3
v: CONST Int type=kotlin.Int value=0
@@ -14411,6 +14411,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/increment/classVarargGetSet.kt");
}
@TestMetadata("classVarargGetSetEvaluationOrder.kt")
public void testClassVarargGetSetEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/increment/classVarargGetSetEvaluationOrder.kt");
}
@TestMetadata("classWithGetSet.kt")
public void testClassWithGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classWithGetSet.kt");
@@ -19256,6 +19261,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
public void testVarargs() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargs.kt");
}
@TestMetadata("varargsEvaluationOrder.kt")
public void testVarargsEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargsEvaluationOrder.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/multiDecl")
@@ -33302,6 +33312,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/vararg/emptyVarargOfBoxedPrimitiveType.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/vararg/evaluationOrder.kt");
}
@TestMetadata("kt1978.kt")
public void testKt1978() throws Exception {
runTest("compiler/testData/codegen/box/vararg/kt1978.kt");
@@ -14411,6 +14411,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/increment/classVarargGetSet.kt");
}
@TestMetadata("classVarargGetSetEvaluationOrder.kt")
public void testClassVarargGetSetEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/increment/classVarargGetSetEvaluationOrder.kt");
}
@TestMetadata("classWithGetSet.kt")
public void testClassWithGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classWithGetSet.kt");
@@ -19234,6 +19239,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class MixedNamedPosition extends AbstractLightAnalysisModeTest {
@TestMetadata("varargsEvaluationOrder.kt")
public void ignoreVarargsEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargsEvaluationOrder.kt");
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
@@ -30936,6 +30946,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/vararg/emptyVarargOfBoxedPrimitiveType.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/vararg/evaluationOrder.kt");
}
@TestMetadata("kt1978.kt")
public void testKt1978() throws Exception {
runTest("compiler/testData/codegen/box/vararg/kt1978.kt");
@@ -13016,6 +13016,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/increment/classVarargGetSet.kt");
}
@TestMetadata("classVarargGetSetEvaluationOrder.kt")
public void testClassVarargGetSetEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/increment/classVarargGetSetEvaluationOrder.kt");
}
@TestMetadata("classWithGetSet.kt")
public void testClassWithGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classWithGetSet.kt");
@@ -17861,6 +17866,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
public void testVarargs() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargs.kt");
}
@TestMetadata("varargsEvaluationOrder.kt")
public void testVarargsEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargsEvaluationOrder.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/multiDecl")
@@ -31536,6 +31546,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/vararg/emptyVarargOfBoxedPrimitiveType.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/vararg/evaluationOrder.kt");
}
@TestMetadata("kt1978.kt")
public void testKt1978() throws Exception {
runTest("compiler/testData/codegen/box/vararg/kt1978.kt");
@@ -11161,6 +11161,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/increment/classVarargGetSet.kt");
}
@TestMetadata("classVarargGetSetEvaluationOrder.kt")
public void testClassVarargGetSetEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/increment/classVarargGetSetEvaluationOrder.kt");
}
@TestMetadata("classWithGetSet.kt")
public void testClassWithGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classWithGetSet.kt");
@@ -14386,6 +14391,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
public void testVarargs() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargs.kt");
}
@TestMetadata("varargsEvaluationOrder.kt")
public void testVarargsEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargsEvaluationOrder.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/multiDecl")
@@ -25672,6 +25682,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/vararg/doNotCopyImmediatelyCreatedArrays.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/vararg/evaluationOrder.kt");
}
@TestMetadata("kt1978.kt")
public void testKt1978() throws Exception {
runTest("compiler/testData/codegen/box/vararg/kt1978.kt");
@@ -11161,6 +11161,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/increment/classVarargGetSet.kt");
}
@TestMetadata("classVarargGetSetEvaluationOrder.kt")
public void testClassVarargGetSetEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/increment/classVarargGetSetEvaluationOrder.kt");
}
@TestMetadata("classWithGetSet.kt")
public void testClassWithGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classWithGetSet.kt");
@@ -14386,6 +14391,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
public void testVarargs() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargs.kt");
}
@TestMetadata("varargsEvaluationOrder.kt")
public void testVarargsEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargsEvaluationOrder.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/multiDecl")
@@ -25672,6 +25682,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/vararg/doNotCopyImmediatelyCreatedArrays.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/vararg/evaluationOrder.kt");
}
@TestMetadata("kt1978.kt")
public void testKt1978() throws Exception {
runTest("compiler/testData/codegen/box/vararg/kt1978.kt");
@@ -11226,6 +11226,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/increment/classVarargGetSet.kt");
}
@TestMetadata("classVarargGetSetEvaluationOrder.kt")
public void testClassVarargGetSetEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/increment/classVarargGetSetEvaluationOrder.kt");
}
@TestMetadata("classWithGetSet.kt")
public void testClassWithGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classWithGetSet.kt");
@@ -14451,6 +14456,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
public void testVarargs() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargs.kt");
}
@TestMetadata("varargsEvaluationOrder.kt")
public void testVarargsEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/mixedNamedPosition/varargsEvaluationOrder.kt");
}
}
@TestMetadata("compiler/testData/codegen/box/multiDecl")
@@ -25687,6 +25697,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/vararg/doNotCopyImmediatelyCreatedArrays.kt");
}
@TestMetadata("evaluationOrder.kt")
public void testEvaluationOrder() throws Exception {
runTest("compiler/testData/codegen/box/vararg/evaluationOrder.kt");
}
@TestMetadata("kt1978.kt")
public void testKt1978() throws Exception {
runTest("compiler/testData/codegen/box/vararg/kt1978.kt");