From 6b83f2d70e3a04072a671cbb85077e2c07b7907a Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Wed, 30 Sep 2020 22:22:48 +0000 Subject: [PATCH] [FIR] Remove Candidate.oldToNewArgumentMapping and use Candidate.argumentMapping instead of the Fir*Call's argumentList to remap vararg arguments to a FirVarargArgumentExpression. The argumentList was used to preserve the order, but we can rely on Candidate.argumentMapping to have its keys in order. --- .../kotlin/fir/resolve/calls/Candidate.kt | 1 - .../calls/FirArgumentsToParametersMapper.kt | 7 ++----- .../fir/resolve/calls/ResolutionStages.kt | 1 - ...rCallCompletionResultsWriterTransformer.kt | 19 +++++-------------- .../body/resolve/BodyResolveUtils.kt | 15 +++++++-------- 5 files changed, 14 insertions(+), 29 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Candidate.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Candidate.kt index fe761814ab6..3b28df7f6e4 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Candidate.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Candidate.kt @@ -104,7 +104,6 @@ class Candidate( var argumentMapping: Map? = null var numDefaults: Int = 0 - var oldToNewArgumentMapping: Map? = null lateinit var typeArgumentMapping: TypeArgumentMapping val postponedAtoms = mutableListOf() diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/FirArgumentsToParametersMapper.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/FirArgumentsToParametersMapper.kt index 4ac142f1eb8..1a9320cd304 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/FirArgumentsToParametersMapper.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/FirArgumentsToParametersMapper.kt @@ -32,7 +32,6 @@ data class ArgumentMapping( // 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, - val oldToNewArgumentMap: Map, val diagnostics: List ) { fun toArgumentToParameterMapping(): Map { @@ -53,7 +52,7 @@ data class ArgumentMapping( } } -private val EmptyArgumentMapping = ArgumentMapping(emptyMap(), emptyMap(), emptyList()) +private val EmptyArgumentMapping = ArgumentMapping(emptyMap(), emptyList()) fun BodyResolveComponents.mapArguments( arguments: List, @@ -72,7 +71,6 @@ fun BodyResolveComponents.mapArguments( // 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() if ((function as? FirSimpleFunction)?.isOperator == true && function.name == Name.identifier("set") && function.valueParameters.any { it.defaultValue != null || it.isVararg } @@ -86,7 +84,6 @@ fun BodyResolveComponents.mapArguments( name = function.valueParameters.last().name } argumentsInParenthesis = argumentsInParenthesis.dropLast(1) + listOf(namedV) - oldToNewArgumentMap[v] = namedV } } @@ -97,7 +94,7 @@ fun BodyResolveComponents.mapArguments( } processor.processDefaultsAndRunChecks() - return ArgumentMapping(processor.result, oldToNewArgumentMap, processor.diagnostics ?: emptyList()) + return ArgumentMapping(processor.result, processor.diagnostics ?: emptyList()) } private class FirCallArgumentsProcessor( diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt index 6cd0c402238..c184071e083 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt @@ -172,7 +172,6 @@ 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() 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 1090a5f4cc4..d8a5ae6ee4b 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 @@ -10,7 +10,6 @@ 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 @@ -166,15 +165,7 @@ 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.handleVarargs() subCandidate.argumentMapping?.let { result.replaceArgumentList(buildResolvedArgumentList(it)) } @@ -221,7 +212,7 @@ class FirCallCompletionResultsWriterTransformer( } } if (!calleeReference.isError) { - subCandidate.handleVarargs(annotationCall.argumentList) + subCandidate.handleVarargs() subCandidate.argumentMapping?.let { annotationCall.replaceArgumentList(buildResolvedArgumentList(it)) } @@ -229,14 +220,14 @@ class FirCallCompletionResultsWriterTransformer( return annotationCall.compose() } - private fun Candidate.handleVarargs(argumentList: FirArgumentList) { + private fun Candidate.handleVarargs() { val argumentMapping = this.argumentMapping val varargParameter = argumentMapping?.values?.firstOrNull { it.isVararg } if (varargParameter != null) { // Create a FirVarargArgumentExpression for the vararg arguments val varargParameterTypeRef = varargParameter.returnTypeRef val resolvedArrayType = varargParameterTypeRef.substitute(this) - this.argumentMapping = remapArgumentsWithVararg(varargParameter, resolvedArrayType, argumentList, argumentMapping) + this.argumentMapping = remapArgumentsWithVararg(varargParameter, resolvedArrayType, argumentMapping) } } @@ -367,7 +358,7 @@ class FirCallCompletionResultsWriterTransformer( val argumentsMapping = runIf(!calleeReference.isError) { calleeReference.candidate.createArgumentsMapping() } delegatedConstructorCall.argumentList.transformArguments(this, argumentsMapping) if (!calleeReference.isError) { - subCandidate.handleVarargs(delegatedConstructorCall.argumentList) + subCandidate.handleVarargs() subCandidate.argumentMapping?.let { delegatedConstructorCall.replaceArgumentList(buildResolvedArgumentList(it)) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt index 23176d1f985..8beb7ec520a 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/BodyResolveUtils.kt @@ -37,7 +37,6 @@ internal inline var FirExpression.resultType: FirTypeRef internal fun remapArgumentsWithVararg( varargParameter: FirValueParameter, varargArrayType: ConeKotlinType, - argumentList: FirArgumentList, argumentMapping: Map ): Map { // Create a FirVarargArgumentExpression for the vararg arguments. @@ -45,13 +44,14 @@ internal fun remapArgumentsWithVararg( // FIR2IR uses the mapping order to determine if arguments need to be reordered. val varargParameterTypeRef = varargParameter.returnTypeRef val varargElementType = varargArrayType.arrayElementType() - var indexAfterVarargs = argumentList.arguments.size + val argumentList = argumentMapping.keys.toList() + var indexAfterVarargs = argumentList.size val newArgumentMapping = mutableMapOf() val varargArgument = buildVarargArgumentsExpression { this.varargElementType = varargParameterTypeRef.withReplacedConeType(varargElementType) this.typeRef = varargParameterTypeRef.withReplacedConeType(varargArrayType) - for ((i, arg) in argumentList.arguments.withIndex()) { - val valueParameter = argumentMapping[arg] ?: continue + for ((i, arg) in argumentList.withIndex()) { + val valueParameter = argumentMapping.getValue(arg) if (valueParameter.isVararg) { // `arg` is a vararg argument. arguments += arg @@ -68,10 +68,9 @@ internal fun remapArgumentsWithVararg( 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 + for (i in indexAfterVarargs until argumentList.size) { + val arg = argumentList[i] + newArgumentMapping[arg] = argumentMapping.getValue(arg) } return newArgumentMapping }