[FIR] Handle varargs in overloaded indexed access operator.

This commit is contained in:
Mark Punzalan
2020-09-22 23:16:46 -07:00
committed by teamcityserver
parent c471a7735e
commit f6ce2d893c
14 changed files with 94 additions and 11 deletions
@@ -13011,6 +13011,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/increment/classNaryGetSet.kt");
}
@TestMetadata("classVarargGetSet.kt")
public void testClassVarargGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classVarargGetSet.kt");
}
@TestMetadata("classWithGetSet.kt")
public void testClassWithGetSet() throws Exception {
runTest("compiler/testData/codegen/box/increment/classWithGetSet.kt");
@@ -104,6 +104,7 @@ class Candidate(
var argumentMapping: Map<FirExpression, FirValueParameter>? = null
var numDefaults: Int = 0
var oldToNewArgumentMapping: Map<FirExpression, FirExpression>? = null
lateinit var typeArgumentMapping: TypeArgumentMapping
val postponedAtoms = mutableListOf<PostponedResolvedAtom>()
@@ -30,6 +30,7 @@ data class ArgumentMapping(
// foo(b = bar(), a = qux())
// parameterToCallArgumentMap.values() should be [ 'bar()', 'foo()' ]
val parameterToCallArgumentMap: Map<FirValueParameter, ResolvedCallArgument>,
val oldToNewArgumentMap: Map<FirExpression, FirExpression>,
val diagnostics: List<ResolutionDiagnostic>
) {
fun toArgumentToParameterMapping(): Map<FirExpression, FirValueParameter> {
@@ -50,7 +51,7 @@ data class ArgumentMapping(
}
}
private val EmptyArgumentMapping = ArgumentMapping(emptyMap(), emptyList())
private val EmptyArgumentMapping = ArgumentMapping(emptyMap(), emptyMap(), emptyList())
fun BodyResolveComponents.mapArguments(
arguments: List<FirExpression>,
@@ -67,11 +68,12 @@ fun BodyResolveComponents.mapArguments(
arguments.subList(0, arguments.size - 1)
}
// If this is an overloading indexed access operator, it could have default values in the middle.
// If this is an overloading indexed access operator, it could have default values or a vararg parameter in the middle.
// For proper argument mapping, wrap the last one, which is supposed to be the updated value, as a named argument.
val oldToNewArgumentMap = mutableMapOf<FirExpression, FirExpression>()
if ((function as? FirSimpleFunction)?.isOperator == true &&
function.name == Name.identifier("set") &&
function.valueParameters.any { it.defaultValue != null }
function.valueParameters.any { it.defaultValue != null || it.isVararg }
) {
val v = argumentsInParenthesis.last()
if (v !is FirNamedArgumentExpression) {
@@ -82,6 +84,7 @@ fun BodyResolveComponents.mapArguments(
name = function.valueParameters.last().name
}
argumentsInParenthesis = argumentsInParenthesis.dropLast(1) + listOf(namedV)
oldToNewArgumentMap[v] = namedV
}
}
@@ -92,7 +95,7 @@ fun BodyResolveComponents.mapArguments(
}
processor.processDefaultsAndRunChecks()
return ArgumentMapping(processor.result, processor.diagnostics ?: emptyList())
return ArgumentMapping(processor.result, oldToNewArgumentMap, processor.diagnostics ?: emptyList())
}
private class FirCallArgumentsProcessor(
@@ -172,6 +172,7 @@ internal object MapArguments : ResolutionStage() {
val mapping = context.bodyResolveComponents.mapArguments(callInfo.arguments, function, candidate.originScope)
candidate.argumentMapping = mapping.toArgumentToParameterMapping()
candidate.numDefaults = mapping.numDefaults()
candidate.oldToNewArgumentMapping = mapping.oldToNewArgumentMap
mapping.diagnostics.forEach(sink::reportDiagnostic)
sink.yieldIfNeed()
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.builder.buildArgumentList
import org.jetbrains.kotlin.fir.references.builder.buildErrorNamedReference
import org.jetbrains.kotlin.fir.references.builder.buildResolvedCallableReference
import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference
@@ -165,6 +166,14 @@ class FirCallCompletionResultsWriterTransformer(
val expectedArgumentsTypeMapping = runIf(!calleeReference.isError) { subCandidate.createArgumentsMapping() }
result.argumentList.transformArguments(this, expectedArgumentsTypeMapping)
if (!calleeReference.isError) {
subCandidate.oldToNewArgumentMapping?.let {
result.replaceArgumentList(buildArgumentList {
source = result.argumentList.source
for (oldArgument in result.argumentList.arguments) {
arguments += it[oldArgument] ?: oldArgument
}
})
}
subCandidate.handleVarargs(result.argumentList)
subCandidate.argumentMapping?.let {
result.replaceArgumentList(buildResolvedArgumentList(it))