From 8bab20832278f5143e0c4806c11b2156a404b4d0 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 11 Feb 2021 11:28:20 +0300 Subject: [PATCH] FIR2IR: use information about callable reference adaptation from resolve --- .../backend/generators/AdapterGenerator.kt | 109 ++++++++---------- .../calls/CallableReferenceResolution.kt | 7 +- .../kotlin/fir/resolve/calls/Candidate.kt | 8 ++ .../calls/FirArgumentsToParametersMapper.kt | 1 - ...rCallCompletionResultsWriterTransformer.kt | 7 +- .../FirResolvedCallableReference.kt | 2 + .../FirResolvedCallableReferenceBuilder.kt | 3 + .../impl/FirResolvedCallableReferenceImpl.kt | 2 + .../fir/resolve/calls/ResolvedCallArgument.kt | 7 +- .../fir/tree/generator/NodeConfigurator.kt | 1 + .../kotlin/fir/tree/generator/Types.kt | 2 + .../adaptedReferences/toStringNoReflect.kt | 1 - ...bleReferencesNotEqualToCallablesFromAPI.kt | 1 - .../adaptedExtensionFunctions.fir.kt.txt | 15 ++- .../adaptedExtensionFunctions.fir.txt | 27 ++++- .../callableReferences/kt37131.fir.kt.txt | 5 +- .../callableReferences/kt37131.fir.txt | 6 +- ...erReferenceWithAdaptedArguments.fir.kt.txt | 2 +- ...emberReferenceWithAdaptedArguments.fir.txt | 2 + .../withAdaptedArguments.fir.kt.txt | 15 ++- .../withAdaptedArguments.fir.txt | 20 +++- .../withVarargViewedAsArray.fir.kt.txt | 5 +- .../withVarargViewedAsArray.fir.txt | 9 +- 23 files changed, 169 insertions(+), 88 deletions(-) rename compiler/fir/{resolve => tree}/src/org/jetbrains/kotlin/fir/resolve/calls/ResolvedCallArgument.kt (77%) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt index df955f46770..b17d0bceec7 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/AdapterGenerator.kt @@ -12,7 +12,10 @@ import org.jetbrains.kotlin.fir.backend.convertWithOffsets import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.impl.FirNoReceiverExpression +import org.jetbrains.kotlin.fir.references.FirResolvedCallableReference import org.jetbrains.kotlin.fir.render +import org.jetbrains.kotlin.fir.resolve.calls.FirFakeArgumentForCallableReference +import org.jetbrains.kotlin.fir.resolve.calls.ResolvedCallArgument import org.jetbrains.kotlin.fir.resolve.inference.* import org.jetbrains.kotlin.fir.types.ConeClassLikeType import org.jetbrains.kotlin.fir.types.ConeKotlinType @@ -29,10 +32,7 @@ import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl -import org.jetbrains.kotlin.ir.types.IrSimpleType -import org.jetbrains.kotlin.ir.types.IrType -import org.jetbrains.kotlin.ir.types.isUnit -import org.jetbrains.kotlin.ir.types.typeOrNull +import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.name.Name @@ -58,7 +58,7 @@ internal class AdapterGenerator( function: IrFunction ): Boolean = needSuspendConversion(type, function) || needCoercionToUnit(type, function) || - needVarargSpread(callableReferenceAccess, type, function) + needVarargSpread(callableReferenceAccess) /** * For example, @@ -96,26 +96,11 @@ internal class AdapterGenerator( * * At the use site, instead of referenced, we can put the adapter: { a, b -> referenced(a, b) } */ - private fun needVarargSpread( - callableReferenceAccess: FirCallableReferenceAccess, - type: IrSimpleType, - function: IrFunction - ): Boolean { + private fun needVarargSpread(callableReferenceAccess: FirCallableReferenceAccess): Boolean { // Unbound callable reference 'A::foo' - val shift = if (callableReferenceAccess.explicitReceiver is FirResolvedQualifier) 1 else 0 - val typeArguments = type.arguments - // Drop the return type from type arguments - val expectedParameterSize = typeArguments.size - 1 - shift - if (expectedParameterSize < function.valueParameters.size) { - return false - } - var hasSpreadCase = false - function.valueParameters.forEachIndexed { index, irValueParameter -> - if (irValueParameter.isVararg && typeArguments[shift + index] == irValueParameter.varargElementType) { - hasSpreadCase = true - } - } - return hasSpreadCase + return (callableReferenceAccess.calleeReference as? FirResolvedCallableReference)?.mappedArguments?.any { + it.value is ResolvedCallArgument.VarargArgument || it.value is ResolvedCallArgument.DefaultArgument + } == true } internal fun ConeKotlinType.kFunctionTypeToFunctionType(): IrSimpleType = @@ -138,7 +123,7 @@ internal class AdapterGenerator( callableReferenceAccess, startOffset, endOffset, firAdaptee!!, adaptee, type, boundDispatchReceiver, boundExtensionReceiver ) val irCall = createAdapteeCallForCallableReference( - callableReferenceAccess, adapteeSymbol, irAdapterFunction, boundDispatchReceiver, boundExtensionReceiver + callableReferenceAccess, firAdaptee, adapteeSymbol, irAdapterFunction, boundDispatchReceiver, boundExtensionReceiver ) irAdapterFunction.body = irFactory.createBlockBody(startOffset, endOffset) { if (expectedReturnType?.isUnit() == true) { @@ -268,6 +253,7 @@ internal class AdapterGenerator( private fun createAdapteeCallForCallableReference( callableReferenceAccess: FirCallableReferenceAccess, + firAdaptee: FirFunction<*>, adapteeSymbol: IrFunctionSymbol, adapterFunction: IrFunction, boundDispatchReceiver: IrExpression?, @@ -296,6 +282,7 @@ internal class AdapterGenerator( } var adapterParameterIndex = 0 + var parameterShift = 0 if (boundDispatchReceiver != null || boundExtensionReceiver != null) { val receiverValue = IrGetValueImpl( startOffset, endOffset, adapterFunction.extensionReceiverParameter!!.symbol, IrStatementOrigin.ADAPTED_FUNCTION_REFERENCE @@ -312,50 +299,56 @@ internal class AdapterGenerator( ) if (adapteeFunction.extensionReceiverParameter != null) { irCall.extensionReceiver = adaptedReceiverValue - adapterParameterIndex++ } else { irCall.dispatchReceiver = adaptedReceiverValue } + parameterShift++ } - adapteeFunction.valueParameters.mapIndexed { index, valueParameter -> - when { - valueParameter.isVararg -> { - if (adapterFunction.valueParameters.size <= index) { - irCall.putValueArgument(index, null) + val mappedArguments = (callableReferenceAccess.calleeReference as? FirResolvedCallableReference)?.mappedArguments + + fun buildIrGetValueArgument(argument: FirExpression): IrGetValue { + val parameterIndex = (argument as? FirFakeArgumentForCallableReference)?.index ?: adapterParameterIndex + adapterParameterIndex++ + return adapterFunction.valueParameters[parameterIndex + parameterShift].toIrGetValue(startOffset, endOffset) + } + + adapteeFunction.valueParameters.zip(firAdaptee.valueParameters).mapIndexed { index, (valueParameter, firParameter) -> + when (val mappedArgument = mappedArguments?.get(firParameter)) { + is ResolvedCallArgument.VarargArgument -> { + val valueArgument = if (mappedArgument.arguments.isEmpty()) { + null } else { - val adaptedValueArgument = - IrVarargImpl(startOffset, endOffset, valueParameter.type, valueParameter.varargElementType!!) - var neitherArrayNorSpread = false - while (adapterParameterIndex < adapterFunction.valueParameters.size) { - val irValueArgument = - adapterFunction.valueParameters[adapterParameterIndex].toIrGetValue(startOffset, endOffset) - if (irValueArgument.type == valueParameter.type) { - adaptedValueArgument.addElement(IrSpreadElementImpl(startOffset, endOffset, irValueArgument)) - adapterParameterIndex++ - break - } else if (irValueArgument.type == valueParameter.varargElementType) { - adaptedValueArgument.addElement(irValueArgument) - adapterParameterIndex++ - } else { - neitherArrayNorSpread = true - break - } - } - if (neitherArrayNorSpread) { - irCall.putValueArgument(index, null) - } else { - irCall.putValueArgument(index, adaptedValueArgument) + val adaptedValueArgument = IrVarargImpl( + startOffset, endOffset, + valueParameter.type, valueParameter.varargElementType!!, + ) + for (argument in mappedArgument.arguments) { + val irValueArgument = buildIrGetValueArgument(argument) + adaptedValueArgument.addElement(irValueArgument) } + adaptedValueArgument } + irCall.putValueArgument(index, valueArgument) } - valueParameter.hasDefaultValue() -> { + ResolvedCallArgument.DefaultArgument -> { irCall.putValueArgument(index, null) } - else -> { - irCall.putValueArgument( - index, adapterFunction.valueParameters[adapterParameterIndex++].toIrGetValue(startOffset, endOffset) - ) + is ResolvedCallArgument.SimpleArgument -> { + val irValueArgument = buildIrGetValueArgument(mappedArgument.callArgument) + if (valueParameter.isVararg) { + irCall.putValueArgument( + index, IrVarargImpl( + startOffset, endOffset, + valueParameter.type, valueParameter.varargElementType!!, + listOf(IrSpreadElementImpl(startOffset, endOffset, irValueArgument)) + ) + ) + } else { + irCall.putValueArgument(index, irValueArgument) + } + } + null -> { } } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt index c7c0e2ce9a4..b2aa2187ffe 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CallableReferenceResolution.kt @@ -51,8 +51,7 @@ internal object CheckCallableReferenceExpectedType : CheckerStage() { } candidate.resultingTypeForCallableReference = resultingType - candidate.usesSuspendConversion = - callableReferenceAdaptation?.suspendConversionStrategy == SuspendConversionStrategy.SUSPEND_CONVERSION + candidate.callableReferenceAdaptation = callableReferenceAdaptation candidate.outerConstraintBuilderEffect = fun ConstraintSystemOperation.() { addOtherSystem(candidate.system.asReadOnlyStorage()) @@ -133,7 +132,7 @@ internal class CallableReferenceAdaptation( val argumentTypes: Array, val coercionStrategy: CoercionStrategy, val defaults: Int, - val mappedArguments: Map, + val mappedArguments: CallableReferenceMappedArguments, val suspendConversionStrategy: SuspendConversionStrategy ) @@ -351,7 +350,7 @@ private fun createFakeArgumentsForReference( } } -private class FirFakeArgumentForCallableReference( +class FirFakeArgumentForCallableReference( val index: Int ) : FirExpression() { override val source: FirSourceElement? 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 91096caae85..03c49b1ed0d 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 @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeTypeVariable import org.jetbrains.kotlin.fir.types.FirTypeProjection import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.calls.components.SuspendConversionStrategy import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemOperation import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage @@ -97,6 +98,13 @@ class Candidate( var resultingTypeForCallableReference: ConeKotlinType? = null var outerConstraintBuilderEffect: (ConstraintSystemOperation.() -> Unit)? = null var usesSAM: Boolean = false + + internal var callableReferenceAdaptation: CallableReferenceAdaptation? = null + set(value) { + field = value + usesSuspendConversion = value?.suspendConversionStrategy == SuspendConversionStrategy.SUSPEND_CONVERSION + } + var usesSuspendConversion: Boolean = false var argumentMapping: LinkedHashMap? = null 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 c709fdfb012..e77990954d1 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 @@ -18,7 +18,6 @@ import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents import org.jetbrains.kotlin.fir.resolve.defaultParameterResolver import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.name.Name -import java.util.* import kotlin.collections.ArrayList import kotlin.collections.LinkedHashMap import kotlin.collections.component1 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 c6ae1f159bc..fe7d1d2ad96 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 @@ -14,10 +14,8 @@ import org.jetbrains.kotlin.fir.references.builder.buildErrorNamedReference import org.jetbrains.kotlin.fir.references.builder.buildResolvedCallableReference import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference import org.jetbrains.kotlin.fir.resolve.* -import org.jetbrains.kotlin.fir.resolve.calls.Candidate -import org.jetbrains.kotlin.fir.resolve.calls.FirErrorReferenceWithCandidate -import org.jetbrains.kotlin.fir.resolve.calls.FirNamedReferenceWithCandidate -import org.jetbrains.kotlin.fir.resolve.calls.varargElementType +import org.jetbrains.kotlin.fir.resolve.calls.* +import org.jetbrains.kotlin.fir.resolve.calls.CallableReferenceAdaptation import org.jetbrains.kotlin.fir.resolve.dfa.FirDataFlowAnalyzer import org.jetbrains.kotlin.fir.resolve.inference.* import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor @@ -295,6 +293,7 @@ class FirCallCompletionResultsWriterTransformer( name = calleeReference.name resolvedSymbol = calleeReference.candidateSymbol inferredTypeArguments.addAll(computeTypeArgumentTypes(calleeReference.candidate)) + mappedArguments = subCandidate.callableReferenceAdaptation?.mappedArguments ?: emptyMap() }, ).transformDispatchReceiver(StoreReceiver, subCandidate.dispatchReceiverExpression()) .transformExtensionReceiver(StoreReceiver, subCandidate.extensionReceiverExpression()) diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/FirResolvedCallableReference.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/FirResolvedCallableReference.kt index aa5572da77e..37456c2481e 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/FirResolvedCallableReference.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/FirResolvedCallableReference.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.fir.references import org.jetbrains.kotlin.fir.FirSourceElement +import org.jetbrains.kotlin.fir.resolve.calls.CallableReferenceMappedArguments import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.name.Name @@ -22,6 +23,7 @@ abstract class FirResolvedCallableReference : FirResolvedNamedReference() { abstract override val candidateSymbol: AbstractFirBasedSymbol<*>? abstract override val resolvedSymbol: AbstractFirBasedSymbol<*> abstract val inferredTypeArguments: List + abstract val mappedArguments: CallableReferenceMappedArguments override fun accept(visitor: FirVisitor, data: D): R = visitor.visitResolvedCallableReference(this, data) } diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/builder/FirResolvedCallableReferenceBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/builder/FirResolvedCallableReferenceBuilder.kt index 443cc771a61..35c28b7c25c 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/builder/FirResolvedCallableReferenceBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/builder/FirResolvedCallableReferenceBuilder.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.builder.FirBuilderDsl import org.jetbrains.kotlin.fir.references.FirResolvedCallableReference import org.jetbrains.kotlin.fir.references.impl.FirResolvedCallableReferenceImpl +import org.jetbrains.kotlin.fir.resolve.calls.CallableReferenceMappedArguments import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.visitors.* @@ -26,6 +27,7 @@ class FirResolvedCallableReferenceBuilder { lateinit var name: Name lateinit var resolvedSymbol: AbstractFirBasedSymbol<*> val inferredTypeArguments: MutableList = mutableListOf() + lateinit var mappedArguments: CallableReferenceMappedArguments fun build(): FirResolvedCallableReference { return FirResolvedCallableReferenceImpl( @@ -33,6 +35,7 @@ class FirResolvedCallableReferenceBuilder { name, resolvedSymbol, inferredTypeArguments, + mappedArguments, ) } diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/impl/FirResolvedCallableReferenceImpl.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/impl/FirResolvedCallableReferenceImpl.kt index 965d200deb5..bd5b475725f 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/impl/FirResolvedCallableReferenceImpl.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/references/impl/FirResolvedCallableReferenceImpl.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.references.impl import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.references.FirResolvedCallableReference +import org.jetbrains.kotlin.fir.resolve.calls.CallableReferenceMappedArguments import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.name.Name @@ -22,6 +23,7 @@ internal class FirResolvedCallableReferenceImpl( override val name: Name, override val resolvedSymbol: AbstractFirBasedSymbol<*>, override val inferredTypeArguments: MutableList, + override val mappedArguments: CallableReferenceMappedArguments, ) : FirResolvedCallableReference() { override val candidateSymbol: AbstractFirBasedSymbol<*>? get() = null diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolvedCallArgument.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/resolve/calls/ResolvedCallArgument.kt similarity index 77% rename from compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolvedCallArgument.kt rename to compiler/fir/tree/src/org/jetbrains/kotlin/fir/resolve/calls/ResolvedCallArgument.kt index 29f483c3bd5..e8d3d9f033a 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolvedCallArgument.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/resolve/calls/ResolvedCallArgument.kt @@ -1,10 +1,11 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.fir.resolve.calls +import org.jetbrains.kotlin.fir.declarations.FirValueParameter import org.jetbrains.kotlin.fir.expressions.FirExpression sealed class ResolvedCallArgument { @@ -23,4 +24,6 @@ sealed class ResolvedCallArgument { } class VarargArgument(override val arguments: List) : ResolvedCallArgument() -} \ No newline at end of file +} + +typealias CallableReferenceMappedArguments = Map \ No newline at end of file diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt index d655c4b4282..c14d0f0b7f0 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/NodeConfigurator.kt @@ -535,6 +535,7 @@ object NodeConfigurator : AbstractFieldConfigurator(FirTreeBuild resolvedCallableReference.configure { +fieldList("inferredTypeArguments", coneKotlinTypeType) + +field("mappedArguments", callableReferenceMappedArgumentsType) } delegateFieldReference.configure { diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/Types.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/Types.kt index 38e16b2f88b..a619f80682d 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/Types.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/Types.kt @@ -86,3 +86,5 @@ val declarationAttributesType = generatedType("declarations", "FirDeclarationAtt val annotationResolveStatusType = generatedType("expressions", "FirAnnotationResolveStatus") val exhaustivenessStatusType = generatedType("expressions", "ExhaustivenessStatus") + +val callableReferenceMappedArgumentsType = type("fir.resolve.calls", "CallableReferenceMappedArguments") \ No newline at end of file diff --git a/compiler/testData/codegen/box/callableReference/adaptedReferences/toStringNoReflect.kt b/compiler/testData/codegen/box/callableReference/adaptedReferences/toStringNoReflect.kt index 364e67296f6..1d0f6870953 100644 --- a/compiler/testData/codegen/box/callableReference/adaptedReferences/toStringNoReflect.kt +++ b/compiler/testData/codegen/box/callableReference/adaptedReferences/toStringNoReflect.kt @@ -1,5 +1,4 @@ // TARGET_BACKEND: JVM -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME package test diff --git a/compiler/testData/codegen/box/reflection/methodsFromAny/adaptedCallableReferencesNotEqualToCallablesFromAPI.kt b/compiler/testData/codegen/box/reflection/methodsFromAny/adaptedCallableReferencesNotEqualToCallablesFromAPI.kt index ee56bd23817..cf9dcb4463b 100644 --- a/compiler/testData/codegen/box/reflection/methodsFromAny/adaptedCallableReferencesNotEqualToCallablesFromAPI.kt +++ b/compiler/testData/codegen/box/reflection/methodsFromAny/adaptedCallableReferencesNotEqualToCallablesFromAPI.kt @@ -1,5 +1,4 @@ // TARGET_BACKEND: JVM -// IGNORE_BACKEND_FIR: JVM_IR // WITH_REFLECT import kotlin.reflect.* diff --git a/compiler/testData/ir/irText/expressions/callableReferences/adaptedExtensionFunctions.fir.kt.txt b/compiler/testData/ir/irText/expressions/callableReferences/adaptedExtensionFunctions.fir.kt.txt index 0dd8e57a20b..b6f65c65d60 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/adaptedExtensionFunctions.fir.kt.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/adaptedExtensionFunctions.fir.kt.txt @@ -20,13 +20,22 @@ fun C.extensionBoth(i: Int, s: String = "", vararg t: String) { } fun testExtensionVararg() { - use(f = ::extensionVararg) + use(f = local fun extensionVararg(p0: C, p1: Int) { + p0.extensionVararg(i = p1) + } +) } fun testExtensionDefault() { - use(f = ::extensionDefault) + use(f = local fun extensionDefault(p0: C, p1: Int) { + p0.extensionDefault(i = p1) + } +) } fun testExtensionBoth() { - use(f = ::extensionBoth) + use(f = local fun extensionBoth(p0: C, p1: Int) { + p0.extensionBoth(i = p1) + } +) } diff --git a/compiler/testData/ir/irText/expressions/callableReferences/adaptedExtensionFunctions.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/adaptedExtensionFunctions.fir.txt index 7cacf363154..318a0636e69 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/adaptedExtensionFunctions.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/adaptedExtensionFunctions.fir.txt @@ -44,12 +44,33 @@ FILE fqName: fileName:/adaptedExtensionFunctions.kt FUN name:testExtensionVararg visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY CALL 'public final fun use (f: @[ExtensionFunctionType] kotlin.Function2<.C, kotlin.Int, kotlin.Unit>): kotlin.Unit declared in ' type=kotlin.Unit origin=null - f: FUNCTION_REFERENCE 'public final fun extensionVararg (i: kotlin.Int, vararg s: kotlin.String): kotlin.Unit declared in ' type=kotlin.reflect.KFunction2<.C, kotlin.Int, kotlin.Unit> origin=null reflectionTarget= + f: FUN_EXPR type=kotlin.Function2<.C, kotlin.Int, kotlin.Unit> origin=ADAPTED_FUNCTION_REFERENCE + FUN ADAPTER_FOR_CALLABLE_REFERENCE name:extensionVararg visibility:local modality:FINAL <> (p0:.C, p1:kotlin.Int) returnType:kotlin.Unit + VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:.C + VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p1 index:1 type:kotlin.Int + BLOCK_BODY + CALL 'public final fun extensionVararg (i: kotlin.Int, vararg s: kotlin.String): kotlin.Unit declared in ' type=kotlin.Unit origin=null + $receiver: GET_VAR 'p0: .C declared in .testExtensionVararg.extensionVararg' type=.C origin=null + i: GET_VAR 'p1: kotlin.Int declared in .testExtensionVararg.extensionVararg' type=kotlin.Int origin=null FUN name:testExtensionDefault visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY CALL 'public final fun use (f: @[ExtensionFunctionType] kotlin.Function2<.C, kotlin.Int, kotlin.Unit>): kotlin.Unit declared in ' type=kotlin.Unit origin=null - f: FUNCTION_REFERENCE 'public final fun extensionDefault (i: kotlin.Int, s: kotlin.String): kotlin.Unit declared in ' type=kotlin.reflect.KFunction2<.C, kotlin.Int, kotlin.Unit> origin=null reflectionTarget= + f: FUN_EXPR type=kotlin.Function2<.C, kotlin.Int, kotlin.Unit> origin=ADAPTED_FUNCTION_REFERENCE + FUN ADAPTER_FOR_CALLABLE_REFERENCE name:extensionDefault visibility:local modality:FINAL <> (p0:.C, p1:kotlin.Int) returnType:kotlin.Unit + VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:.C + VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p1 index:1 type:kotlin.Int + BLOCK_BODY + CALL 'public final fun extensionDefault (i: kotlin.Int, s: kotlin.String): kotlin.Unit declared in ' type=kotlin.Unit origin=null + $receiver: GET_VAR 'p0: .C declared in .testExtensionDefault.extensionDefault' type=.C origin=null + i: GET_VAR 'p1: kotlin.Int declared in .testExtensionDefault.extensionDefault' type=kotlin.Int origin=null FUN name:testExtensionBoth visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY CALL 'public final fun use (f: @[ExtensionFunctionType] kotlin.Function2<.C, kotlin.Int, kotlin.Unit>): kotlin.Unit declared in ' type=kotlin.Unit origin=null - f: FUNCTION_REFERENCE 'public final fun extensionBoth (i: kotlin.Int, s: kotlin.String, vararg t: kotlin.String): kotlin.Unit declared in ' type=kotlin.reflect.KFunction2<.C, kotlin.Int, kotlin.Unit> origin=null reflectionTarget= + f: FUN_EXPR type=kotlin.Function2<.C, kotlin.Int, kotlin.Unit> origin=ADAPTED_FUNCTION_REFERENCE + FUN ADAPTER_FOR_CALLABLE_REFERENCE name:extensionBoth visibility:local modality:FINAL <> (p0:.C, p1:kotlin.Int) returnType:kotlin.Unit + VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:.C + VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p1 index:1 type:kotlin.Int + BLOCK_BODY + CALL 'public final fun extensionBoth (i: kotlin.Int, s: kotlin.String, vararg t: kotlin.String): kotlin.Unit declared in ' type=kotlin.Unit origin=null + $receiver: GET_VAR 'p0: .C declared in .testExtensionBoth.extensionBoth' type=.C origin=null + i: GET_VAR 'p1: kotlin.Int declared in .testExtensionBoth.extensionBoth' type=kotlin.Int origin=null diff --git a/compiler/testData/ir/irText/expressions/callableReferences/kt37131.fir.kt.txt b/compiler/testData/ir/irText/expressions/callableReferences/kt37131.fir.kt.txt index f4cd26c31b7..eda7defdd2c 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/kt37131.fir.kt.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/kt37131.fir.kt.txt @@ -20,7 +20,10 @@ fun use(fn: Function0): Any { } fun testFn(): Any { - return use(fn = ::foo) + return use(fn = local fun foo(): String { + return foo() + } +) } fun testCtor(): Any { diff --git a/compiler/testData/ir/irText/expressions/callableReferences/kt37131.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/kt37131.fir.txt index e103fdec210..31909678561 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/kt37131.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/kt37131.fir.txt @@ -49,7 +49,11 @@ FILE fqName: fileName:/kt37131.kt BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun testFn (): kotlin.Any declared in ' CALL 'public final fun use (fn: kotlin.Function0): kotlin.Any declared in ' type=kotlin.Any origin=null - fn: FUNCTION_REFERENCE 'public final fun foo (x: kotlin.String): kotlin.String declared in ' type=kotlin.reflect.KFunction0 origin=null reflectionTarget= + fn: FUN_EXPR type=kotlin.Function0 origin=ADAPTED_FUNCTION_REFERENCE + FUN ADAPTER_FOR_CALLABLE_REFERENCE name:foo visibility:local modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + RETURN type=kotlin.Nothing from='local final fun foo (): kotlin.String declared in .testFn' + CALL 'public final fun foo (x: kotlin.String): kotlin.String declared in ' type=kotlin.String origin=null FUN name:testCtor visibility:public modality:FINAL <> () returnType:kotlin.Any BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun testCtor (): kotlin.Any declared in ' diff --git a/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.kt.txt b/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.kt.txt index 3f60c4c9a20..5a8cd7bbd58 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.kt.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.kt.txt @@ -32,7 +32,7 @@ object Obj : A { fun testUnbound() { use1(fn = local fun foo(p0: A, p1: Int) { - p0.foo() + p0.foo(xs = [p1]) } ) } diff --git a/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt index d74ac176161..ddd260c9264 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/unboundMemberReferenceWithAdaptedArguments.fir.txt @@ -67,6 +67,8 @@ FILE fqName: fileName:/unboundMemberReferenceWithAdaptedArguments.kt BLOCK_BODY CALL 'public open fun foo (vararg xs: kotlin.Int): kotlin.Int declared in .A' type=kotlin.Int origin=null $this: GET_VAR 'p0: .A declared in .testUnbound.foo' type=.A origin=null + xs: VARARG type=kotlin.IntArray varargElementType=kotlin.Int + GET_VAR 'p1: kotlin.Int declared in .testUnbound.foo' type=kotlin.Int origin=null FUN name:testBound visibility:public modality:FINAL <> (a:.A) returnType:kotlin.Unit VALUE_PARAMETER name:a index:0 type:.A BLOCK_BODY diff --git a/compiler/testData/ir/irText/expressions/callableReferences/withAdaptedArguments.fir.kt.txt b/compiler/testData/ir/irText/expressions/callableReferences/withAdaptedArguments.fir.kt.txt index 8063416bc28..bd358b64efa 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/withAdaptedArguments.fir.kt.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/withAdaptedArguments.fir.kt.txt @@ -35,7 +35,10 @@ object Host { } fun testDefault(): String { - return use(fn = ::fnWithDefault) + return use(fn = local fun fnWithDefault(p0: Int): String { + return fnWithDefault(a = p0) + } +) } fun testVararg(): String { @@ -63,9 +66,15 @@ fun testImportedObjectMember(): String { } fun testDefault0(): String { - return use0(fn = ::fnWithDefaults) + return use0(fn = local fun fnWithDefaults(): String { + return fnWithDefaults() + } +) } fun testVararg0(): String { - return use0(fn = ::fnWithVarargs) + return use0(fn = local fun fnWithVarargs(): String { + return fnWithVarargs() + } +) } diff --git a/compiler/testData/ir/irText/expressions/callableReferences/withAdaptedArguments.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/withAdaptedArguments.fir.txt index d9b00022000..c37272be7a3 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/withAdaptedArguments.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/withAdaptedArguments.fir.txt @@ -67,7 +67,13 @@ FILE fqName: fileName:/withAdaptedArguments.kt BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun testDefault (): kotlin.String declared in ' CALL 'public final fun use (fn: kotlin.Function1): kotlin.String declared in ' type=kotlin.String origin=null - fn: FUNCTION_REFERENCE 'public final fun fnWithDefault (a: kotlin.Int, b: kotlin.Int): kotlin.String declared in ' type=kotlin.reflect.KFunction1 origin=null reflectionTarget= + fn: FUN_EXPR type=kotlin.Function1 origin=ADAPTED_FUNCTION_REFERENCE + FUN ADAPTER_FOR_CALLABLE_REFERENCE name:fnWithDefault visibility:local modality:FINAL <> (p0:kotlin.Int) returnType:kotlin.String + VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:kotlin.Int + BLOCK_BODY + RETURN type=kotlin.Nothing from='local final fun fnWithDefault (p0: kotlin.Int): kotlin.String declared in .testDefault' + CALL 'public final fun fnWithDefault (a: kotlin.Int, b: kotlin.Int): kotlin.String declared in ' type=kotlin.String origin=null + a: GET_VAR 'p0: kotlin.Int declared in .testDefault.fnWithDefault' type=kotlin.Int origin=null FUN name:testVararg visibility:public modality:FINAL <> () returnType:kotlin.String BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun testVararg (): kotlin.String declared in ' @@ -109,9 +115,17 @@ FILE fqName: fileName:/withAdaptedArguments.kt BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun testDefault0 (): kotlin.String declared in ' CALL 'public final fun use0 (fn: kotlin.Function0): kotlin.String declared in ' type=kotlin.String origin=null - fn: FUNCTION_REFERENCE 'public final fun fnWithDefaults (a: kotlin.Int, b: kotlin.Int): kotlin.String declared in ' type=kotlin.reflect.KFunction0 origin=null reflectionTarget= + fn: FUN_EXPR type=kotlin.Function0 origin=ADAPTED_FUNCTION_REFERENCE + FUN ADAPTER_FOR_CALLABLE_REFERENCE name:fnWithDefaults visibility:local modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + RETURN type=kotlin.Nothing from='local final fun fnWithDefaults (): kotlin.String declared in .testDefault0' + CALL 'public final fun fnWithDefaults (a: kotlin.Int, b: kotlin.Int): kotlin.String declared in ' type=kotlin.String origin=null FUN name:testVararg0 visibility:public modality:FINAL <> () returnType:kotlin.String BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun testVararg0 (): kotlin.String declared in ' CALL 'public final fun use0 (fn: kotlin.Function0): kotlin.String declared in ' type=kotlin.String origin=null - fn: FUNCTION_REFERENCE 'public final fun fnWithVarargs (vararg xs: kotlin.Int): kotlin.String declared in ' type=kotlin.reflect.KFunction0 origin=null reflectionTarget= + fn: FUN_EXPR type=kotlin.Function0 origin=ADAPTED_FUNCTION_REFERENCE + FUN ADAPTER_FOR_CALLABLE_REFERENCE name:fnWithVarargs visibility:local modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + RETURN type=kotlin.Nothing from='local final fun fnWithVarargs (): kotlin.String declared in .testVararg0' + CALL 'public final fun fnWithVarargs (vararg xs: kotlin.Int): kotlin.String declared in ' type=kotlin.String origin=null diff --git a/compiler/testData/ir/irText/expressions/callableReferences/withVarargViewedAsArray.fir.kt.txt b/compiler/testData/ir/irText/expressions/callableReferences/withVarargViewedAsArray.fir.kt.txt index b124b6438db..6d71b1eb0ad 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/withVarargViewedAsArray.fir.kt.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/withVarargViewedAsArray.fir.kt.txt @@ -48,5 +48,8 @@ fun testArrayAsVararg() { } fun testArrayAndDefaults() { - useStringArray(fn = ::zap) + useStringArray(fn = local fun zap(p0: Array) { + zap(b = [*p0]) + } +) } diff --git a/compiler/testData/ir/irText/expressions/callableReferences/withVarargViewedAsArray.fir.txt b/compiler/testData/ir/irText/expressions/callableReferences/withVarargViewedAsArray.fir.txt index bfeb06966e0..02ca2409cea 100644 --- a/compiler/testData/ir/irText/expressions/callableReferences/withVarargViewedAsArray.fir.txt +++ b/compiler/testData/ir/irText/expressions/callableReferences/withVarargViewedAsArray.fir.txt @@ -82,4 +82,11 @@ FILE fqName: fileName:/withVarargViewedAsArray.kt FUN name:testArrayAndDefaults visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY CALL 'public final fun useStringArray (fn: kotlin.Function1, kotlin.Unit>): kotlin.Unit declared in ' type=kotlin.Unit origin=null - fn: FUNCTION_REFERENCE 'public final fun zap (vararg b: kotlin.String, k: kotlin.Int): kotlin.Unit declared in ' type=kotlin.reflect.KFunction1, kotlin.Unit> origin=null reflectionTarget= + fn: FUN_EXPR type=kotlin.Function1, kotlin.Unit> origin=ADAPTED_FUNCTION_REFERENCE + FUN ADAPTER_FOR_CALLABLE_REFERENCE name:zap visibility:local modality:FINAL <> (p0:kotlin.Array) returnType:kotlin.Unit + VALUE_PARAMETER ADAPTER_PARAMETER_FOR_CALLABLE_REFERENCE name:p0 index:0 type:kotlin.Array + BLOCK_BODY + CALL 'public final fun zap (vararg b: kotlin.String, k: kotlin.Int): kotlin.Unit declared in ' type=kotlin.Unit origin=null + b: VARARG type=kotlin.Array varargElementType=kotlin.String + SPREAD_ELEMENT + GET_VAR 'p0: kotlin.Array declared in .testArrayAndDefaults.zap' type=kotlin.Array origin=null